1, what is the reflection mechanism?
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any object can call his properties and methods, this dynamic acquisition of properties and methods of the function is called the Java Reflection mechanism.
That is, a Java program can load a runtime to know the name of class, learn the complete construction of the class (but not including the methods definition), and generate its object entities, or set values for its fields, or evoke its methods. In short, the JVM can load, detect, and use classes that are completely unknown at runtime.
2. The reflection API provided by the JDK
Java reflection-related APIs in package Java.lang.reflect
Member interface |
The interface can get information about the class member (domain or method) of the latter constructor. |
AccessibleObject class |
The class is the base class for the Domain (field) object, method object, constructor (constructor) object. It provides the ability to mark reflected objects as being used to cancel the default Java language access control check. |
Array class |
This class provides a way to dynamically generate and access a Java array. |
Constructor class |
Provides information about the constructors of a class and the interfaces that access the class's constructors. |
Field class |
Provides information about the domain of a class and the interfaces that access the class's domain. |
Method class |
Provides information about the methods of a class and the interfaces that access the methods of the class. |
Modifier class |
Static methods and constants are provided to decode the class and member access modifiers. |
proxy class |
Provides static methods for dynamically generating proxy classes and class instances. |
3. The function of reflection mechanism
Get member variables and methods for any class at run time
To construct an object of any class at run time
Determine the class to which any object belongs at run time
Methods to invoke any object at run time
Generating dynamic agents
4, where to use the reflection mechanism
When we first learned JDBC, we started with a line of code class.forname (' com. MySQL.jdbc.Driver.class '). newinstance (); it was just a perceptual knowledge used to instantiate a drive object. Many of the frameworks we use in our development use reflection, such as spring, hibernate, etc. in Web. XML, as well as hints for the tools we use in our development.
5. Code implementation
3.1 Getting the class object
Public classClasscreate { Public Static voidMain (string[] args) {Try{Class C1= Class.forName ("java.lang.String"); String s=NewString ("string"); Class C2=S.getclass (); Class C3= String.class; SYSTEM.OUT.PRINTLN (C1+ "|" + C2 + "|" +C3); SYSTEM.OUT.PRINTLN (C1==C2); SYSTEM.OUT.PRINTLN (C1==C3); } Catch(ClassNotFoundException e) {e.printstacktrace (); } }}
It can be seen from the above that there are 3 methods to get the class object, and that the same class has a unique classes object in memory (assuming that the uniform classloader is used).
3.2. Get the construction of the class
The Class<t> class provides several ways to get the constructor of a class
Public constructor<t> getconstructor (class<?> ... parametertypes) |
Returns a Constructor object that reflects the specified public construction method for the class represented by this class object |
Public constructor<?>[] GetConstructors () |
Returns an array containing some Constructor objects that reflect all public constructors of the class represented by this class object |
Public constructor<t> getdeclaredconstructor (class<?> ... parametertypes) |
Returns a Constructor object that reflects the specified construction method for the class or interface represented by this class object |
Public constructor<?>[] Getdeclaredconstructors () |
Returns an array of Constructor objects that reflect all of the constructor methods for the class declaration represented by this class object. They are public, protected, default (package) access, and private construction methods |
Public classObtainconstructor { Public Static voidMain (string[] args)throwsException {Class clazz= Class.forName ("Com.classTest.classload.OC"); constructor[] CT=clazz.getconstructors (); for(Constructor c:ct) System.out.println (C.getname () + "|" +c.getparametertypes (). length); }}classOC {PrivateString name; PublicOC () {} PublicOC (String name) { This. Name =name; }}
3.3. Methods for obtaining classes
The Class<t> class provides several ways to get a class.
p Ublic methodgetmethod (STRING&NBSP;NAME,CLASS<?> parametertypes) |
The Returns a method object that reflects the specified public member methods of the class or interface represented by this class object |
public method[] GetMethods () |
Returns a An array containing some method objects that reflect the public member method of the class or interface represented by this class object, including those declared by the class or interface, and those inherited from the superclass and the hyper-interface, |
Public methodgetdeclaredmethod (stringname,class<?> .... parameterTypes) |
Returns a method object that reflects the specified declared method of the class or interface represented by this class object |
public method[] getdeclaredmethods () |
return Method An array of objects that reflect all methods of the class or interface declaration represented by this class object, including public, protected, default (package) access, and private methods, but do not include inherited methods |
Public classObtainmethod { Public Static voidMain (string[] args)throwsException {Class clazz= Class.forName ("Com.classTest.classload.OM"); Method[] MT=Clazz.getmethods (); for(Method m:mt) System.out.println (M.getname ()+ "|" + m.getreturntype () + "|" +m.getparametertypes (). length); }}classOM { Public voidgetList () {} Public voidgetList (String keyword) {}}
3.4. Get the member variables of the class
The JAVA Class<t> class provides several ways to get the properties of a class.
Public Fieldgetfield (String name) |
Returns a Field object that reflects the specified public member field of the class or interface represented by this class object |
Public field[] GetFields () |
Returns an array containing some field objects that reflect all accessible public fields of the class or interface represented by this class object |
Public Fieldgetdeclaredfield (Stringname) |
Returns a Field object that reflects the specified declared field of the class or interface represented by this class object |
Public field[] Getdeclaredfields () |
Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object |
Public classObtainfield { Public Static voidMain (string[] args)throwsException {Class clazz= Class.forName ("Com.classTest.classload.OF"); Field[] FD=Clazz.getfields (); for(Field f:fd) System.out.println (F.getname () + "|" +F.gettype ()); }}classof { PublicString name; Public intAge ; PrivateString address;}
6. Advantages and disadvantages of reflection mechanism
The advantage of the reflection mechanism is that it enables dynamic compilation and creation of objects. For example, a software update, the use of static words, the entire program needs to be recompiled once to achieve the functionality of the update, and the use of reflection mechanism, it can not uninstall, just to run the dynamic creation and compilation, you can achieve this function.
The drawback of the reflection mechanism is that reflection is essentially an interpretation operation that has an effect on performance.
Java Basics (11) Java Reflection mechanism (bottom)