java反射实例

Java提供了一套机制来动态执行方法和构造方法,以及数组操作等,这套机制就叫——反射。反射机制是如今很多流行框架的实现基础,其中包括Spring、Hibernate等。原理性的问题不是本文的重点,接下来让我们在实例中学习这套精彩的机制。

1. 得到某个对象的属性

   1. public Object getProperty(Object owner, String fieldName) throws Exception {         
   2.     Class ownerClass = owner.getClass();                                             
   3.                                                                                      
   4.     Field field = ownerClass.getField(fieldName);                                    
   5.                                                                                      
   6.     Object property = field.get(owner);                                              
   7.                                                                                      
   8.     return property;                                                                 
   9. }      

                                                                           

Class ownerClass = owner.getClass():得到该对象的Class。

Field field = ownerClass.getField(fieldName):通过Class得到类声明的属性。

Object property = field.get(owner):通过对象得到该属性的实例,如果这个属性是非公有的,这里会报IllegalAccessException。

2. 得到某个类的静态属性

   1. public Object getStaticProperty(String className, String fieldName)       
   2.             throws Exception {                                            
   3.     Class ownerClass = Class.forName(className);                          
   4.                                                                           
   5.     Field field = ownerClass.getField(fieldName);                         
   6.                                                                           
   7.     Object property = field.get(ownerClass);                              
   8.                                                                           
   9.     return property;                                                      
  10. }               

                                                       

Class ownerClass = Class.forName(className) :首先得到这个类的Class。

Field field = ownerClass.getField(fieldName):和上面一样,通过Class得到类声明的属性。

Object property = field.get(ownerClass) :这里和上面有些不同,因为该属性是静态的,所以直接从类的Class里取。


3. 执行某对象的方法

   1. public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {      
   2.     Class ownerClass = owner.getClass();      
   3.     Class[] argsClass = new Class[args.length];      
   4.     for (int i = 0, j = args.length; i < j; i++) {      
   5.         argsClass[i] = args[i].getClass();      
   6.     }       
   7.     Method method = ownerClass.getMethod(methodName, argsClass);         
   8.     return method.invoke(owner, args);       
   9. }    


Class owner_class = owner.getClass() :首先还是必须得到这个对象的Class。

3~6行:配置参数的Class数组,作为寻找Method的条件。

Method method = ownerClass.getMethod(methodName, argsClass):通过Method名和参数的Class数组得到要执行的Method。

method.invoke(owner, args):执行该Method,invoke方法的参数是执行这个方法的对象,和参数数组。返回值是Object,也既是该方法的返回值。


4. 执行某个类的静态方法

   1. public Object invokeStaticMethod(String className, String methodName,               
   2.             Object[] args) throws Exception {                                       
   3.     Class ownerClass = Class.forName(className);                                    
   4.                                                                                     
   5.     Class[] argsClass = new Class[args.length];                                     
   6.                                                                                     
   7.     for (int i = 0, j = args.length; i < j; i++) {                                  
   8.         argsClass[i] = args[i].getClass();                                          
   9.     }                                                                               
  10.                                                                                     
  11.     Method method = ownerClass.getMethod(methodName, argsClass);                    
  12.                                                                                     
  13.     return method.invoke(null, args);                                               
  14. }         

                                                                       
基本的原理和实例3相同,不同点是最后一行,invoke的一个参数是null,因为这是静态方法,不需要借助实例运行。

5. 新建实例

   1. public Object newInstance(String className, Object[] args) throws Exception {       
   2.     Class newoneClass = Class.forName(className);                                   
   3.                                                                                     
   4.     Class[] argsClass = new Class[args.length];                                     
   5.                                                                                     
   6.     for (int i = 0, j = args.length; i < j; i++) {                                  
   7.         argsClass[i] = args[i].getClass();                                          
   8.     }                                                                               
   9.                                                                                     
  10.     Constructor cons = newoneClass.getConstructor(argsClass);                       
  11.                                                                                     
  12.     return cons.newInstance(args);                                                  
  13.                                                                                     
  14. }          

                                                                      
这里说的方法是执行带参数的构造函数来新建实例的方法。如果不需要参数,可以直接使用newoneClass.newInstance()来实现。

Class newoneClass = Class.forName(className):第一步,得到要构造的实例的Class。

第6~第10行:得到参数的Class数组。

   1. Constructor cons = newoneClass.getConstructor(argsClass):得到构造子。  
   2.  
   3. cons.newInstance(args):新建实例。  


6. 判断是否为某个类的实例

   1. public boolean isInstance(Object obj, Class cls) {           
   2.     return cls.isInstance(obj);                              
   3. }            

                                             

7. 得到数组中的某个元素

   1. public Object getByArray(Object array, int index) {          
   2.     return Array.get(array,index);                           
   3. }                   

                                      

