A brief summary of JAVA reflection mechanism

Posted by Raphael diSanto on Sun, 12 Sep 2021 07:21:04 +0200

1. What is reflection

        Java reflection mechanism is a mechanism to dynamically obtain and call the properties and methods of any class when the program is running

         Various information obtained through reflection will be mapped into individual classes in java, such as the class corresponding to attribute (Field), the class corresponding to Method (Method), the class corresponding to construction (Constructo), etc. through these classes, we can operate the data obtained in reflection

2. Why reflection

        Java is a language that compiles first and then runs. The objects in the program are fixed during compilation, but some classes may need to be loaded dynamically when the program runs, and these classes are not used before compilation, so they can't be loaded into the JVM. At this time, we can create objects dynamically at run time through reflection

         Increase the flexibility of the program, avoid dead writing and decoupling

         For example, there are classes A and B. the object instances required by A certain program are uncertain. Sometimes A and sometimes B are required. It is impossible for us to modify the code every time. At this time, we can dynamically create different instances according to the needs through reflection

3. How to use reflection

The call to reflection is realized through the methods in Class. Therefore, we must first obtain the Class type object corresponding to each bytecode file

What is Class?

Our. Class file is loaded into memory through the class loading mechanism. In memory, this bytecode will be described as a class object

There are three ways to get Class objects

It should be noted here that each Class will only have one Class, and repeated creation will only obtain the same Class. After obtaining the Class, you can get all true

//Method 1: Class.forName("absolute path");
Class class1 = Class.forName("com.zc.test");   //test is the class to get, custom
System.out.println("class1 = " + class1);

//Method 2: class name.class
Class class2 = test.class;
System.out.println("class2 = " + class2);

//Method 3: object. getClass();
test test = new test();
Class class3 = test.getClass();
System.out.println("class3 = " + class3);

//Compare three objects
System.out.println(class1 == class2);//Return true
System.out.println(class1 == class3);//Return true

The first method, Class.forName("xx.xx.xx"), is recommended

The second is to import the package of the class to be obtained, which is highly dependent

The third kind is new, and the reflection is slightly redundant

Operate through Class (just some common and simple)

public static void main(String[] args) throws Exception {
        text text=new text();
        Class class1 = text.getClass();

//Operations on variables
        //Get all member variables
        Field[] fields = class1.getFields();
        //Gets the member variable with the specified name
        Field name = class1.getField("Attribute name");
        //Gets the value of the member variable name
        Object value = name.get(text);
        //Modify the value of ame
        name.set(text, "Zhang San");
        System.out.println(text.name);//At this point, the name is changed to Zhang San




//Operations on constructs
        //Get parameterless construction
        Constructor con = class1.getConstructor();
        //Create an instance object by parameterless construction
        Object p1 = con.newInstance();
        //Get parameters
        Object p2 = con.newInstance("xxx","xxx");
        //Get all construction methods
        Constructor[] con2 = class1.getConstructors();



//Operations on Methods
        //Get parameterless method
        Method method = class1.getMethod("Method name");
        //Execute the obtained parameterless method
        method.invoke(text);

        //Get parameterized method
        Method method1 = class1.getMethod("Method name", String.class);//The second parameter is the data type. class of the method parameter
        //Execute the obtained parameterized method
        method1.invoke(text, "Zhang San"); //The second parameter is the parameter to be used by the called method

        //Get all methods of public decoration
        Method[] methods = class1.getMethods();
        for (Method methods1 : methods) {
            //Get method name
            System.out.println(method.getName());
        }

    }

#Summarize and write by yourself, understand mistakes, prompt for modification, and don't spray if you don't like it

Topics: Java reflection