public String obj2String(Object obj){
if(null == obj) return null;
Class<?> clazz = obj.getClass();
Field [] fields = clazz.getDeclaredFields();
String clazzName = clazz.getName();
StringBuilder sb = new StringBuilder("\t"+clazzName);
sb.append("\t{");
try {
for (int i = 0,l = fields.length; i <l; i++) {
Field field = fields[i];
field.setAccessible(true);//true indicates that the reflected object should suppress Java language access checking when it is used.
sb.append("\n\t\t"+field.getName()+":"+field.get(obj));
if (null==field.get(obj)) {
sb.append("______________");//null
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("\n\t}");
return sb.toString();
}
if(null == obj) return null;
Class<?> clazz = obj.getClass();
Field [] fields = clazz.getDeclaredFields();
String clazzName = clazz.getName();
StringBuilder sb = new StringBuilder("\t"+clazzName);
sb.append("\t{");
try {
for (int i = 0,l = fields.length; i <l; i++) {
Field field = fields[i];
field.setAccessible(true);//true indicates that the reflected object should suppress Java language access checking when it is used.
sb.append("\n\t\t"+field.getName()+":"+field.get(obj));
if (null==field.get(obj)) {
sb.append("______________");//null
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("\n\t}");
return sb.toString();
}