利用反射打印对象一般成员属性值

利用反射打印对象一般成员属性值

public static void printVoOrDtoVal(Object obj){

StringBuffer sb = new StringBuffer();
Method[] ms = obj.getClass().getDeclaredMethods();

try {
sb.append(obj.getClass().getSimpleName());
for(Method m : ms){
String mn = m.getName();
if(mn.startsWith("get")){
sb.append("[" + mn.substring(3, mn.length()) + ": " + m.invoke(obj, null)+ "]" );
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};

System.out.println(sb.toString());
}

你可能感兴趣的:(利用反射打印对象一般成员属性值)