The soul of the frame -- Reflection

Keywords: Java Back-end

preface

I've been blogging for some time. Most of the previous articles are about what to use in work and what to write. They have no purpose. Recently, I've been thinking about filling in the pits left during learning, such as java reflection and dynamic agent. I only remember when I was learning. The teacher said that this was the key point and reflection was the soul of the framework. Only after learning this can I better understand the framework of learning later. At that time, I only felt that I learned a little knowledge. In the future, whether it is work or study, the opportunity to face it directly is still relatively low. Now, in turn, I want to consolidate the foundation to facilitate the subsequent understanding of the source code.

Mechanism of reflection

In short, it is to encapsulate all parts of a class into other objects, which is the radiation mechanism. Let's use the following diagram for a simple understanding:

The following three parts represent the three stages of the java code we write in the computer. In the first part, both the java code we write and the class file after that exist in our hard disk. It cannot run and must be loaded into memory to run. At this time, through the class loader, the java corresponds to the classloader object, Load the class file into memory.

We all know that everything in Java can be an object. Taking this step alone can be understood as converting the Student object into a Class object. It can also be said that the Class file is used to describe the loaded object. All the information of Student is stored in this Class object. And how does this information exist? In Figure 2, we can see that there are three arrays, representing member variables, construction methods, and member methods. Yes, all attributes should not be encapsulated into such three arrays, or three objects. This is why we can "program for". In IDEA  , When we create a new object, the IDEA loads the Class, and the IDEA is always running, so it can provide us with convenient tips. This is the third part of the code, the run phase. It is also its function -- you can get the parameters of the object at run time!

  Three ways to get Class objects

Method 1: class is obtained through class objects

Student student = new Student();
Class<? extends Student> aClass = student.getClass();

*Method 2: directly adjust class through class

Class<Student> aClass = Student.class;

*Method 3: pass Class.forName();         

Class<?> aClass = Class.forName("com.xxx.xxx.xxx.Student");

The third method is generally recommended to reduce the coupling

Get construction method through Class

//Get all construction methods (including private, protected, default and public)
 public Constructor[] getConstructors();


//Gets a single "public" constructor
public Constructor getConstructor(Class... parameterTypes);


//Gets whether a constructor can be private, protected, default, or public
Constructor getDeclaredConstructor(Class... parameterTypes);

Get member variables through Class

//Get all public fields
Field[] getFields();


//Get all fields, including private, protected, default and public
Field[] getDeclaredFields();


//Get a "public" field
public Field getField(String fieldName);


//Get a field (which can be private)
public Field getDeclaredField(String fieldName);

//Set the value of the field
public void set(Object obj,Object value);

Get member method through Class

//Get all public methods
Method[] getMethods();


//Get all methods, including private, protected, default and public
Field[] getDeclaredMethods();


//Get a "public" field, and var2 represents the formal parameter type
Method getMethod(String var1, Class<?>... var2);


//Get a field (which can be private), and var2 represents the formal parameter type
public Method getDeclaredMethod(String var1, Class<?>... var2);


//Operation method
//The previous operation only obtains the Method, not the real call. If you want to run the Method, you should also call the Method in the Method
//var1 is the object of the class and var2 is the formal parameter
public Object invoke(Object var1, Object... var2);

//In addition to using Class to create objects, newInstance is the method of Constructor Class, which can be understood as using this method to create objects instead of the Constructor of the method explained by Class class
public T newInstance();

    
    

Posted by daboymac on Sun, 05 Dec 2021 08:32:50 -0800