LinuxChan的程序園地

          前端后端Web開發技術

           

          2009年1月23日

          兩個很簡單的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 @ 2009-01-23 12:04 LinuxChan 閱讀(503) | 評論 (2)編輯 收藏

          開博了,今后會將自己的一些靈感收集其中

          記錄人生成長的方向。

          posted @ 2009-01-23 11:57 LinuxChan 閱讀(118) | 評論 (1)編輯 收藏

          僅列出標題  

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 扎鲁特旗| 陇南市| 泸定县| 荆州市| 东源县| 阜城县| 昭觉县| 建平县| 城口县| 新乡市| 大厂| 嘉荫县| 柘城县| 牙克石市| 麟游县| 墨竹工卡县| 济源市| 巴林右旗| 巢湖市| 盘山县| 长子县| 安泽县| 肥乡县| 天气| 贵德县| 民县| 肇州县| 安泽县| 班玛县| 三明市| 舟曲县| 新津县| 和田市| 安庆市| 安顺市| 卢龙县| 和林格尔县| 吉隆县| 安西县| 康平县| 铜陵市|