✿ let's take learning introspection mechanism and reflection as an example
1. Learn new knowledge points - remember the function (first understand what it is, what is the connection and function with the previous knowledge points)
(1) What is reflection first? [briefly understand and compare the official description]
-
Simple understanding: reflection is the process of getting the class [java.lang.Class] of the class (this kind of object) and getting the metadata of the class.
-
More official description: during runtime, dynamically obtain the information of members in a class (constructor, method, field, internal class, interface, parent class, etc.).
(1) What is introspection first?
-
Simple understanding: the mechanism implemented by reflection to obtain the information of Java beans.
-
More official description: the technology launched by sun company for a special class - JavaBean. The introspection mechanism is used to obtain the information of JavaBeans.
(2) The link between introspection and Reflection:
In fact, the introspection mechanism is also realized through reflection, which is suitable for all classes to dynamically obtain class information, and the introspection mechanism is a technology launched by sun company for the special class JavaBean. The introspection mechanism is used to obtain the information of JavaBeans.
(3) What is the function of reflection?
- Get the class [java.lang.Class] of the class (this kind of object), that is, get the class information (constructor, method, field, internal class, interface, parent class, etc.).
(3) What is the role of introspection?
- It is used to obtain the information of JavaBeans (properties, methods, events)
2. Learn new knowledge -- grasp the core category, breakthrough, see the name and know the meaning
-
First focus on the package where the core Class is located, for example, the reflection Class is in the lang package; Introspection Introspector in beans package
■ for example, the core Class of reflection - Class [see the meaning of the name for the combination of functions, and has all the information related to the Class (constructor, method, field, internal Class, interface, parent Class, etc.)]
■ for example, Introspector, the core class of introspection mechanism [see the name and meaning for the combination of functions, and has all the information (attributes, methods and events) related to JavaBeans]
2-2. Reflection operation constructor (Methods and fields can be obtained in the same way)
Class (core class, breakthrough of learning reflection mechanism, learning entry of learning operation constructor) - operation g constructor
-
The first step is to get the bytecode object of the class where the constructor is located (to get the constructor of a class, you need to load the class into the virtual machine first):
-
The static method forName("fully qualified name of the Class") in the Class gets the Class object [bytecode object]
-
Find the method to get the constructor in Class [in the breakthrough: get the constructor object]
-
-
The second breakthrough is the Constructor with everything related to the Constructor: [in the breakthrough: get the method of creating object instances]
-
Constructor maximum effect: create object
-
✿ the code of reflection mechanism operation constructor is as follows:
//The constructor in the Class can be accessed by the outside world without parameters, and the object can be created directly using the newInstance method of Class Class<Person> clazz = Person.class; Constructor<Person> con= clazz.getConstructor(); con.newInstance(); System.out.println("========================"); //Reflection: call constructor's method to create object [parameterless constructor] clazz = Person.class; con= clazz.getConstructor(String.class); con.newInstance("shan"); System.out.println("========================"); //Reflection: call constructor's method to create object [parameterless constructor] clazz = Person.class; con= clazz.getDeclaredConstructor(String.class, int.class); //Set the current constructor [private] to be accessible con.setAccessible(true); con.newInstance("shan", 10);
2-2. Introspection - operating JavaBeans
Introspector (core class, breakthrough for learning introspection mechanism, learning entry for learning and operating JavaBeans) - operating JavaBeans
-
The first step is to get the javaBean: [in the breakthrough: get the description object of javaBean, BeanInfo object]
- The parameter of the method getBeanInfo is the bytecode of the javaBean, and the result is the information of the javaBean - BeanInfo object (the description object of the javaBean)
-
The second breakthrough is the BeanInfo that owns everything about JavaBeans: [in the breakthrough: get the property descriptor of JavaBeans]
- Method getPropertyDescriptors(), and the result is the property information of javaBean - PropertyDescriptor property array object (property descriptor object of javaBean)
-
The third breakthrough is the PropertyDescriptor that owns all the properties of javaBean: [in the breakthrough: get the operation on properties]
- The property descriptor is an array -- traversal
✿ the attribute code of javaBean operated by introspection mechanism is as follows:
@Test public void testIntrospector() throws Exception { //1. Obtain the javaBean description object BeanInfo through the breakthrough (core class Introspector) BeanInfo beanInfo = Introspector.getBeanInfo(Person.class,Object.class); //2. Get the property descriptor in javaBean PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //Create a Person object Person person = new Person(); for (PropertyDescriptor pd : pds) {//Traversal array descriptor array //3-1. Get the property name of the current property System.out.println("The property name of the current property is:" +pd.getName()); System.out.println("Of the current property getter The method is:" +pd.getReadMethod()); System.out.println("Of the current property setter The method is:" +pd.getWriteMethod()); if("name".equals(pd.getName())) {//If the current attribute name is name [the setName method cannot be used directly, because the specific method is not known during reflection] Method setter = pd.getWriteMethod();//3-2. Get setter method setter.invoke(person, "shan");//Set the name value of the current person object to shan } } }