This is the 10th day of my participation in the Novembermore Challenge.The final text challenge in 2021
In learning Java, there are always layers of progression. Almost everything from data types to IO, exceptions and even the basics (different books vary). Then we move on to Java, where we learn about generics, reflection, and so on. Today’s talk has to do with reflection, called introspection techniques. Introspection technology is based on reflection technology and provides more apis for manipulating Javabeans. In general, we don’t call Javabeans until we learn JavaWeb, so “introspective techniques” are part of JavaWeb (funny logic, never mind).
1. What is introspection
As mentioned, introspection is an API provided by the JDK for JavaBean operations, based on reflection technology.
2. How to code introspection
The class that introspection uses is called Introspector, and ultimately learning introspection is learning the use of the class Introspecteor.
Because introspection techniques are relatively simple, let’s use code for examples. Create a Person class:
public class Person{ private String name; private String sex; Public Person(){}}Copy the code
Class is ready to use introspection.
Step 1: Get the BeanInfo object
@test public void demo(){// Call Introspector getBeanInfo(), BeanInfo BeanInfo = Introspector.getBeanInfo(person.class); }Copy the code
The beanInfo above is the complete information for the Person class.
Step 2: Get the method descriptor and property descriptor
@test public void demo(){// Call Introspector getBeanInfo(), BeanInfo BeanInfo = Introspector.getBeanInfo(person.class); / * * * * * * * * * * * * * * * * * * * * * line * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / get methods descriptor MethodDescriptor methodDescriptors = [] beanInfo.getMethodDescriptors(); // Get the property descriptor, call getPropertyDescriptors, Return an array of propertyDescriptors // where one PropertyDescriptor represents a PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); }Copy the code
Then you can do something about it. As anyone who has studied JavaWeb knows, forms have a feature that automatically encapsulates data. In fact, its implementation is introspection technique, specific to take a similar example.
Requirements: Thread the data to be assigned into a Map, then use methods to encapsulate the Map data into a JavaBean.
We didn’t say much and just started writing. Create a Map-> Assign a value to a Map-> Create an object -> pass the value from the Map to the object
@test public void demo(){// Create a Map Map<String, String> p1 = new HashMap<String, String>(); P1. put("name", "zack"); // Assign to Map, where key is the variable name in Person and value is the value to be assigned. p1.put("sex", "male"); Person = new Person(); SetData2Object (p1, person); }Copy the code
We used a setData2Object() method, which we now implement:
private static void setData2Object(Map<String, String> map, Object obj) throws Exception {// Obtain BeanInfo BeanInfo BeanInfo = Introspector.getBeanInfo(person.getClass()); / / retrieve attributes descriptor PropertyDescriptor [] propertyDescriptors = beanInfo. GetPropertyDescriptors (); / / traverse descriptor for (PropertyDescriptor descriptor: propertyDescriptors) {/ / by the property name in the Map to find the corresponding key String name = descriptor.getName(); If (map.containsKey(name)){String value = map.get(name); if (map.containsKey(name)){String value = map.get(name); / / Method by using the Method of descriptor for writing attribute writeMethod = descriptor. GetWriteMethod (); // Invoke Value writemethod. invoke(person, Value); }}}Copy the code
One of the things that I didn’t talk about is how to get a write property from the property descriptor. In the API, the property descriptor has two methods: getWriteMethod and getReadMethod. Get the get and set methods. And then we use reflection.
The number of introspective attribute descriptors is calculated not by the number of attributes, but by the get and set methods. For example, setAge and getName are used to get an attribute name by removing set and get.