LinuxChan的程序園地

          前端后端Web開發技術

           

          兩個很簡單的java反射應用

          1,是否對java Web中無處不在的Vo ,Dto比較無語呢?想要看看其中的數據,就必需不厭其煩的調用getXXX();所以寫了個BaseVo 只要繼承它,
          然后直接調用它的toString()方法,即可將里面的值都打印出來
              public String toString() {
                  Field[] fields 
          = this.getClass().getDeclaredFields();
                  StringBuffer result 
          = new StringBuffer();
                  
          for (Field field : fields) {
                      
          // System.out.prIntegerln(field.getName());
                      String fieldName = field.getName();
                      String methodName 
          = "get" + fieldName.substring(01).toUpperCase()
                              
          + fieldName.substring(1);
                      result.append(fieldName 
          + ":");
                      Method method 
          = null;
                      Object obj 
          = null;
                      
          try {
                          method 
          = UserVo.class.getMethod(methodName);
                          obj 
          = method.invoke(thisnull);
                      }
           catch (SecurityException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (NoSuchMethodException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (IllegalArgumentException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (IllegalAccessException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
           catch (InvocationTargetException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                      
          if (obj != null{
                          result.append(obj);
                      }
           else
                          result.append(
          "");
                      result.append(
          "\n");
                  }

                  
          return result.toString();
              }


          2,2個同類型的Vo/Dto,需要將一個中的值填充到另外一個里去。正常方法需要set/get 如果字段一多,簡直是噩夢。而利用反射,可以輕易實現。
           1    public static void parseVO(Object obj, Object target)
           2            throws Exception, NoSuchMethodException {
           3        for (Field field : obj.getClass().getDeclaredFields()) {
           4            Method getMethod = obj.getClass().getMethod(
           5                    "get" + field.getName().substring(01).toUpperCase()
           6                            + field.getName().substring(1));
           7            Object result = getMethod.invoke(obj);
           8            if (result != null{
           9                Method setMethod = target.getClass().getMethod(
          10                        "set" + field.getName().substring(01).toUpperCase()
          11                                + field.getName().substring(1), result.getClass());
          12                setMethod.invoke(target, result);
          13            }

          14        }

          15    }

          posted on 2009-01-23 12:04 LinuxChan 閱讀(501) 評論(2)  編輯  收藏

          評論

          # re: 兩個很簡單的java反射應用 2009-01-30 09:58 sorcerer

          反射的性能問題需要斟酌一下.  回復  更多評論   

          # re: 兩個很簡單的java反射應用 2009-02-13 14:19 Rory

          有時候比較簡單的也的確沒必要用反射,畢竟性能方面的考慮  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 钟祥市| 昆明市| 西平县| 太白县| 广昌县| 墨脱县| 福建省| 淮阳县| 万山特区| 阿尔山市| 增城市| 三穗县| 专栏| 定襄县| 南和县| 寿阳县| 武强县| 青冈县| 孟连| 陇南市| 监利县| 上高县| 青岛市| 清徐县| 策勒县| 南漳县| 赤城县| 安化县| 垣曲县| 遂溪县| 尚义县| 信阳市| 饶阳县| 井冈山市| 贡山| 巴彦县| 吉首市| 冕宁县| 日土县| 贡嘎县| 广河县|