我想知道是否有什么方法可以在运行时获取类的注释信息?因为我想获得专门注释的属性。
例子:
class TestMain {
@Field(
store = Store.NO)
private String name;
private String password;
@Field(
store = Store.YES)
private int age;
//..........getter and setter
}
注释来自休眠搜索,现在我想要的是获取“TestMain”的哪个属性被注释为“字段”(在示例中,它们是[name,age]),哪个是“存储的( store=store.yes)'(在示例中,它们是 [ age ])在运行时。
有任何想法吗?
更新:
public class FieldUtil {
public static List<String> getAllFieldsByClass(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
ArrayList<String> fieldList = new ArrayList<String>();
ArrayList<String> storedList=new ArrayList<String>();
String tmp;
for (int i = 0; i < fields.length; i++) {
Field fi = fields[i];
tmp = fi.getName();
if (tmp.equalsIgnoreCase("serialVersionUID"))
continue;
if (fi.isAnnotationPresent(org.hibernate.search.annotations.Field.class)) {
//it is a "field",add it to list.
fieldList.add(tmp);
//make sure if it is stored also
Annotation[] ans = fi.getAnnotations();
for (Annotation an : ans) {
//here,how to get the detail annotation information
//I print the value of an,it is something like this:
//@org.hibernate.search.annotations.Field(termVector=NO, index=UN_TOKENIZED, store=NO, name=, boost=@org.hibernate.search.annotations.Boost(value=1.0), analyzer=@org.hibernate.search.annotations.Analyzer(impl=void, definition=), bridge=@org.hibernate.search.annotations.FieldBridge(impl=void, params=[]))
//how to get the parameter value of this an? using the string method?split?
}
}
}
return fieldList;
}
}