Java 获取字段名和值并将其存储在映射中

Java 获取字段名和值并将其存储在映射中,java,Java,这是我获取字段值和注释值的代码,我将这些值放在地图中,但问题是地图中有一个$This Map doc = null; String kind = null; Class classObj = obj.getClass(); Annotation[] annotations = classObj.getAnnotations(); for (Annotation annotation : annotations) {

这是我获取字段值和注释值的代码,我将这些值放在地图中,但问题是地图中有一个
$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

字段
此$0
是对外部类的引用

如果要避免这种情况,请将类设置为static。IMHO您应该尽可能使嵌套类保持静态,部分是为了性能,但主要是为了清晰。如果这不是一个选项,您可以显式忽略该字段


顺便说一句:如果您创建一个名为
this$0
的字段,它将使用
this$$0
this$$$$0
等字段。您可以使用该字段获取类变量并存储在映射中。
Field[]fields=YourClassName.class.getFields()

你能用四个空格缩进你的代码吗?我怎样才能避免被存储在映射中,我需要映射只包含字段值(在类中声明)使类是静态的,而不包含字段。如果无法执行此操作,请添加
If(!field.getName().equals(“this$0”))
其字段所在的“obj”可以是任何对象。当我构建一个库时,用户可以创建任何传递,并将其传递到将解析字段值和注释值的方法中。在这种情况下,您可以按名称排除它。如果我只传递一个对象,而它不知道其中的类名,该怎么办。我只是得到了“对象”,你可以看到这个例子。对象属于任何类。你永远不需要知道如何使用,就像下面的例子一样。public static void main(String[]args){Object obj=new YourClass();java.lang.reflect.Field[]fields=obj.getClass().getDeclaredFields();System.out.println(“循环前”);for(java.lang.reflect.Field:fields){System.out.println(Field.getName();}