First, what is the reflection mechanism of Java
Java reflection is a key property of Java as a dynamic language. This mechanism allows the program to obtain the internal information of any known name class, including its modifiers (such as public,static, etc.), superclass (for example, object), through the reflection APIs at runtime. The interfaces of implementation (for example, cloneable) also includes all the information of fields and methods, and can change the fields content or evoke methods at run time.
The Java reflection mechanism allows programs to load, detect, and use classes that are completely unknown during compilation at run time.
In other words, Java can load a runtime to know the name of class and get its full structure.
Ii. reflection API provided in JDK
Java reflection mechanism-related API in package Java.lang.reflect, refer to JDK1.6.0 's package for more information about it
| Member interface |
The interface can get information about a class member (domain or method) or 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 methods for dynamically generating and accessing Java arrays |
| Constructor class |
Provides information about the constructors of a class and interfaces that access the class's constructors |
| Field class |
Provides information about the domain of a class and the interface of the domain that accesses the class |
| 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 |
Iii. What functionality is provided by the reflection mechanism of Java
Determine the class to which any object belongs at run time
To construct an object of any class at run time
Determine the member variables and methods that any one class has at run time
Methods to invoke any object at run time
To create a new class object at run time
When using the Java Reflection feature, it is essential to get the class object first, and then get the other objects through the class object
1. Get class objects for classes
Instances of class classes represent classes and interfaces in a running Java application.
There are several ways to get class objects for classes:
| Call GetClass |
Boolean var1 = true;
class<?> classType2 = Var1.getclass ();
System.out.println (CLASSTYPE2);
Output: Class Java.lang.Boolean |
| Use the. Class syntax |
class<?> classType4 = Boolean.class;
System.out.println (CLASSTYPE4);
Output: Class Java.lang.Boolean |
| Using the static method Class.forName () |
class<?> ClassType5 = Class.forName ("Java.lang.Boolean");
System.out.println (CLASSTYPE5);
Output: Class Java.lang.Boolean |
|
Using the type syntax of primitive wrapper classes
This returns the native type, and the Boolean.class returns the different |
class<?> classType3 = Boolean.type;
System.out.println (CLASSTYPE3);
Output: Boolean |
2. Get the fields of the class
A property of a class can be obtained through a reflection mechanism, and then the property value corresponding to an instance of the class is changed.
The Java Class<t> class provides several ways to get the properties of a class.
| Public Field GetField (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 Field Getdeclaredfield (String name) |
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 |
3. Get the method of the class
A method of a class is obtained through a reflection mechanism, and then the method that corresponds to an instance of the class is called
Class<t> provides several ways to get classes
| Public Method GetMethod (String name,class<?>...parametertypes) |
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 an array that contains some method objects that reflect the public member method of the class or interface represented by this class object, including those declared by it or by those classes or interfaces that inherit from the superclass and the hyper-interface. |
| Public Method Getdeclaredmethod (String name,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 () |
Returns an array of method 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 not inherited methods |
4. Get the constructor of a class
A constructor for a class is obtained through the reflection mechanism, and then the constructor is called to create an instance of the class
The Class<t> class provides a pass method to get the constructor of the 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 |
5. Instances of the new class
Creating an instance of a new class through the reflection mechanism, there are several ways to create
| call no arguments ctor |
1, call class object n The Ewinstance method invokes the default constructor of the object, and if there is no default constructor, the call fails
class<?> ClassType = Extendtype.class;
Object Inst = classtype.newinstance ();
System.out.println (inst);
Output:
Type:default Constructor
Extendtype:default Constructor
[email protected]
2. Newinstance method that calls the default constructor object
Class<?> ClassType = Extendtype.class;
Constructor<?> Constructor1 = Classtype.getconstructor ();
Object Inst = constructor1.newinstance ();
System.out.println (inst); |
|
3, call constructor with parameter Newin object Stance method
Constructor<?> Constructor2 = Classtype.getdeclaredconstructor (int.class,sting.class);
Object Inst = constructor2.newinstance (1, "123"); |
Iv. functions that call classes
V. Set/Get property values for a class
Vi. dynamic creation of proxy classes
Vii. Java Reflection class<t> type source code Analysis
Eight, Java Reflection principle analysis
IX. Application of Java Reflection
The reflection mechanism of Java