反射,reflection,听其名就像照镜子一样,可以看见自己也可以看见别人的每一部分。在java语言中这是一个很重要的特性。下面是来自sun公司官网关于反射的介绍:
Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.
The ability to examine and manipulate a Java class from within itself may not sound like very much, but in other programming languages this feature simply doesn't exist. For example, there is no way in a Pascal, C, or C++ program to obtain information about the functions defined within that program.
One tangible use of reflection is in JavaBeans, where software components can be manipulated visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically loaded.
反射的前传:类类型 Class Class
注释很明确的告诉了我们,这个类是有JVM来创建的,所以我们就不用麻烦了。如果我们拿到一个类的类型信息,就可以利用反射获取其各种成员以及方法了。(注:Class 从JDK1.5版本后就开始更多为泛型服务了)那么我们怎么拿到一个类型的信息呢?假设我们有一个Role类:
package yui; /** * A base class having some attributes and methods * @author Octobershiner * @since 2012 3 17 * * */ public class Role { private String name; private String type; // Constructors public Role(){ System.out.println("Constructor Role() is invoking"); } //私有构造器 private Role(String name){ this.name = name; System.out.println("Constructor Role(String name) is invoking."); } //get and set method public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } //override the toString method to show the class @Override public String toString(){ return "This is a role called "+this.name; } }
//获得类类型的两种方式
Class cls1 = Role.class;
Class cls2 = Class.forName("yui.Role");
注意第二种方式中,forName中的参数一定是完整的类名(包名+类名),并且这个方法需要捕获异常。现在得到cls1就可以创建一个Role类的实例了,利用Class的newInstance方法相当于调用类的默认的构造器
Object o = cls1.newInstance(); //创建一个实例
//Object o1 = new Role(); //与上面的方法等价
这样就创建了一个对象,缺点是我们只能利用默认构造函数,因为Class的newInstance是不接受参数的,后面会讲到可接受参数的newInstance,第二,如果类的构造函数是private的,比如Class,我们仍旧不能实例化其对象。
获取类的构造器
首先介绍一下Constructor类,这个类用来封装反射得到的构造器,Class有四个方法来获得Constructor对象
/** * 获取构造方法Constructor * getConstructor() only for public * getDeclaredConstructor() global access all * * */ //指定参数列表获取特定的方法 Constructor con = cls1.getDeclaredConstructor(new Class[]{String.class}); con.setAccessible(true); //设置可访问的权限 Object obj = con.newInstance(new Object[]{"liyang"}); System.out.println(obj); //打印一下这个对象的信息 //获取所有的构造方法集合 Constructor con1[] = cls1.getDeclaredConstructors(); con1[1].setAccessible(true); Object obj1 = con1[1].newInstance(new Object[]{"tom"}); System.out.println(obj1);
/** * 获取成员变量Field * getField() * getDeclaredField() * */ Field mem = cls1.getDeclaredField("name"); mem.setAccessible(true); System.out.println("we get form field :"+mem.get(obj));
/** * 调用类的方法 Method * getMethod() * getDeclaredMethod() * * */ Method f = cls1.getMethod("getName", null); Object name = f.invoke(obj, null); System.out.println("we invoke method : "+ name);
本文中的完整源代码地址:https://github.com/octobershiner/Java-Taste/tree/master/Reflection
转载地址:http://www.cnblogs.com/octobershiner/archive/2012/03/18/2404751.html