我有一个类,在Java中有一些方法,如下所示:
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;
}
}
我希望得到Class1中以String get开头的所有方法,这些方法没有使用@JsonIgnore注释声明.
我该怎么做呢?
解决方法:
您可以使用Java Reflection迭代所有公共和私有方法:
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());
}
}
标签:java,methods,annotations
来源: https://codeday.me/bug/20190728/1558975.html