- 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
- 3 Class ownerClass = owner.getClass();
- 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(owner, args);
- 14 }
- Class owner_class = owner.getClass() :首先還是必須得到這個對象的Class。
- 5 ~ 9 行:配置參數的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
- 2 public Object newInstance(String className, Object[] args) throws Exception {
- 3 Class newoneClass = 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 Constructor cons = newoneClass.getConstructor(argsClass);
- 12
- 13 return cons.newInstance(args);
- 14
- 15 }
- 這裏說的方法是執行帶參數的構造函數來新建實例的方法。如果不需要參數,可以直接使用newoneClass.newInstance()來實現。
- Class newoneClass = Class.forName(className):第一步,得到要構造的實例的Class。
- 第5 ~第 9 行:得到參數的Class數組。
- Constructor cons = newoneClass.getConstructor(argsClass):得到構造子。
- 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 }
- 附完整源碼:
- import java.lang.reflect.Array;
- 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);
- }
- }
Java反射經典實例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.