내가 가지고 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;
}
}
나는를 가지고있다 달성하기 위해 필요한 것은 Object
에서 test
내가 모든 게터를 호출 할하고 복잡한 객체를 반환하는 경우 다른 클래스의 객체를 내가 남아있는 복잡한 객체까지 반복적으로 가고 싶어
나는 모든 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
방법을 업데이트했습니다 . 또한 쓰기 방법을 확인하기 위해 추가했습니다. 그렇지 않으면 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();
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다