神秘的 J2ee 殿堂

          ·古之學(xué)者必有師·做學(xué)者亦要做師者·FIGHTING·

          java反射應(yīng)用對(duì)BEAN的操作,寫通用類、標(biāo)簽時(shí)有用


          package
           com.hunau.liuyong;

          import java.lang.reflect.Field;
          import java.lang.reflect.InvocationTargetException;
          import java.lang.reflect.Method;
          import java.util.ArrayList;
          import java.util.List;

          //其中User就是一個(gè)bean,里面就一個(gè)name屬性
          public
           class Test {
              
          public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
          User user
          =new User();
          List list
          =new ArrayList();
          list.add(user);
          System.out.print(
          "+++++++");
          Method method
          =getSetMethod(list.get(0).getClass(),"name");
          System.out.print(
          "+++++++");
          method.invoke(list.get(
          0),"liuyong");
          method
          =getGetMethod(list.get(0).getClass(),"name");
          System.out.println(method.invoke(list.get(
          0),null));
          }

          public static Method getGetMethod(Class objectClass,String fieldName) throws SecurityException, NoSuchMethodException{
              System.out.print(
          "+++++++");
              StringBuffer sb
          =new StringBuffer();
              sb.append(
          "get");
              sb.append(fieldName.substring(
          0,1).toUpperCase());
              sb.append(fieldName.substring(
          1));
              Method method
          =objectClass.getMethod(sb.toString(), null);
              System.out.print(
          "+++++++");
              
          return method;
          }

          public static Method getSetMethod(Class objectClass,String fieldName) throws SecurityException, NoSuchMethodException, NoSuchFieldException{
              Class[] parameterTypes
          =new Class[1];
              Field field
          =objectClass.getDeclaredField(fieldName);
              System.out.println(
          "dasg");
              parameterTypes[
          0]=field.getType();
              StringBuffer sb
          =new StringBuffer();
              System.out.println(
          "dasg");
              sb.append(
          "set");
              sb.append(fieldName.substring(
          0,1).toUpperCase());
              sb.append(fieldName.substring(
          1));
              System.out.println(fieldName);
              Method method
          =objectClass.getMethod(sb.toString(), parameterTypes);
              System.out.println(
          "dasg");
              
          return method;
              
          }
          }

          posted on 2008-07-11 13:52 月芽?jī)?/a> 閱讀(788) 評(píng)論(1)  編輯  收藏 所屬分類: J2EE學(xué)習(xí)心得

          評(píng)論

          # re: java反射應(yīng)用對(duì)BEAN的操作,寫通用類、標(biāo)簽時(shí)有用 2010-07-30 09:32 鋼炮

          寫點(diǎn)實(shí)戰(zhàn)性的東西比較好吧!!  回復(fù)  更多評(píng)論   

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 将乐县| 特克斯县| 定襄县| 津市市| 舒城县| 同心县| 许昌市| 中宁县| 临猗县| 迁西县| 台北县| 涞水县| 阿鲁科尔沁旗| 开原市| 肃南| 昌江| 梁平县| 玉林市| 潜山县| 门源| 恩平市| 建瓯市| 汉中市| 京山县| 海淀区| 灵宝市| 五峰| 周宁县| 青海省| 兖州市| 育儿| 平阴县| 伊宁市| 漳州市| 柯坪县| 津南区| 湖州市| 内黄县| 洛隆县| 乌什县| 精河县|