Deeply understand Java reflection mechanism

Deeply understand Java reflection mechanism Introduction: The Java reflection mechanism is a powerful feature that can analyze the internal information of classes, interfaces, methods, and properties at runtime. It provides the ability to dynamically manipulate Java objects, allowing us to check, retrieve, and modify object properties, call object methods, and even instantiate a class object at runtime. Through the reflection mechanism, Java's dynamism and flexibility have been greatly improved, providing many conveniences for some special needs and frameworks. The basic concept of reflection mechanism: The core class of the Java reflection mechanism is' Class', which represents the class object of a Java class. Through this class object, we can obtain information and operations related to that class. 1. Obtain Class object: To use the reflection mechanism, the first step is to obtain the 'Class' object of the target class. There are several methods available: -Using the 'Class. forName (String className)' method, pass in the fully qualified name of the class to obtain it. -Use the '. class' syntax, such as' String. class'. -Call the '. getClass()' method of the object. 2. Obtain the properties of the class: Obtaining the properties of a class is one of the important functions of the reflection mechanism, which can be achieved through some methods of the 'Class' object, such as: -'getDeclaredFields()': Gets all attributes declared in the class, including private attributes. -'getField (String name)': Gets the public attribute of the specified name. -'getDeclaredField (String name)': Gets the attribute of the specified name, which can be public or private. 3. Method for obtaining classes: The method of obtaining a class is similar to obtaining properties, which can be implemented through some methods of the 'Class' object, such as: -'getDeclaredMethods()': Gets all methods defined in the class, including private methods. -'getMethod (String name, Class<?>... parameterTypes)': Gets the public method for the specified name and parameter type. -'getDeclaredMethod (String name, Class<?>... parameterTypes)': Gets the method for the specified name and parameter type, which can be public or private. 4. Call the method of the class: After obtaining the method object, the reflection mechanism can be used to call the method, whether it is a public or private method. By using the 'invoke (Object obj, Object... args)' method of the 'Method' class, passing in the object and parameters, the method can be called. 5. Create an instance of the class: By using the reflection mechanism, an instance of a class can be created at runtime, even if the constructor cannot be accessed. Implement through the 'newInstance()' method of the 'Class' object, noting that the instantiated class needs to have a parameterless construction method. Example code: The following is an example code that uses reflection mechanism to obtain class properties, call methods, and create instances: import java.lang.reflect.*; public class ReflectExample { public static void main(String[] arguments) throws Exception { Class<?> clazz = Class.forName("com.example.Person"); //Get the properties of the class Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { System. out. println ("attribute name:"+field. getName()); } //Calling methods of a class Method method = clazz.getMethod("greet", String.class); Object obj = clazz.newInstance(); method.invoke(obj, "World"); //Create an instance of a class Object instance = clazz.newInstance(); System.out.println(instance.toString()); } } class Person { private String name; public void greet(String target) { System.out.println("Hello, " + target + "!"); } } Conclusion: Reflection mechanism is a powerful feature in Java that allows us to analyze and manipulate classes, methods, and properties at runtime. It brings greater flexibility and dynamism to Java programming. However, the reflection mechanism may bring some performance losses, so it should be used with caution. Overall, understanding and proficiently using reflection mechanisms will enable us to better understand and apply the powerful features of Java.