posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          屬性填充 ( 懶人版)

          Posted on 2007-04-20 10:09 G_G 閱讀(364) 評論(0)  編輯  收藏 所屬分類: Inspiration
          主要類先上

          package ?myUtil;

          import ?java.lang.reflect.InvocationTargetException;
          import ?java.lang.reflect.Method;
          import ?java.util.Enumeration;
          import ?java.util.Hashtable;
          import ?java.util.Map;
          import ?java.util.Vector;

          public ? class ?CopyAttribute? {
          ????
          public ? static ? void ?copy(Object?bn1,Object?bn2)? throws ?Exception {
          ????????Hashtable?m1?
          = ? new ?Hashtable();
          ????????Hashtable?m2?
          = ? new ?Hashtable();
          ????????
          ????????
          // 取出?類?一?的?set?方法
          ????????Method[]?me1? = ??bn1.getClass().getMethods()?;
          ????????
          for ( int ?i = 0 ;i < me1.length;i ++ ) {
          ????????????String?name?
          = ?me1[i].getName()?;
          ????????????
          if (?name.indexOf( " set " ) == 0 ?) {
          ????????????????String?Att?
          = ?(name.substring( 3 ,name.length())).toUpperCase();
          ????????????????m1.put(Att,me1[i]);
          ????????????}

          ????????}

          ????????
          ????????
          // ????取出?類?二?的?get?方法
          ????????Method[]?me2? = ??bn2.getClass().getMethods()?;
          ????????
          for ( int ?i = 0 ;i < me2.length;i ++ ) {
          ????????????String?name?
          = ?me2[i].getName()?;
          ????????????
          if (?name.indexOf( " get " ) == 0 ?) {
          ????????????????String?Att?
          = ?(name.substring( 3 ,name.length())).toUpperCase();
          ????????????????m2.put(Att,me2[i]);
          ????????????}

          ????????}

          ????????
          ????????Enumeration?en2?
          = ??m2.keys();
          ????????Enumeration?en1?
          = ??m1.keys();
          ????????
          ????????
          while (en2.hasMoreElements()) {
          ????????????String?Att?
          = ?(String)en2.nextElement();
          ????????????Method?get?
          = ?(Method)m2.get(Att);
          ????????????Method?set?
          = ?(Method)m1.get(Att);
          ????????????
          ????????????
          if (set == null ) continue ;
          ????????????
          ????????????set.invoke(bn1,
          new ?Object[] {?get.invoke(bn2, new ?Object[] {} )?} );
          ????????}

          ????????
          ????}

          }




          運行
          }package?test;

          import?myUtil.CopyAttribute;
          import?Bean.Bean1;
          import?Bean.Bean2;
          import?junit.framework.TestCase;

          public?class?test?extends?TestCase?{

          ????
          protected?void?setUp()?throws?Exception?{
          ????????
          super.setUp();
          ????}


          ????
          protected?void?tearDown()?throws?Exception?{
          ????????
          super.tearDown();
          ????}

          ????
          ????
          public?void?testMyCopy()?throws?Exception{
          ????????
          ????????Bean1?b1?
          =?new?Bean1();
          ????????b1.setAvg(
          23);
          ????????b1.setName(
          "liukaiyi");
          ????????
          ????????Bean2?b2?
          =?new?Bean2();
          ????????b2.setName(null);
          ????????CopyAttribute.copy(b2,b1);
          ????????
          ????????assertNotNull(b2.getName());

          ???}
          }


          主站蜘蛛池模板: 安多县| 宕昌县| 太仓市| 郎溪县| 同仁县| 元朗区| 伊宁县| 习水县| 夏邑县| 耒阳市| 秭归县| 犍为县| 南昌市| 巴林左旗| 泾源县| 饶平县| 安图县| 梁山县| 桓台县| 甘谷县| 乌拉特中旗| 长泰县| 呈贡县| 哈尔滨市| 资源县| 洛浦县| 合作市| 巨鹿县| 邯郸市| 上犹县| 昌邑市| 舞阳县| 合川市| 广河县| 班戈县| 贵溪市| 天气| 蒲城县| 新乡县| 康平县| 墨竹工卡县|