Fundamentals of java reflection mechanism

Posted by zoobooboozoo on Sun, 26 Dec 2021 05:34:01 +0100

Reflection (you also want to get the information in the class when the class is loaded) / / use the class in another way

Normally, you need to load the class first, and then create an object call

When we don't know the properties, methods, constructors and comments in the class, we also want to call the properties and methods in the class. We can get the class information through reflection and then use it.

java.lang.Class {/ / without a constructor, objects cannot be created, so it is the class of all classes (including object)

}

Object is an object of Class

new Class() / / but Class cannot be created. Object = object;

Class of class

Class Field of property

Class Method of Method

Constructor, annotation..... All have corresponding classes, which are objects of large classes

Reflection can destroy encapsulation

Get the public modified attribute in the class, and also include the public attribute inherited from the parent class
Field[] getFields()
Field getField(String name)
Get the properties declared in the class (including private), but you cannot get the properties inherited from the parent class
Field[] getDeclaredFields()
Field getDeclaredField(String name)

Gets the public method in the current class, including the public method inherited from the parent class
Method[] getMethods()
Method getMethod(String name, Class<?>... parameterTypes)
Gets the method declared in the current class (including private), but cannot get the method inherited from the parent class
Method[] getDeclaredMethods()
Method getDeclaredMethod(String name, Class<?>... parameterTypes)

Reflection access properties

public static void main(String[] args)throws Exception {
    Student stu = new Student();
    Class c = stu.getClass();
    //Gets the property named name in the class
    Field f1 = c.getDeclaredField("name");
    //Set the private property to be accessible, otherwise an error will be reported
    f1.setAccessible(true);
    //Assign a value to the name attribute of the specified object by reflection
    //Equivalent to the previous stu name = "tom";
    f1.set(stu,"tom");
    //Get the value of this property by reflection
    //Equivalent to the previous stu name
    System.out.println(f1.get(stu));
    System.out.println("----------------------");
    //Gets the property named age in the class
    Field f2 = c.getDeclaredField("age");
    //Assign a value to the age attribute of the specified object by reflection
    //Equivalent to the previous stu age = 20;
    f2.set(stu,20);
    //Get the value of this property by reflection
    //Equivalent to the previous stu age
    System.out.println(f2.get(stu));
    System.out.println("----------------------");
    //Gets the property named num in the class
    Field f3 = c.getDeclaredField("num");
    //Assign a value to the static attribute num in the way of reflection, and no object is required
    //Equivalent to previous student num = 99;
    f3.set(null,99);
    //Use reflection to get the value of this static property, and no object is required
    //Equivalent to previous student num
    System.out.println(f3.get(null));
}

Reflection call method

public static void main(String[] args)throws Exception {
    Student stu = new Student();
    Class c = stu.getClass();
    //Get the toString method in the class without parameters. This is a method inherited from the parent class
    Method m1 = c.getMethod("toString", null);
    //The method in the stu object is called in a reflective manner, without parameters, and the execution result is received
    //Equivalent to the previous: object result = stu toString();
    Object result = m1.invoke(stu,null);
    //Output execution results
    System.out.println(result);
    System.out.println("-------------------");
    //To get the sayHello method in the class, you need a String type parameter, which is your own defined method
    Method m2 = c.getMethod("sayHello", String.class);
    //In the reflection mode, call this method in the stu object with the parameter "tom" and receive the execution result
    //Equivalent to the previous: object result = stu sayHello("tom");
    result = m2.invoke(stu,"tom");
    //Output execution results
    System.out.println(result);
}`

Note that public Object invoke(Object obj, Object... args)
obj, indicating which object method to call. If it is a static method, null can be passed
args, variable parameter, indicating the parameter list of the method to be called
m. M in invoke (obj, null) is a specified method in the Class obtained through the Class object

Create objects with reflections

You can create objects by using reflection, that is, you need to call the constructor by reflection.

For example, a reflection invokes an object created by a parameterless constructor in a class

public static void main(String[] args)throws Exception {
    Class c = Student.class;
    //By default, the parameterless constructor in the class is called to create the object
    //Equivalent to the previous: Object obj = new Student();
    Object obj = c.newInstance();
    System.out.println(obj);
}

For example, there is a parameter constructor in the reflection call class to create an object

public static void main(String[] args)throws Exception {
    Class c = Student.class;
    //Gets the two parameter constructor in the class
    Constructor constructor = c.getConstructor(String.class, int.class);
    //Call the parameterized constructor to create the object and pass in the corresponding parameter value
    //Equivalent to the previous: Object obj = new Student("tom",20);
    Object obj = constructor.newInstance("tom",20);
    System.out.println(obj);
}

Reflection get annotation

Using reflection, you can get the annotation information in a class

Similar to the above, but can only catch the annotations running in the runtime stage

Topics: Java reflection