Java reflection mechanism principle and java reflection mechanism Principle
Concepts of a reflection mechanism:
All attributes and methods of a class can be known for any class in the running state, and any method of the class can be called for any object. this kind of dynamic information retrieval and the function of dynamically calling object methods is called the reflection mechanism of java language.
Application of the binary reflection mechanism:
Generate a dynamic proxy for slicing programming (stack frames are added before and after the method is called ).
Principles of the Three Reflection mechanisms:
1. The first clear concept: Everything is an object-the class is also an object.
2 and then know the content in the class: modifier constructor field method.
3. Understand loading: When Animal. class is in the hard disk, It is a file. When it is loaded into the memory, it can be considered as an object and a java. lang. class Object.
When a program is running, Animal. class objects are generated during instantiation. What are the differences between the two objects?
For example, the above Animal. class Object is the object of java. lang. class; the following object is the object generated by Animal. class instantiation.
4 when Animal. class is loaded into JVM, how does JVM identify methods, member variables, constructor methods, and so on contained in Animal. class?
For example, the member variable color in Animal. class instantiation object is red;
When it is loaded to the memory, the gross color = null. The gross color is assigned red when it is instantiated.
Animal. the margin property in class is for java. lang. class is an attribute value of Field. lang. class is also an object. this is also the reason why the reflection mechanism dynamically obtains information and calls object methods.
Is a method in Java. lang. class.
In java. lang. in class, the gross color is an attribute value of the Field. All methods, member variables, constructors, and modifiers of Animal follow java. lang. standard in the class. Otherwise, an error will be reported during compilation.
Simple sample code of the Four-radiation Mechanism
Public class _ 14_ReflectTest07
{
Public static void main (String [] args) throws Exception {
// 1. Obtain the class
Class c = Class. forName ("_ 12_CustomerService ");
// Obtain a specific method
// Pass: method name + parameter list
Method m = c. getDeclaredMethod ("login", String. class, String. class );
// Execute the login method through the reflection mechanism.
Object o = c. newInstance ();
// Call the m method of the o object and pass the "admin" "123" parameter. The execution result of the method is retValue.
Object retValue = m. invoke (o, "admin", "123 ");
System. out. println (retValue); // true
}
}
Public class _ 12_CustomerService
{
// Log on
Public boolean login (String name, String pwd ){
If ("admin". equals (name) and & "123". equals (pwd )){
Return true;
}
Return false;
}
// Exit
Public void logout (){
System. out. println ("the System has been safely exited! ");
}
}