ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          1.調用構造函數的例子
          /**
               * Invoke a constructor on a class using reflection.
               *
               * @param klass The class.
               * @param classes The classes in the parameter list.
               * @param objects The objects to be used as parameters.
               * @return The object constructed.
               */
              public static Object invokeConstructor(final Class klass, final Class[] classes, final Object[] objects) {
                  try {
                      Constructor constructor;
                      try {
                          constructor = klass.getDeclaredConstructor(classes);
                      }
                      catch (NoSuchMethodException e) {
                          constructor = klass.getConstructor(classes);
                      }
                      constructor.setAccessible(true);
                      return constructor.newInstance(objects);
                  }
                  catch (NoSuchMethodException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (InstantiationException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (IllegalAccessException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (InvocationTargetException e) {
                      throw new RuntimeException(e.getTargetException().getMessage());
                  }
              }

          2.調用域的例子
          /**
               * Get the value of an instance field on an object using reflection.
               *
               * @param instance The instance of the object.
               * @param fieldName The name of the field.
               * @return The object returned by getting the field.
               */
              public static Object invokeGetInstanceField(final Object instance, final String fieldName) {
                  try {
                      Field field;
                      try {
                          field = instance.getClass().getField(fieldName);
                      }
                      catch (NoSuchFieldException e) {
                          field = instance.getClass().getDeclaredField(fieldName);
                      }
                      field.setAccessible(true);
                      return field.get(instance);
                  }
                  catch (NoSuchFieldException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (IllegalAccessException e) {
                      throw new RuntimeException(e.getMessage());
                  }
              }

          3.調用類方法的例子
          /**
               * Invoke an instance method on an object using reflection.
               *
               * @param instance The instance of the object.
               * @param methodName The name of the method.
               * @param classes The classes in the parameter list.
               * @param objects The objects to be used as parameters.
               * @return The object returned by invoking the method.
               */
              public static Object invokeInstanceMethod(
                      final Object instance, final String methodName, final Class[] classes, final Object[] objects) {

                  try {
                      Method method;
                      try {
                          method = instance.getClass().getDeclaredMethod(methodName, classes);
                      }
                      catch (NoSuchMethodException e) {
                          method = instance.getClass().getMethod(methodName, classes);
                      }
                      method.setAccessible(true);
                      return method.invoke(instance, objects);
                  }
                  catch (NoSuchMethodException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (IllegalAccessException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (InvocationTargetException e) {
                      throw new RuntimeException(e.getTargetException().getMessage());
                  }
              }

          4.調用靜態方法的例子
          /**
               * Invoke a static method on a class using reflection.
               *
               * @param klass The class.
               * @param methodName The name of the method.
               * @param classes The classes in the parameter list.
               * @param objects The objects to be used as parameters.
               * @return The object returned by invoking the method.
               */
              public static Object invokeStaticMethod(
                      final Class klass, final String methodName, final Class[] classes, final Object[] objects) {

                  try {
                      Method method;
                      try {
                          method = klass.getDeclaredMethod(methodName, classes);
                      }
                      catch (NoSuchMethodException e) {
                          method = klass.getMethod(methodName, classes);
                      }
                      method.setAccessible(true);
                      return method.invoke(klass, objects);
                  }
                  catch (NoSuchMethodException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (IllegalAccessException e) {
                      throw new RuntimeException(e.getMessage());
                  }
                  catch (InvocationTargetException e) {
                      throw new RuntimeException(e.getTargetException().getMessage());
                  }
              }
          posted on 2005-08-04 14:47 ivaneeo 閱讀(409) 評論(0)  編輯  收藏 所屬分類: java魔力
          主站蜘蛛池模板: 安远县| 开封市| 荥阳市| 横山县| 南华县| 丹阳市| 克什克腾旗| 镇沅| 闸北区| 樟树市| 夏邑县| 襄城县| 独山县| 岐山县| 澄城县| 武定县| 南溪县| 湖口县| 舒城县| 通道| 龙里县| 樟树市| 怀柔区| 新龙县| 桐城市| 墨竹工卡县| 雷山县| 松原市| 盐源县| 正定县| 胶州市| 观塘区| 桦甸市| 宜州市| 伊宁市| 怀柔区| 冀州市| 兖州市| 南阳市| 治县。| 仁化县|