編程生活

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            113 隨筆 :: 0 文章 :: 18 評(píng)論 :: 0 Trackbacks
          public class DayFactory {
              
          static {
                  
          try {
                      Constructor con 
          = Day.class.getDeclaredConstructors()[0];
                      Method[] methods 
          = con.getClass().getDeclaredMethods();
                      
          for (Method m : methods) {
                          
          if (m.getName().equals("acquireConstructorAccessor")) {
                              m.setAccessible(
          true);
                              m.invoke(con, 
          new Object[0]);
                          }

                      }

                      Field[] fields 
          = con.getClass().getDeclaredFields();
                      Object ca 
          = null;
                      
          for (Field f : fields) {
                          
          if (f.getName().equals("constructorAccessor")) {
                              f.setAccessible(
          true);
                              ca 
          = f.get(con);
                          }

                      }

                      Method m 
          = ca.getClass().getMethod("newInstance"new Class[] { Object[].class });
                      m.setAccessible(
          true);
                      Day v 
          = (Day) m.invoke(ca, new Object[] new Object[] "VACATION", Integer.MAX_VALUE } });
                      System.out.println(v.getClass() 
          + ":" + v.name() + ":" + v.ordinal());
                      
          for(Day day:Day.values()) {
                          System.out.println(day);
                      }

                  }
           catch (Exception ex) {
                      ex.printStackTrace();
                  }

              }

              
              
          public static void say() {
              }

              
              @Test
              
          public void ddd() {
                  DayFactory.say();
              }

          posted on 2009-08-19 09:07 wilesun 閱讀(1511) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阜康市| 开原市| 巧家县| 怀远县| 五原县| 济宁市| 勃利县| 神农架林区| 宜城市| 惠东县| 北流市| 定结县| 城市| 仙居县| 利辛县| 绥滨县| 平阳县| 长葛市| 扬州市| 双城市| 平遥县| 隆德县| 陆川县| 调兵山市| 五寨县| 江永县| 买车| 韩城市| 新巴尔虎右旗| 正镶白旗| 海晏县| 章丘市| 蒲城县| 南皮县| 青州市| 郯城县| 泾川县| 五莲县| 涪陵区| 新竹县| 灵丘县|