(轉)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 閱讀(132) 評論(0)  編輯  收藏 所屬分類: Java基礎

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          boddiy

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 馆陶县| 齐河县| 台南市| 临湘市| 怀宁县| 柞水县| 中阳县| 灵石县| 福清市| 开原市| 乌鲁木齐县| 方山县| 同仁县| 进贤县| 蓬莱市| 巴马| 梓潼县| 太谷县| 遂川县| 集安市| 甘洛县| 江源县| 鹤山市| 郎溪县| 西畴县| 恭城| 延庆县| 平和县| 宜城市| 会东县| 阿拉尔市| 汾西县| 德州市| 岳西县| 平遥县| 栾城县| 观塘区| 平塘县| 双牌县| 海城市| 乌鲁木齐县|