神秘的 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 月芽兒 閱讀(780) 評論(1)  編輯  收藏 所屬分類: J2EE學習心得

          評論

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

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

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 杭锦旗| 左云县| 蚌埠市| 辉南县| 广宗县| 乌审旗| 平舆县| 烟台市| 汝州市| 曲阜市| 内黄县| 原平市| 页游| 三亚市| 胶南市| 巴彦县| 南木林县| 阳新县| 临猗县| 项城市| 东乡| 来凤县| 乐都县| 敦化市| 宁远县| 乌苏市| 柳河县| 政和县| 渑池县| 平果县| 革吉县| 措勤县| 延安市| 永仁县| 长寿区| 临湘市| 五河县| 泾源县| 喜德县| 五大连池市| 手游|