输出打印某个对象所有属性及属性值
由于对象的属性值太多,又不想用Debug调试查看,自己还懒,于是上网搜搜,整理了一下代码
package com.port.tools;
import java.lang.reflect.Field;
/**
* @author gch
* @date 2015-5-6
* @version 1.0
* 辅助测试
*/
public class PrintAllField {
public static <T> T outprint(String s1,Object o ){
try {
Class<?> c = Class.forName(s1);
Field [] fields = c.getDeclaredFields();
for(Field f:fields){
f.setAccessible(true);
}
System.out.println("============="+s1+"===============");
for(Field f:fields){
String field = f.toString().substring(f.toString().lastIndexOf(".")+1); //取出属性名称
System.out.println(field+" --> "+f.get(o));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}