Java 反射机制是在运行状态中,对于任意一个类,都可以得到这个类的全部属性和方法,对于任意一个对象都可以调用它的任意一个属性和方法。这种在运行时动态的获取信息以及动态调用对象的方法的功能称为Java 的反射机制。java
Class 类与java.lang.reflect 类库一块儿对反射的概念进行了支持,该类库包含了Field,Method,Constructor类(每一个类都实现了Member 接口)。这些类型的对象时由JVM 在运行时建立的,用以表示未知类里对应的成员。编程
这样你就可使用Constructor 建立新的对象,用get() 和set() 方法读取和修改与Field 对象关联的字段,用invoke() 方法调用与Method 对象关联的方法。另外,还能够调用getFields() getMethods() 和 getConstructors() 等很便利的方法,以返回表示字段,方法,以及构造器的对象的数组。这样匿名对象的信息就能在运行时被彻底肯定下来,而在编译时不须要知道任何事情。数组
在Java 中能够经过三种方法获取类的字节码(Class)对象函数
ackage com.jas.reflect; public class ReflectTest { public static void main(String[] args) { Fruit fruit = new Fruit(); Class<?> class1 = fruit.getClass(); //方法一 Class<?> class2 = Fruit.class; //方法二 Class class3 = null; try { //方法三,若是这里不指定类所在的包名会报 ClassNotFoundException 异常 class3 = Class.forName("com.jas.reflect.Fruit"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(class1 + " " +class2 + " " + class3); } } class Fruit{}
经过反射机制建立对象,在建立对象以前要得到对象的构造函数对象,经过构造函数对象建立对应类的实例。ui
下面这段代码分别在运行期间建立了一个无参与有参的对象实例。因为getConstructor() 方法与newInstance() 方法抛出了不少异常(你能够经过源代码查看它们),这里就简写了直接抛出一个Exception,下同。this
package com.jas.reflect; import java.lang.reflect.Constructor; public class ReflectTest { public static void main(String[] args) throws Exception { Class clazz = null; clazz = Class.forName("com.jas.reflect.Fruit"); Constructor<Fruit> constructor1 = clazz.getConstructor(); Constructor<Fruit> constructor2 = clazz.getConstructor(String.class); Fruit fruit1 = constructor1.newInstance(); Fruit fruit2 = constructor2.newInstance("Apple"); } } class Fruit{ public Fruit(){ System.out.println("无参构造器Run..........."); } public Fruit(String type){ System.out.println("有参构造器Run..........." + type); } }
输出:
无参构造器Run………..
有参构造器Run………..Apple
经过反射机制获取Class 中的属性spa
package com.jas.reflect; import java.lang.reflect.Field; public class ReflectTest { public static void main(String[] args) throws Exception { Class<?> clazz = null; Field field = null; clazz = Class.forName("com.jas.reflect.Fruit"); //field = clazz.getField("num"); getField() 方法不能获取私有的属性 // field = clazz.getField("type"); 访问私有字段时会报 NoSuchFieldException异常 field = clazz.getDeclaredField("type"); //获取私有type 属性 field.setAccessible(true); //对私有字段的访问取消检查 Fruit fruit = (Fruit) clazz.newInstance(); //建立无参对象实例 field.set(fruit,"Apple"); //为无参对象实例属性赋值 Object type = field.get(fruit); //经过fruit 对象获取属性值 System.out.println(type); } } class Fruit{ public int num; private String type; public Fruit(){ System.out.println("无参构造器Run..........."); } public Fruit(String type){ System.out.println("有参构造器Run..........." + type); } }
输出:
无参构造器Run………..
Apple
经过反射机制获取Class 中的方法并运行。code
package com.jas.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class ReflectTest { public static void main(String[] args) throws Exception { Class clazz = null; Method method = null; clazz = Class.forName("com.jas.reflect.Fruit"); Constructor<Fruit> fruitConstructor = clazz.getConstructor(String.class); Fruit fruit = fruitConstructor.newInstance("Apple"); //建立有参对象实例 method = clazz.getMethod("show",null); //获取空参数show 方法 method.invoke(fruit,null); //执行无参方法 method = clazz.getMethod("show",int.class); //获取有参show 方法 method.invoke(fruit,20); //执行有参方法 } } class Fruit{ private String type; public Fruit(String type){ this.type = type; } public void show(){ System.out.println("Fruit type = " + type); } public void show(int num){ System.out.println("Fruit type = " + type + ".....Fruit num = " + num); } }
输出: Fruit type = Apple Fruit type = Apple…..Fruit num = 20
Class.forName() 生成的结果是在编译时不可知的,所以全部的方法特征签名信息都是在执行时被提取出来的。反射机制能过建立一个在编译期彻底未知的对象,并调用该对象的方法。xml
如下是反射机制与泛型的一个应用,经过一个工厂类建立不一样类型的实例。对象
要建立对象的实例类Apple :
package com.jas.reflect; public interface Fruit {} class Apple implements Fruit{}
加载的配置文件config.properties:
Fruit=com.jas.reflect.Apple
工厂类BasicFactory :
package com.jas.reflect; import java.io.FileReader; import java.util.Properties; public class BasicFactory { private BasicFactory(){} private static BasicFactory bf = new BasicFactory(); private static Properties pro = null; static{ pro = new Properties(); try{ //经过类加载器加载配置文件 pro.load(new FileReader(BasicFactory.class.getClassLoader(). getResource("config.properties").getPath())); }catch (Exception e) { e.printStackTrace(); } } public static BasicFactory getFactory(){ return bf; } //使用泛型得到通用的对象 public <T> T newInstance(Class<T> clazz){ String cName = clazz.getSimpleName(); //得到字节码对象的类名 String clmplName = pro.getProperty(cName); //根据字节码对象的类名经过配置文件得到类的全限定名 try{ return (T)Class.forName(clmplName).newInstance(); //根据类的全限定名建立实例对象 }catch (Exception e) { throw new RuntimeException(e); } } }
建立对象实例:
package com.jas.reflect; public class ReflectTest { public static void main(String[] args) throws Exception { Fruit fruit = BasicFactory.getFactory().newInstance(Fruit.class); System.out.println(fruit); } }
输出
com.jas.reflect.Apple@4554617c
上面这个实例经过一个工厂建立不一样对象的实例,经过这种方式能够下降代码的耦合度,代码获得了很大程度的扩展,之前要建立Apple 对象须要经过new 关键字建立Apple 对象,若是咱们也要建立Orange 对象呢?是否是也要经过new 关键字建立实例并向上转型为Fruit ,这样作是麻烦的。
如今咱们直接有一个工厂,你只要在配置文件中配置你要建立对象的信息,你就能够建立任何类型你想要的对象,是否是简单不少了呢?可见反射机制的价值是很惊人的。
Spring 中的 IOC 的底层实现原理就是反射机制,Spring 的容器会帮咱们建立实例,该容器中使用的方法就是反射,经过解析xml文件,获取到id属性和class属性里面的内容,利用反射原理建立配置文件里类的实例对象,存入到Spring的bean容器中。
参考书籍: 《Java 编程思想》 Bruce Eckel 著 陈昊鹏 译