The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.
In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods.
Features provided by the Java reflection mechanism
Determine the class to which any object belongs at run time
To construct an object of any class at run time
At run time, the member variables and methods that any one class has
Methods that invoke either object at run time
To create a new class object at run time
When using the reflection function of Java, it is essential to get the class object first, and then get the other objects through the class object.
Recommendations for use in reflection mechanism blog: http://blog.csdn.net/xiaoxian8023/article/details/9206055
Java的反射机制的实现要借助于4个类:class,Constructor,Field,Method;
1Class C = class.forname ("Triangle");//Get Class object2 //get the constructor parameters for the corresponding parameters3Constructor con = c.getconstructor (Long.class,Long.class,Long.class);4 //create an object from the appropriate constructor5Object obj = con.newinstance (testcase[0],testcase[1],testcase[2]);6 //methods for obtaining the corresponding parameters7Method m = C.getdeclaredmethod ("GetType", c);
M.invoke (Obj,obj);
On the difference between Getdeclaredmethod and GetMethod of acquiring methods
Getdeclaredmethod can acquire private methods and methods of protection
GetMethod can only get public publicly owned methods, such as to access private methods, you need to set the visibility setaccessible (), set to True is the same as Getdeclaredmethod function.
However, from the development requirements of the class, it has always been emphasized that the attributes within the class must be encapsulated, so now call to find a way to de-encapsulate the setaccessible ()
Packagecn.mldn.demo;importjava.lang.reflect.field;classperson {privatestring name;} Publicclasstestdemo {publicstaticvoidmain (string[] args) throwsexception {Class<?> cls = Class.forName ("Cn.mldn.demo.Person");//get the Class objectObject obj = cls.newinstance ();//Object Instantiation property will not allocate spaceField NameField = Cls.getdeclaredfield ("name");//Locate the Name propertyNamefield.setaccessible (true) ;//release the package.Namefield.set (obj, "Zhang San");//the Person object. Name = "Zhang San"System.out.println (Namefield.get (obj));//The person object. Name}}
Related discussions visible http://www.zhihu.com/question/24304289
Packagetest.reflection;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method; Public classPerson {PrivateString name;//Private Properties PublicString address; PublicPerson (String name) { This. name=name; } Private voidSetaddress (String address) {//Private Methods This. address=address; } Public Static voidMain (string[] args) {Try{ person person=NewPerson ("Zhangsan"); Class<Person> Cls=person.class; //Get Private methodsMethod Method=cls.getdeclaredmethod ("Setaddress",NewClass[]{string.class}); //setting private methods can be accessedMethod.setaccessible (true); //Calling Private MethodsMethod.invoke (Person,Newobject[]{"BJUT"}); System.out.println (person.address);//Output Bjut//Accessing private variablesField Field=cls.getdeclaredfield ("name"); Field.setaccessible (true); System.out.println (Field.get (person));//Output Zhangsan }Catch(Exception ex) {ex.printstacktrace (); } }}
This reflection mechanism is often used when a framework is developed, such as a simple factory or an abstract factory, in order to reduce coupling, we need to use the object created with new as reflection, so that the entire project can be managed through a configuration file, without the need to modify the code.
The reflection of learning in Java