Posted on 2010-09-19 17:13
asdtiang 阅读(238)
评论(0) 编辑 收藏
public class MyReflect {
public static void showInfoByClass(Object obj){
Class objClass = obj.getClass();
java.lang.reflect.Field[] fields = objClass
.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try{
java.lang.reflect.Method voMethod = objClass
.getMethod(
"get"
+ fields[i].getName()
.substring(0, 1)
.toUpperCase()
+ fields[i].getName()
.substring(1),
new Class[] {});
Object o=voMethod.invoke(obj, new Object[] {});
String result="";
result=o.toString();
Logger.info("class name:"+obj.getClass().getName()+" propName: "+fields[i].getName()+" methodName: get"
+ fields[i].getName()
.substring(0, 1)
.toUpperCase()
+ fields[i].getName()
.substring(1)+" value:"+result);
}
catch(Exception e){
Logger.info("class name:"+obj.getClass().getName()+" propName: "+fields[i].getName()+" methodName: get"
+ fields[i].getName()
.substring(0, 1)
.toUpperCase()
+ fields[i].getName()
.substring(1)+"no method");
continue;
}
}
}
}
天苍苍,野茫茫,风吹草底见牛羊