神秘的 J2ee 殿堂

          ·古之學者必有師·做學者亦要做師者·FIGHTING·

          java反射應用對BEAN的操作,寫通用類、標簽時有用


          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就是一個bean,里面就一個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 月芽兒 閱讀(788) 評論(1)  編輯  收藏 所屬分類: J2EE學習心得

          評論

          # re: java反射應用對BEAN的操作,寫通用類、標簽時有用 2010-07-30 09:32 鋼炮

          寫點實戰性的東西比較好吧!!  回復  更多評論   

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 上高县| 山东| 和林格尔县| 西乌珠穆沁旗| 宜兴市| 阿尔山市| 定结县| 梁山县| 且末县| 安徽省| 利津县| 武功县| 林周县| 彭水| 宜昌市| 革吉县| 通海县| 江孜县| 巴东县| 伊川县| 松原市| 馆陶县| 汉源县| 宁化县| 车险| 荆门市| 南通市| 亚东县| 枞阳县| 汉中市| 将乐县| 读书| 瑞安市| 昭平县| 安徽省| 平原县| 赤水市| 长治市| 延津县| 廉江市| 钟山县|