This is the second day of my participation in the August Text Challenge.More challenges in August

1. What is introspection

Introspector is a default processing method of JavaBean class attributes and events.

A JavaBean is a special class that is used to pass data information. The methods in this class are used to access private fields, and the method names comply with certain naming conventions. If you pass information between two modules, you can encapsulate the information into Javabeans, called Value Objects, or VO. There are fewer methods. This information is stored in the class’s private variables, obtained via set() and get().

Its underlying implementation is also Java reflection.

JDK introspection library

2.1, Introspector class

Encapsulate properties in JavaBean for operation. The program treats a class like a JavaBean by calling introspector.getBeanInfo ()

@Test
public void test1(a) throws IntrospectionException {
    // Get User Bean information
    BeanInfo userBeanInfo = Introspector.getBeanInfo(User.class);
    // Attribute description
    PropertyDescriptor[] propertyDescriptors = userBeanInfo.getPropertyDescriptors();
    System.out.println("Attribute Description:");
    Stream.of(propertyDescriptors).forEach(System.out::println);
    // Method description
    System.out.println("Method description:");
    MethodDescriptor[] methodDescriptors = userBeanInfo.getMethodDescriptors();
    Stream.of(methodDescriptors).forEach(System.out::println);
    // Event description
    System.out.println("Event Description:");
    EventSetDescriptor[] eventSetDescriptors = userBeanInfo.getEventSetDescriptors();
    Stream.of(eventSetDescriptors).forEach(System.out::println);
}
Copy the code

2.2, PropertyDescriptor class

The PropertyDescriptor class indicates that a JavaBean class exports a property from storage. Main methods:

  1. GetPropertyType (), the Class object that gets the property;
  2. GetReadMethod (), which gets the method used to read property values; GetWriteMethod (), to get the method used to write property values;
  3. HashCode (), which gets the hash value of the object;
  4. SetReadMethod (Method readMethod) sets the Method used to read attribute values.
  5. SetWriteMethod (Method writeMethod) : sets the Method used to write attribute values.

2.3. Attribute change classes

An implementation is also provided in the Java.beans package:

  • PropertyChangeSupport
  • VetoableChangeSupportUtility classes used by beans that support constraint properties, such as determining that a new value is not null

2.4. Code practice

Suppose there is a class UserInfo:

public class UserInfo {

    private String userName;

    public String getUserName(a) {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName; }}Copy the code

Create a new test class using PropertyDescriptor, Introspector, and Spring’s introspective wrapper class BeanWrapper.

The test classes are as follows:

public class IntrospectorTest {
    @Test
    public void test(a) throws Exception {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("zhang san");

        / / use PropertyDescriptor
        setProperty(userInfo, "userName");

        / / use the Introspector
        setPropertyUseIntrospector(userInfo, "userName");

        / / use the Spring
        setPropertyUseSpring(userInfo, "userName");

    }

    public static void setPropertyUseSpring(UserInfo userInfo, String userName) {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(userInfo);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.add(userName,"Use the spring");
        bw.setPropertyValues(pvs);
        System.out.println("set userName:" + userInfo.getUserName());
    }


    public static void setProperty(UserInfo userInfo, String userName) throws Exception {
        PropertyDescriptor propDesc = new PropertyDescriptor(userName, UserInfo.class);
        Method methodSetUserName = propDesc.getWriteMethod();
        methodSetUserName.invoke(userInfo, "The use of PropertyDescriptor");
        System.out.println("set userName:" + userInfo.getUserName());
    }


    public static void setPropertyUseIntrospector(UserInfo userInfo, String userName) throws Exception {
        BeanInfo beanInfo= Introspector.getBeanInfo(UserInfo.class);
        Arrays.stream(beanInfo.getPropertyDescriptors())
                .filter(x->x.getName().equals(userName))
                .findFirst()
                .get()
                .getWriteMethod().invoke(userInfo,"Use the Introspector");
        System.out.println("set userName:"+ userInfo.getUserName()); }}Copy the code

The output is:

Set userName: Use PropertyDescriptor set userName: Use Introspector set userName: Use SpringCopy the code

Introspection is done in Java through java.beans.Introspector. Common introspection operations include the following, as well as additional types.

3, summarize

Java reflection is a very powerful way to get all the information about a class at run time and manipulate its fields, methods, constructors, and so on. Introspection is actually a subset of reflection, based on reflection. Those that specialize in Javabeans focus only on properties of Javabeans, methods, and some properties of events.