asdtiang的博客 感谢blogjava提供的博客交流平台

java study reflect

Posted on 2010-09-19 17:13 asdtiang 阅读(175) 评论(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;
   }
  }
  
 }
 
 

}



天苍苍,野茫茫,风吹草底见牛羊

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)