1. Java 反射API的第一個主要作用是擷取程式在運行時刻的內部結構。這對於程式的檢查工具和調試器來說,是非常實用的功能。只需要短短的十幾行代碼,就可以遍曆出來一個Java類的內部結構,包括其中的構造方法、聲明的域和定義的方法等。這不得不說是一個很強大的能力。只要有了java.lang.Class類 的對象,就可以通過其中的方法來擷取到該類中的構造方法、域和方法。對應的方法分別是getConstructor、getField和getMethod。這三個方法還有相應的getDeclaredXXX版本,區別在於getDeclaredXXX版本的方法只會擷取該類自身所聲明的元素,而不會考慮繼承下來的。Constructor、Field和Method這三個類分別表示類中的構造方法、域和方法。這些類中的方法可以擷取到所對應結構的中繼資料。
2.待測試類別
Java代碼
- package com.home.action.test.mode.reflection;
-
- import java.util.Map;
-
- public class Counter {
- public int count ;
- public Map<String, Object> map;
-
- public Counter(int count) {
- this.count = count;
- }
-
- public void increase(int step) {
- count = count + step;
- System.out.println("count: "+count);
- }
- }
package com.home.action.test.mode.reflection;import java.util.Map;public class Counter { public int count ; public Map<String, Object> map; public Counter(int count) { this.count = count; } public void increase(int step) { count = count + step; System.out.println("count: "+count); }}
3.測試方法
Java代碼
- package com.home.action.test.mode.reflection;
-
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
-
- /**
- * 測試基本類的反射
- * @author li
- */
- public class Test {
- @SuppressWarnings("rawtypes")
- public static void main(String[] args) {
- try {
- //建構函式
- Constructor<Counter> contructor = Counter.class.getConstructor(int.class);
- System.out.println("contructorName: "+contructor.getName());
-
- Counter counter = contructor.newInstance(10);
- counter.increase(10);
-
- //基本方法
- Method[] methods = Counter.class.getMethods();
- for(Method method: methods) {
- System.out.println("method: "+method.getName());
- }
-
- Method method = Counter.class.getMethod("increase", int.class);
- method.invoke(counter, 6);
-
- //屬性值
- Field[] fields = Counter.class.getFields();
- for(Field field: fields) {
- System.out.println("fieldName: "+field.getName());
- }
-
- //count類型要設定public 否則擷取不到, 提示異常資訊
- Field field = Counter.class.getField("count");
- System.out.println("countField: "+field.getName());
-
- //處理泛型
- Field mapField = Counter.class.getDeclaredField("map");
- Type type = mapField.getGenericType();
- System.out.println("mapType: "+ type.toString());
-
- if(type instanceof ParameterizedType) {
- ParameterizedType paramerizedType = (ParameterizedType)type;
-
- Type[] actualTypes = paramerizedType.getActualTypeArguments();
- for(Type t: actualTypes) {
- if(t instanceof Class){
- Class clz = (Class)t;
- System.out.println("classType: "+ clz.getName());
- }
- }
- }
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- }
- }
- }