Preface:

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; /** * omit get,setMethods * / publicPerson() {}}Copy the code

Class is ready to use introspection.

Step 1: Get the BeanInfo object

@Test
public void demo(){// Call the getBeanInfo() method in the Introspector, passing in the Class object 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 the getBeanInfo() method in the Introspector, passing in the Class object 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>(); // Assign a value to Map, where key is the variable name in Person and value is the value to be assigned p1.put("name"."zack");
    p1.put("sex"."male"); Person = new Person(); // Assign a value to an objectsetData2Object(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 (); // Iterate over the descriptorfor(PropertyDescriptor descriptor: propertyDescriptors) {/ / by the attribute name to Map to find the corresponding key String name = descriptor. The getName (); // Find the corresponding key and assign the valueif(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.

The last

Welcome everyone interested can pay attention to my public number [Java small melon brother sharing platform], the article will be updated inside, there are all kinds of Java information is free to share.