How to get special methods of classes in Java?
•
Java
I have a class with some methods in Java, as shown below:
public class Class1
{
private String a;
private String b;
public setA(String a_){
this.a = a_;
}
public setb(String b_){
this.b = b_;
}
public String getA(){
return a;
}
@JsonIgnore
public String getb(){
return b;
}
}
I want to get all the methods starting with string get in Class1, which are not declared with the @ jsonignore annotation
What should I do?
Solution
You can use java reflection to iterate over all public and private methods:
Class1 obj = new Class1();
Class c = obj.getClass();
for (Method method : c.getDeclaredMethods()) {
if (method.getAnnotation(JsonIgnore.class) == null &&
method.getName().substring(0,3).equals("get")) {
System.out.println(method.getName());
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
