Reflection call method, instantiation object, field assignment

Keywords: Java

Prepare a class with parameters to construct methods, fields, and methods that are all private

public class Car {
    private String name;
    private Integer age;

    private Car(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    private void say() {
        System.out.println("I'm a cat");
    }

    @Override
    public String toString() {
         System.out.println("Car{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}');
        return "Car{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

Get private constructor by reflection

    @Test
    public void reflex1() throws Exception {
        //Get bytecode file
        Class<Car> clazz = Car.class;
        //Get the first constructor with String type and the second constructor with Integer type
        Constructor<Car> constructor = clazz.getDeclaredConstructor(String.class, Integer.class);
        //Cancel Java language access check when using
        constructor.setAccessible(true);
        //Biography
        Car car = constructor.newInstance("Cafe cat", 10);
        System.out.println(car);
    }

Console printing:
Car{name = 'coffee cat', age=10}

Get the fields of the class and assign values through reflection

    @Test
    public void reflex2() throws Exception {
        //Get bytecode file
        Class<Car> clazz = Car.class;
        //Get the first constructor with String type and the second constructor with Integer type
        Constructor<Car> constructor = clazz.getDeclaredConstructor();
        //Cancel Java language access check when using
        constructor.setAccessible(true);
        //Instanced object
        Car car = constructor.newInstance();
        //Get all field arrays
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //Print field name and type
            System.out.println("Field name=" + field.getName() + "Field type=" + field.getType());
            field.setAccessible(true);
            if (field.getType().equals(String.class)) {
                field.set(car, "Cafe cat");
            }
            if (field.getType().equals(Integer.class)) {
                field.set(car, 11);
            }
        }
        System.out.println(car);
    }

Console printing:
Field name = name field type = class java.lang.String
Field name = age field type = class java.lang.Integer
Car{name = 'coffee cat', age=11}

Get the method used by reflection, and call

    @Test
    public void reflex3() throws Exception {
        //Get bytecode file
        Class<Car> clazz = Car.class;
        //Get the first constructor with String type and the second constructor with Integer type
        Constructor<Car> constructor = clazz.getDeclaredConstructor();
        //Cancel Java language access check when using
        constructor.setAccessible(true);
        //Instanced object
        Car car = constructor.newInstance();
        //Get all method names but not inherited methods
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("Method name=" + method.getName());
            //Cancel Java language access check when using
            method.setAccessible(true);
            //Calling method
            method.invoke(car);
        }
    }

Console printing:
Method name = toString
Car{name='null', age=null}
Method name = say
I'm a cat

Posted by twinedev on Thu, 02 Apr 2020 16:36:06 -0700