Java reflection usage

first step:

Before learning the Java reflection mechanism, you should learn the class loading mechanism and loading process. Only in this way can we understand and grasp the origin of the Class object more clearly. The Class object is created by the JVM after loading the Class file. In the heap, there is a reference to the class information loaded into the method area in the Class object, so the Class object can clearly spy on the entire class. structure.

The second step is to clarify the idea of ​​using reflection

Class-related information includes parent class information, constructors, field information, and methods. Therefore, when we perform reflection, we should mainly start from understanding these kinds of information of the class. In order to operate the information of this class, if I have learned the class loading mechanism, I naturally think of how to get the Class object.

There are three ways to get the Class object, choose according to the specific situation;

 

/First way:  
Classc1 = Class.forName("Employee");  
//Second way:  
//Every type in java has a class attribute.  
Classc2 = Employee.class;  
   
//The third way:  
//Any java object in the java language has a getClass method  
Employeee = new Employee();  
Classc3 = e.getClass(); //c3 is the runtime class (e's runtime class is Employee)

After getting the Class object, I can easily spy on the specific internal structure information of the class.

onstructor getConstructor(Class[] params) returns a concrete constructor with public properties according to the parameters of the constructor

Constructor getConstructors() returns an array of all constructors with public properties

Constructor getDeclaredConstructor(Class[] params) According to the parameters of the constructor, return a specific constructor (regardless of public and non-public properties)

Constructor getDeclaredConstructors() returns an array of all constructors in the class (regardless of public and non-public properties)

First of all, our use of reflection is nothing more than want to use the object of the class to instantiate the class. It can be instantiated in two ways, with and without parameters

Class c =Class.forName("Employee");  
  
//Create a new instance of the class represented by this Class object  
Objecto = c.newInstance(); //The parameterless constructor of Employee is called, there must be a parameterless constructor
Class clazz = InterfaceImple.class;
Constructor constructor =clazz.getConstructor(String.class,int.class);
Object obj = constructor.newInstance("ganyao",10);

Then there is the operation member variable

Field getField(String name)  根据变量名,返回一个具体的具有public属性的成员变量

Field[] getFields()  返回具有public属性的成员变量的数组

Field getDeclaredField(String name) 根据变量名,返回一个成员变量(不分public和非public属性)

Field[] getDelcaredFields() 返回所有成员变量组成的数组(不分public和非public属性)
For the acquisition of field information, you can get an array of all fields or get a specified field, and you can also get public and non-public fields

In general, it is used to get the value of the specified field.

Field filed =clazz.getDeclaredField("str"); //Get the specified field object even if the field is privately modified
filed.setAccessible(true); For fields modified by Private, you can operate by setting TRUE
filed.set(obj,"hello");
logger.info((String) filed.get(obj));

Finally, the operation member method

Method getMethod(String name, Class[] params) Returns a specific method with public properties according to the method name and parameters

Method[] getMethods() returns an array of all methods with public properties

Method getDeclaredMethod(String name, Class[] params) Returns a specific method according to the method name and parameters (regardless of public and non-public attributes)

Method[] getDeclaredMethods() Returns an array of all methods in the class (regardless of public and non-public properties)

Method method =  clazz.getMethod("getStr");
	    logger.info((String) method.invoke(obj));





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894232&siteId=291194637