시소당
해당 DTO내의 모든 필드값을 출력한다. 구지 DTO마다 toString()과 같이 필드명을 출력하는 메소드를 만들지않고(단지 getter와 setter만 구현) 이후 디버그시 아래코드를 공통 유틸에 포함시켜 디버그에 활용하도록 한다.
CommUtil.java
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public static void xyDebugList(List list) {
if (IS_DEBUG) {
for (int i = 0; list != null && i < list.size(); i++) {
Object o = list.get(i);
xyDebugBean(o);
}
}
}
public static void xyDebugBean(Object o) {
if (IS_DEBUG) {
Method[] m = o.getClass().getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
if (m[i].getName().startsWith("get")) {
try {
System.out.println(
m[i].getName()
+ " : "
+ m[i].invoke(o, null)
+ " ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
xyDebugln();
}
[예제 1]
public class Test1 {
public static void main(String[] args)
throws
SecurityException,
NoSuchMethodException,
IllegalArgumentException,
IllegalAccessException,
InvocationTargetException {
ArrayList list = new ArrayList();
list.add(new TestDto(1000, "[0]"));
list.add(new TestDto(2000, "[1]"));
list.add(new TestDto(3000, "[2]"));
CommUtil.formatData(list);
CommUtil.xyDebugList(list);
}
}