附完整源码:

   1. import java.lang.reflect.Array;      
   2. import java.lang.reflect.Constructor;      
   3. import java.lang.reflect.Field;      
   4. import java.lang.reflect.Method;      
   5.      
   6.      
   7. /**    
   8.  * Java Reflection Cookbook    
   9.  *    
  10.  * @author Michael Lee    
  11.  * @since 2006-8-23    
  12.  * @version 0.1a    
  13.  */     
  14.      
  15. public class Reflection {      
  16.     /**    
  17.      * 得到某个对象的公共属性    
  18.      *    
  19.      * @param owner, fieldName    
  20.      * @return 该属性对象    
  21.      * @throws Exception    
  22.      *    
  23.      */     
  24.     public Object getProperty(Object owner, String fieldName) throws Exception {      
  25.         Class ownerClass = owner.getClass();      
  26.      
  27.         Field field = ownerClass.getField(fieldName);      
  28.      
  29.         Object property = field.get(owner);      
  30.      
  31.         return property;      
  32.     }      
  33.      
  34.     /**    
  35.      * 得到某类的静态公共属性    
  36.      *    
  37.      * @param className   类名    
  38.      * @param fieldName   属性名    
  39.      * @return 该属性对象    
  40.      * @throws Exception    
  41.      */     
  42.     public Object getStaticProperty(String className, String fieldName)      
  43.             throws Exception {      
  44.         Class ownerClass = Class.forName(className);      
  45.      
  46.         Field field = ownerClass.getField(fieldName);      
  47.      
  48.         Object property = field.get(ownerClass);      
  49.      
  50.         return property;      
  51.     }      
  52.      
  53.      
  54.     /**    
  55.      * 执行某对象方法    
  56.      *    
  57.      * @param owner    
  58.      *            对象    
  59.      * @param methodName    
  60.      *            方法名    
  61.      * @param args    
  62.      *            参数    
  63.      * @return 方法返回值    
  64.      * @throws Exception    
  65.      */     
  66.     public Object invokeMethod(Object owner, String methodName, Object[] args)      
  67.             throws Exception {      
  68.      
  69.         Class ownerClass = owner.getClass();      
  70.      
  71.         Class[] argsClass = new Class[args.length];      
  72.      
  73.         for (int i = 0, j = args.length; i < j; i++) {      
  74.             argsClass[i] = args[i].getClass();      
  75.         }      
  76.      
  77.         Method method = ownerClass.getMethod(methodName, argsClass);      
  78.      
  79.         return method.invoke(owner, args);      
  80.     }      
  81.      
  82.      
  83.       /**    
  84.      * 执行某类的静态方法    
  85.      *    
  86.      * @param className    
  87.      *            类名    
  88.      * @param methodName    
  89.      *            方法名    
  90.      * @param args    
  91.      *            参数数组    
  92.      * @return 执行方法返回的结果    
  93.      * @throws Exception    
  94.      */     
  95.     public Object invokeStaticMethod(String className, String methodName,      
  96.             Object[] args) throws Exception {      
  97.         Class ownerClass = Class.forName(className);      
  98.      
  99.         Class[] argsClass = new Class[args.length];      
100.      
101.         for (int i = 0, j = args.length; i < j; i++) {      
102.             argsClass[i] = args[i].getClass();      
103.         }      
104.      
105.         Method method = ownerClass.getMethod(methodName, argsClass);      
106.      
107.         return method.invoke(null, args);      
108.     }      
109.      
110.      
111.      
112.     /**    
113.      * 新建实例    
114.      *    
115.      * @param className    
116.      *            类名    
117.      * @param args    
118.      *            构造函数的参数    
119.      * @return 新建的实例    
120.      * @throws Exception    
121.      */     
122.     public Object newInstance(String className, Object[] args) throws Exception {      
123.         Class newoneClass = Class.forName(className);      
124.      
125.         Class[] argsClass = new Class[args.length];      
126.      
127.         for (int i = 0, j = args.length; i < j; i++) {      
128.             argsClass[i] = args[i].getClass();      
129.         }      
130.      
131.         Constructor cons = newoneClass.getConstructor(argsClass);      
132.      
133.         return cons.newInstance(args);      
134.      
135.     }      
136.      
137.      
138.           
139.     /**    
140.      * 是不是某个类的实例    
141.      * @param obj 实例    
142.      * @param cls 类    
143.      * @return 如果 obj 是此类的实例,则返回 true    
144.      */     
145.     public boolean isInstance(Object obj, Class cls) {      
146.         return cls.isInstance(obj);      
147.     }      
148.           
149.     /**    
150.      * 得到数组中的某个元素    
151.      * @param array 数组    
152.      * @param index 索引    
153.      * @return 返回指定数组对象中索引组件的值    
154.      */     
155.     public Object getByArray(Object array, int index) {      
156.         return Array.get(array,index);      
157.     }      
158. }     

你可能感兴趣的:(java,spring,Hibernate,框架,J#)