如何在反射中使用递归



我有class test,它包含其他复杂对象private class2 e;,该对象包含其他复杂物体private class3 b;

public class class3 {
    private String x;
    private String y;
    public String getX() {
        return x;
    }
    public void setX(String x) {
        this.x = x;
    }
    public String getY() {
        return y;
    }
    public void setY(String y) {
        this.y = y;
    }
}
//class2
public class class2 {
    private String n;
    private class3 b;
    public String getN() {
        return n;
    }
    public void setN(String n) {
        this.n = n;
    }
    public class3 getB() {
        return b;
    }
    public void setB(class3 b) {
        this.b = b;
    }
}
//class test
public class test {
    private String w;
    private class2 e;
    public String getW() {
        return w;
    }
    public void setW(String w) {
        this.w = w;
    }
    public class2 getE() {
        return e;
    }
    public void setE(class2 e) {
        this.e = e;
    }
}

我需要完成的是从test中获得一个Object,我想调用所有getter,如果它从其他类返回复杂对象,我想递归,直到没有复杂对象离开

我可以读取所有test对象数据,我缺少的部分是隐性部分

这是我的代码:-

private static void writeInLogger(Object obj, String str) {
    Class klazz = obj.getClass();
    if (klazz.isPrimitive() || obj instanceof String
            || obj instanceof Integer || obj instanceof Double
            || obj instanceof Boolean)
        System.out.println(str + obj.toString());
    else {
        try {
            for (PropertyDescriptor propertyDescriptor : Introspector
                    .getBeanInfo(klazz).getPropertyDescriptors()) {
                Method m = propertyDescriptor.getReadMethod();
                if (m != null){
                Object object = m.invoke(obj);
                    Class klazz2 = object.getClass();
                    if(klazz2.isPrimitive() || object instanceof String|| object instanceof Integer || object instanceof Double|| object instanceof Boolean){
                        System.out.println(m + str + m.invoke(obj).toString());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }
}

更新了writeInLogger方法。我还添加了一些内容来检查write方法,否则你会得到Class作为属性,堆栈就会崩溃。

private static void writeInLogger(Object obj, String str) {
    Class klazz = obj.getClass();
    if (klazz.isPrimitive() || obj instanceof String || obj instanceof Integer || obj instanceof Double
            || obj instanceof Boolean)
        System.out.println(str + obj.toString());
    else {
        try {
            for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(klazz).getPropertyDescriptors()) {
                if(propertyDescriptor.getWriteMethod() == null)
                    continue;
                Method m = propertyDescriptor.getReadMethod();
                if (m != null) {
                    Object object = m.invoke(obj);
                    if(object != null)
                        writeInLogger(object, str);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }
}
private static void writeInLogger(Object obj, String str) {
        if (obj == null) {
             System.out.println(str + "null");
             return;
        }
        Class klazz = obj.getClass();        
        if (klazz.isPrimitive() || obj instanceof String
                || obj instanceof Integer || obj instanceof Double
                || obj instanceof Boolean)
            System.out.println(str + obj.toString());
        else {
            try {
                for (Field field : klazz.getDeclaredFields()) {
                    field.setAccessible(true);
                    Object f = field.get(obj);
                    field.setAccessible(false);
                    writeInLogger(f, str);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IntrospectionException e) {
                e.printStackTrace();
            }
        }
}

浏览类中的所有字段,并获取值对象作为writeInLogger参数。

getFields()只获取公共字段,getDeclaredFields获取类中的所有字段。

相关内容

  • 没有找到相关文章

最新更新