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 閱讀(117) | 評論 (1)編輯 收藏

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 前郭尔| 定西市| 民和| 南通市| 称多县| 砀山县| 梁平县| 启东市| 东方市| 曲沃县| 邳州市| 陆河县| 耒阳市| 秭归县| 罗定市| 潢川县| 永定县| 通榆县| 商南县| 郸城县| 海林市| 静宁县| 衡山县| 葵青区| 成武县| 张掖市| 寿宁县| 绵竹市| 呼和浩特市| 乌拉特前旗| 兴仁县| 中超| 新泰市| 宜君县| 霸州市| 怀宁县| 怀远县| 南康市| 祁门县| 济宁市| 龙井市|