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)

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 德惠市| 武清区| 台中县| 乾安县| 开原市| 平凉市| 叙永县| 承德县| 安岳县| 皋兰县| 怀来县| 寿光市| 新宁县| 都江堰市| 游戏| 丽水市| 开原市| 富裕县| 肇州县| 遵化市| 土默特右旗| 珲春市| 德昌县| 宜城市| 咸丰县| 信丰县| 彰武县| 宜川县| 潢川县| 滦平县| 蓝山县| 疏附县| 沭阳县| 若羌县| 凭祥市| 沿河| 曲周县| 安塞县| 微博| 泰和县| 黑河市|