This is the 8th day of my participation in Gwen Challenge

This reflection is actually a primary to intermediate interview questions, but also the primary must understand, intermediate must understand and skilled use of knowledge skills.

Let’s start the conversation.

What is reflection?

Reflection means that in the running state of the program, for any class, it can obtain the attributes and methods of the class in a specific way, and can call these attributes and methods.

In plain English, reflection is the ability to get and execute a class property or method while the program is running.

What exactly does reflection do?

The things that can be done are mainly divided into the following categories.

By default, we write a class: app.java;

public class APP {

    private Integer id;

    private String username;
    
    public String password;

    public Integer getId(a) {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername(a) {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public String toString(a){
        System.out.print("toString");
    }
    
    private String toString_2(a){
        System.out.print("toString_2"); }}Copy the code

For class

Class.forname (param) method

Param: Specifies the full path of the class, such as com.test.APP.

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        Class aClass = Class.forName("com.test.APP");

        APP app = (APP) aClass.newInstance();

    }
Copy the code

NewInstance () instantiates the Class to generate an object, which is the same as new.

Calling a public property (public)

GetField (param) : gets the specified public attribute, param: specifies the name of the attribute.

GetFields () : Gets all the public attributes, returning an array of values Field[].

The code is as follows:

    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("com.test.APP");
// APP app = (APP) aClass.newInstance();

            Field password = aClass.getField("password");
            
            Public java.lang.string com.test.app.password
            System.out.println(password);

            Field[] fields = aClass.getFields();
            
        }catch(Exception e){ e.printStackTrace(); }}Copy the code

Calling private properties

GetDeclaredField (param) : getDeclaredField(param) : getDeclaredField(param) : getDeclaredField(param) : private property name.

GetDeclaredFields () : Gets all private attributes.

The code is as follows:

    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("com.test.APP");

            Field username = aClass.getDeclaredField("username");
            // Enforce access to private variables
            username.setAccessible(true);
            Public java.lang.string com.test.app.username
            System.out.println(username);

            Field[] fields = aClass.getDeclaredFields();

        }catch(Exception e){ e.printStackTrace(); }}Copy the code

There is also a method involved, setAccessible(True), which is used to gain access that would otherwise not be available.

Calling a public method (public)

GetMethod (param) : gets the specified public method, param: specifies the name of the method

GetMethods () : Gets all public methods


    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("com.test.APP");

            Method method = aClass.getMethod("toString");

            Method[] methods = aClass.getMethods();

            // Executes the toString() method
            Object app = aClass.newInstance();
            method.invoke(app);

        }catch(Exception e){ e.printStackTrace(); }}Copy the code

Here is another knowledge, is how to execute the Method, in the code is also reflected, you can try it yourself.

Calling private methods

GetDeclaredMethod (param) : obtains the specified private method. Param: specifies the name of the specified private method.

GetDeclaredMethods () : Gets all private methods.

    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("com.test.APP");

            Method method = aClass.getDeclaredMethod("toString_2");
            method.setAccessible(true);

            Method[] methods = aClass.getDeclaredMethods();

            // Executes the toString() method
            Object app = aClass.newInstance();
            method.invoke(app);

        }catch(Exception e){ e.printStackTrace(); }}Copy the code

Today, a junior high school teacher who had never taught me added me to wechat to inquire about my industry. Between the lines, he revealed that he wanted to find a job for his son through the back door. He didn’t know how to ask, and he worried about his future.