Java 如何在反射中使用递归?
我有Java 如何在反射中使用递归?,java,reflection,Java,Reflection,我有类测试它包含其他复杂对象私有类2e并且该对象包含其他复杂对象私有类3b 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() { r
类测试
它包含其他复杂对象私有类2e代码>并且该对象包含其他复杂对象私有类3b代码>
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
中获得一个对象,我想调用所有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
参数
getFields()
仅获取公共字段,getDeclaredFields
获取类中的所有字段。更新了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();
}
}
}
要执行递归调用,您应该提取一个方法,该方法接受一个目标对象并对其调用getter,如果结果为“primitive”,则立即返回结果,或者调用本身。调用中的str
参数是什么?@KDM您可以将其设置为“”,它不重要,不起作用。控制台中没有打印任何内容,klazz.getClass().getFields()
没有字段
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();
}
}
}