这是我获取字段值和注释值的代码,我将值放入地图中,但问题是地图中有一个$this
。
Map doc = null;
String kind = null;
Class classObj = obj.getClass();
Annotation[] annotations = classObj.getAnnotations();
for (Annotation annotation : annotations) {
Entity entityAnnotation = (Entity)annotation;
kind = entityAnnotation.name();
if (entityAnnotation != null){
if (doc == null){
doc = new LinkedHashMap();
}
Field[] fields = classObj.getDeclaredFields();
for (Field field : fields){
annotations = field.getDeclaredAnnotations();
String value = null;
String fieldName = field.getName();
try {
boolean access = field.isAccessible();
field.setAccessible(true);
Object fieldValue = field.get(obj);
doc.put(fieldName, fieldValue);
field.setAccessible(access);
} catch (Exception e) {
e.printStackTrace();
}
// Process fields with annotation
for(Annotation fieldAnnotation : annotations){
if (annotation instanceof Id){
Id idAnnotation = (Id) fieldAnnotation;
log.info("Field name="+fieldName+" "+ value + " Annotation value: " + idAnnotation.value());
doc.put("_id", value);
}
}
}
}
}
测试输出:
Key=id value=id1
Key=name value=Eli
Key=age value=25
Key=this$0 value=org.goo.AnnotationTest@7f5227