1.   Java 反射API的第一个主要作用是获取程序在运行时刻的内部结构。这对于程序的检查工具和调试器来说,是非常实用的功能。只需要短短的十几行代码,就可以遍历出来一个Java类的内部结构,包括其中的构造方法、声明的域和定义的方法等。这不得不说是一个很强大的能力。只要有了java.lang.Class类 的对象,就可以通过其中的方法来获取到该类中的构造方法、域和方法。对应的方法分别是getConstructor、getField和getMethod。这三个方法还有相应的getDeclaredXXX版本,区别在于getDeclaredXXX版本的方法只会获取该类自身所声明的元素,而不会考虑继承下来的。Constructor、Field和Method这三个类分别表示类中的构造方法、域和方法。这些类中的方法可以获取到所对应结构的元数据。

2.待测试类

Java代码
  1. package com.home.action.test.mode.reflection;   
  2.   
  3. import java.util.Map;   
  4.   
  5. public class Counter {   
  6.     public int count ;   
  7.     public Map<String, Object> map;   
  8.        
  9.     public Counter(int count) {   
  10.         this.count = count;   
  11.     }   
  12.        
  13.     public void increase(int step) {   
  14.         count = count + step;   
  15.         System.out.println("count: "+count);   
  16.     }   
  17. }  
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代码
  1. package com.home.action.test.mode.reflection;   
  2.   
  3. import java.lang.reflect.Constructor;   
  4. import java.lang.reflect.Field;   
  5. import java.lang.reflect.InvocationTargetException;   
  6. import java.lang.reflect.Method;   
  7. import java.lang.reflect.ParameterizedType;   
  8. import java.lang.reflect.Type;   
  9.   
  10. /**  
  11.  * 测试基本类的反射  
  12.  * @author li  
  13.  */  
  14. public class Test {   
  15.     @SuppressWarnings("rawtypes")   
  16.     public static void main(String[] args) {   
  17.         try {   
  18.             //构造函数   
  19.             Constructor<Counter> contructor = Counter.class.getConstructor(int.class);   
  20.             System.out.println("contructorName: "+contructor.getName());   
  21.                
  22.             Counter counter = contructor.newInstance(10);   
  23.             counter.increase(10);   
  24.                
  25.             //基本方法   
  26.             Method[] methods = Counter.class.getMethods();   
  27.             for(Method method: methods) {   
  28.                 System.out.println("method: "+method.getName());   
  29.             }   
  30.                
  31.             Method method = Counter.class.getMethod("increase"int.class);   
  32.             method.invoke(counter, 6);   
  33.                
  34.             //属性值   
  35.             Field[] fields = Counter.class.getFields();   
  36.             for(Field field: fields) {   
  37.                 System.out.println("fieldName: "+field.getName());   
  38.             }   
  39.                
  40.             //count类型要设置public 否则获取不到, 提示异常信息   
  41.             Field field = Counter.class.getField("count");   
  42.             System.out.println("countField: "+field.getName());   
  43.                
  44.             //处理泛型   
  45.             Field mapField = Counter.class.getDeclaredField("map");   
  46.             Type type = mapField.getGenericType();   
  47.             System.out.println("mapType: "+ type.toString());   
  48.                
  49.             if(type instanceof ParameterizedType) {   
  50.                 ParameterizedType paramerizedType = (ParameterizedType)type;   
  51.                    
  52.                 Type[] actualTypes = paramerizedType.getActualTypeArguments();   
  53.                 for(Type t: actualTypes) {   
  54.                     if(t instanceof Class){   
  55.                         Class clz = (Class)t;   
  56.                         System.out.println("classType: "+ clz.getName());   
  57.                     }   
  58.                 }   
  59.             }   
  60.         } catch (SecurityException e) {   
  61.             e.printStackTrace();   
  62.         } catch (NoSuchMethodException e) {   
  63.             e.printStackTrace();   
  64.         } catch (IllegalArgumentException e) {   
  65.             e.printStackTrace();   
  66.         } catch (InstantiationException e) {   
  67.             e.printStackTrace();   
  68.         } catch (IllegalAccessException e) {   
  69.             e.printStackTrace();   
  70.         } catch (InvocationTargetException e) {   
  71.             e.printStackTrace();   
  72.         } catch (NoSuchFieldException e) {   
  73.             e.printStackTrace();   
  74.         }   
  75.     }   
  76. }