Too many frameworks use Java reflection, highlighting the importance of Java reflection. Reflection is a very important advanced feature of Java. This chapter introduces Java reflection.

Java reflection mechanism is in the process of program running, for any class, can know all its properties and methods; These properties and methods are called dynamic invocation, and the ability to dynamically obtain information and dynamically call object methods is called Java’s reflection mechanism.

Java reflection mechanism main functions:

  • At run time == determine which class any object belongs to. = =
  • Construct an object of any class at run time ==. = =
  • Determine all member variables and methods of any class at run time. = =
  • Call methods on any object at run time ==. = =

An example shows Java reflection:

package com.lingyiwin.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestReflect {
    public static void main(String[] args) throws Exception {
        Class student = null;

        try {
            student = Class.forName("com.lingyiwin.test.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("Get all common properties of student");
        Field[] fields = student.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("\n gets all properties of student, excluding inherited properties");
        Field[] declaredFields = student.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        System.out.println("\n get all public methods for student");
        Method[] methods = student.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println("\n gets all public methods from student, not including inherited properties");
        Method[] declaredMethods = student.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }

        System.out.println("\n gets all public constructors for student");
        Constructor[] declaredConstructors = student.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }

        System.out.println("\n Instantiates student.newinstance () with the default constructor. It must have a default constructor that takes no arguments. Otherwise, an error will be reported.");
        Student instance = (Student) student.newInstance();
        instance.setAddress("Shanghai");
        System.out.println(instance.toString());

        System.out.println("\n instantiated with constructor Student(String className)");
        Constructor constructor = student.getConstructor(String.class);
        Student st = (Student) constructor.newInstance("Class name");
        System.out.println(st.toString());

        System.out.println("\n instantiate Student(String name, int age, String className, String address)");
        Constructor ct = student.getConstructor(String.class, int.class, String.class, String.class);
        Student st1 = (Student) ct.newInstance("Xiao Ming".18."Class name"."Shanghai");
        System.out.println(st1.toString());

        System.out.println("\n takes a method with no arguments and executes :");
        Method learn = student.getMethod("learn");
        learn.invoke(st1);

        System.out.println("\n takes a method with arguments and executes :");
        Method readBook = student.getMethod("readBook",String.class);
        String str = (String) readBook.invoke(st1,"Chinese");
        System.out.println(Method return value:+ str); }}Copy the code
package com.lingyiwin.test;

public class Student extends Person implements Study {
    public String className;
    private String address;

    public String getClassName(a) {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getAddress(a) {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Student(String name, int age, String className, String address) {
        super(name, age);
        this.className = className;
        this.address = address;
    }

    public Student(a) {}public Student(String className) {
        this.className = className;
    }

    @Override
    public String toString(a) {
        return "Student{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                ", className='" + className + '\' ' +
                ", address='" + address + '\' ' +
                '} ';
    }

    @Override
    public void learn(a) {
        System.out.println("Implement the Study interface");
    }

    public String readBook(String book) {
        System.out.println("Students read.");
        return this.name + "Read"+ book; }}Copy the code
package com.lingyiwin.test;

public class Person {
    public String name;
    public int age;

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(a) {
        super(a); }public Person(String name, int age) {
        super(a);this.name = name;
        this.age = age;
    }

    public String showInfo(a) {
        return "name:" + name + ",age:"+ age; }}Copy the code
package com.lingyiwin.test;

public interface Study {
    public void learn(a);
}

Copy the code

For many methods, please also refer to the API, only a few of which are commonly used.