(轉)reflect

           

          import java.lang.reflect.Constructor;

          import java.lang.reflect.Field;

          import java.lang.reflect.Method;





          /**

           * Java Reflection Cookbook

           *

           * 
          @author Michael Lee

           * 
          @since 2006-8-23

           * 
          @version 0.1a

           
          */



          public class Reflection {

              
          /**

               * 得到某個對象的公共屬性

               *

               * 
          @param owner, fieldName

               * 
          @return 該屬性對象

               * 
          @throws Exception

               *

               
          */

              
          public Object getProperty(Object owner, String fieldName) throws Exception {

                  Class ownerClass 
          = owner.getClass();



                  Field field 
          = ownerClass.getField(fieldName);



                  Object property 
          = field.get(owner);



                  
          return property;

              }



              
          /**

               * 得到某類的靜態公共屬性

               *

               * 
          @param className   類名

               * 
          @param fieldName   屬性名

               * 
          @return 該屬性對象

               * 
          @throws Exception

               
          */

              
          public Object getStaticProperty(String className, String fieldName)

                      
          throws Exception {

                  Class ownerClass 
          = Class.forName(className);



                  Field field 
          = ownerClass.getField(fieldName);



                  Object property 
          = field.get(ownerClass);



                  
          return property;

              }





              
          /**

               * 執行某對象方法

               *

               * 
          @param owner

               *            對象

               * 
          @param methodName

               *            方法名

               * 
          @param args

               *            參數

               * 
          @return 方法返回值

               * 
          @throws Exception

               
          */

              
          public Object invokeMethod(Object owner, String methodName, Object[] args)

                      
          throws Exception {



                  Class ownerClass 
          = owner.getClass();



                  Class[] argsClass 
          = new Class[args.length];



                  
          for (int i = 0, j = args.length; i < j; i++) {

                      argsClass[i] 
          = args[i].getClass();

                  }



                  Method method 
          = ownerClass.getMethod(methodName, argsClass);



                  
          return method.invoke(owner, args);

              }





                
          /**

               * 執行某類的靜態方法

               *

               * 
          @param className

               *            類名

               * 
          @param methodName

               *            方法名

               * 
          @param args

               *            參數數組

               * 
          @return 執行方法返回的結果

               * 
          @throws Exception

               
          */

              
          public Object invokeStaticMethod(String className, String methodName,

                      Object[] args) 
          throws Exception {

                  Class ownerClass 
          = Class.forName(className);



                  Class[] argsClass 
          = new Class[args.length];



                  
          for (int i = 0, j = args.length; i < j; i++) {

                      argsClass[i] 
          = args[i].getClass();

                  }



                  Method method 
          = ownerClass.getMethod(methodName, argsClass);



                  
          return method.invoke(null, args);

              }







              
          /**

               * 新建實例

               *

               * 
          @param className

               *            類名

               * 
          @param args

               *            構造函數的參數

               * 
          @return 新建的實例

               * 
          @throws Exception

               
          */

              
          public Object newInstance(String className, Object[] args) throws Exception {

                  Class newoneClass 
          = Class.forName(className);



                  Class[] argsClass 
          = new Class[args.length];



                  
          for (int i = 0, j = args.length; i < j; i++) {

                      argsClass[i] 
          = args[i].getClass();

                  }



                  Constructor cons 
          = newoneClass.getConstructor(argsClass);



                  
          return cons.newInstance(args);



              }





              

              
          /**

               * 是不是某個類的實例

               * 
          @param obj 實例

               * 
          @param cls 類

               * 
          @return 如果 obj 是此類的實例,則返回 true

               
          */

              
          public boolean isInstance(Object obj, Class cls) {

                  
          return cls.isInstance(obj);

              }

              

              
          /**

               * 得到數組中的某個元素

               * 
          @param array 數組

               * 
          @param index 索引

               * 
          @return 返回指定數組對象中索引組件的值

               
          */

              
          public Object getByArray(Object array, int index) {

                  
          return Array.get(array,index);

              }

          }

          posted on 2011-05-04 23:16 liujg 閱讀(139) 評論(0)  編輯  收藏 所屬分類: Java基礎

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          boddiy

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 邛崃市| 诏安县| 彝良县| 图片| 娱乐| 乃东县| 罗定市| 高要市| 吴桥县| 咸丰县| 论坛| 沁阳市| 新宾| 岢岚县| 衡东县| 鄂尔多斯市| 孙吴县| 石棉县| 金昌市| 札达县| 平遥县| 图片| 镇远县| 石屏县| 井研县| 西林县| 曲松县| 卢湾区| 临高县| 沭阳县| 阜新| 池州市| 甘谷县| 武隆县| 合江县| 平山县| 中山市| 浦东新区| 吉安县| 托克逊县| 连云港市|