Reflection Definition Simple Expression:
For any one (dynamic) running class, we can dissect it, get the constructor method in the class, the member variable, the member Method.
Loading of classes
(1) loading
This means reading the class file into memory and creating a class object for it.
The system creates a class object when any class is used
(2) Connection
Verify that you have the correct internal structure and that it is consistent with other classes
Prepare to allocate memory for static members of the class and set default initialization values
Resolve to replace a symbolic reference in a Class's binary data with a direct reference
(3) initialization
1. Creating an instance of a class
2. Static variables for a class, or assign a value to a static variable
3. Static Methods for classes
4. Use reflection to force the creation of a Java.lang.Class object for a class or interface (static code blocks are not loaded)
5. Initialize subclasses of a class
6. Use the Java.exe command directly to run a main class
How to get. ClassFile Object
1, gets the class object through the object class GetClass () method
2, gets the class object by the Name.
3, by way of reflection,
Class.forName (String Classname) gets the class object
public static class<?> forname (string ClassName) throws ClassNotFoundException returns the class object associated with the classes or interfaces with the given string name
gets the construction method in the class through reflection, and completes the creation of the object
Gets the specified constructor method
Public constructor<t> getconstructor (class<?> ... parametertypes)
Gets the construction method for the specified public adornment
Public constructor<t> getdeclaredconstructor (class<?> ... parametertypes)
Gets the specified constructor method that contains the private
Get all the construction methods
Public constructor<?>[] getconstructors () gets all the public adornments constructed by
Public constructor<?>[] getdeclaredconstructors () gets all the constructor methods, including the private
gets the construction method in the class through reflection, and completes the creation of the object
Steps:
1. Get the bytecode file object
2, through the bytecode file object, gets to the specified constructor method GetConstructor (parameter);
3, create an object by constructing the method
Public T newinstance (Object ... Initargs)
Private construction methods, creating objects
1. Get the bytecode file object
2, through the bytecode file object, gets to the specified constructor method Getdeclaredconstructor (parameter);
3, Violent Access con.setaccessible (true);
4, create an object by constructing the method
Public T newinstance (Object ... Initargs)
Get the method in the class file by reflection
Gets the specified method
Public Method getmethod (String name, class<?> .... parametertypes)
Gets the specified public method
Public Method getdeclaredmethod (String name, class<?> .... parametertypes)
Gets the specified arbitrary method that contains the private
Get all the methods
Public method[] GetMethods () Gets the methods of this class and all public decorations in the parent class
Ublic method[] getdeclaredmethods () gets all the methods in this class, including private
Invoking a method through reflection
Steps:
1, get the Class object
2, Get construction method, Create object
3, gets the specified public method
4, Execution method
Public object Invoke (object obj, object ... Args)
Calls to private methods:
1, get the Class object
2, Get construction method, Create object
3, gets the specified private method
4. Turn on violent access
M5.setaccessible (true);(m5: Gets the method Name)
5, Execution method
Public object Invoke (object obj, object ... Args)
Get member variables (fields) by reflection
Gets the specified member variable
Public Field GetField (String Name) Gets the public decorated member variable
Public Field Getdeclaredfield (String Name) gets any member variable that contains the private
Get all the member variables
Public field[] GetFields () gets all the public decorated member variables
Public field[] Getdeclaredfields () gets all the member variables of the division, including the private
L Get member variables by reflection, and assign values to use
Steps:
1. Get the bytecode file object
2, Get construction method, Create object
3, gets the specified member variable
4, assign value to member variable \ Get value operation
public void Set (object obj, object Value) Assignment
public object get (object Obj) Gets the value
/* Comprehensive case of Reflection:
* Through the reflection technique, get JavaBean object, inject (assign) value to javabean member variable */
public class Usereflectsetfileldvalue {
public Static void main (string[] Args) throws Exception {
Properties Pro =New Properties ();
Pro.load (new FileReader ("data.properties"));
4. Create a JavaBean object using reflection Technology
Class<?> clazz = Class. forname ("cn.itcast.demo02.javabean.User");
Object obj = clazz.newinstance ();
set<string> names = Pro.stringpropertynames ();
for (String s:names) {
Stitching Set Method
String methodname= "set" +s.substring (0, 1). touppercase () +s.substring (1);
7. Get the Setxxx method using reflection technology
Method Setmethod = Clazz.getmethod (methodName, String. Class);
Setmethod.invoke (obj, Pro.getproperty (s));
/*
* Extension: Stitching GetXXX method
*/
String getmethodname= "get" +s.substring (0, 1). touppercase () +s.substring (1);
Method GetMethod = Clazz.getmethod (getmethodname);
System. Out. println (getmethod.invoke (obj));
}
System. Out. println (obj);
}
public class User implements serializable{
/**
* Add serial number
*/
Private static final Long Serialversionuid = 8252072893140834728L;
Private String id;
Private String username;
Private String password;
Public User () {
Super ();
TODO auto-generated Constructor stub
}
@Override
Public String toString () {
return "User [id=" + ID + ", username=" + username + ", password=" + password + "]";
}
Public String getId () {
Return id;
}
public void SetId (String Id) {
This.id = id;
}
Public String GetUserName () {
Return username;
}
public void Setusername (String Username) {
This.username = username;
}
Public String GetPassword () {
Return password;
}
public void SetPassword (String Password) {
This.password = password;
}
}
The reflection of Java EE