java 获取变量注释_java – 从类中获取带注释的变量

/** Annotation declaration */

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

public @interface isSearchable{

//...

}

@isSearchable

public String anyField = "any value";

检查如下:

//use MyClass.class.getDeclaredFields() if you want the fields only for this class.

//.getFields() returns the fields for all the class hierarchy

for(Field field : MyClass.class.getFields()){

isSearchable s = field.getAnnotation(isSearchable.class);

if (s != null) {

//field has the annotation isSearchable

} else {

//field has not the annotation

}

}


版权声明:本文为weixin_28890941原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。