More powerful than the reflection technique, introspection technology

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ZackSock/article/details/100611768

In the process of learning Java, always progressive layers. Almost from the data type to IO, even if abnormal fundamental part of (the book different is not the same). After Java is advanced, usually we will learn generics, reflection and the like. Today to say things, and reflecting a relationship, called "introspection technology." "Introspection technology" is based on a reflective technology, it provides more convenient operation of JavaBean API. Generally, we only learned JavaWeb will call JavaBean, can be seen "introspection technology" belongs JavaWeb content (funny logic, never mind).

1. What is introspection

     As already said, introspection API JDK is provided JavaBean operation, based on reflective technology.

2, how to use the code introspection

     Introspection to use the class is Introspector, in the final analysis is the use of introspection learn learning class Introspecteor class.

Since introspection technology is relatively simple, the following example the code directly. Create a Person class:

public class Person{
    
    private String name;

    private String sex;

    /**
    *    省略get、set方法  
    */

    public Person(){

    }

}

Class ready to start using the introspection.

The first step: Get the BeanInfo objects

@Test
public void demo(){
    
    //调用Introspector中的getBeanInfo()方法,传入要获取信息的类的Class对象
    BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

}

BeanInfo above information is complete Person class.

Step Two: obtaining attributes and methods described descriptor

@Test
public void demo(){
    
    //调用Introspector中的getBeanInfo()方法,传入要获取信息的类的Class对象
    BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

    
    /*********************分割线****************************/
    
    //获取方法描述器
    MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
    
    //获取属性描述器,调用getPropertyDescriptors方法,返回一个PropertyDescriptor数组
    //其中一个PropertyDescriptor代表一个属性
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

}

After the corresponding operation can be carried out the. I learned JavaWeb should all know, form functional automatic packaging data. In fact, it is the realization of introspection technology, it specifically cited a similar example.

Requirements: the data to be assigned penetrate Map, then use the method, the encapsulated data to the Map JavaBean them.

We did not talk much, just start writing. They are: to create Map-> assigned to Map -> Create Object -> pass the value of the object Map

@Test
public void demo(){
    
    //创建一个Map
    Map<String, String> p1 = new HashMap<String, String>();
    
    //给Map赋值,其中key为Person中的变量名,value为要赋的值
    p1.put("name", "zack");
    p1.put("sex", "male");

    //创建Person对象
    Person person = new Person();

    //给对象赋值,待实现方法
    setData2Object(p1, person);
}

Above using a setData2Object () method, and now we look to implement this method:

private static void setData2Object(Map<String, String> map, Object obj) 
                                        throws Exception {
    //获取BeanInfo
    BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass());

    //获取属性描述器
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

    //遍历描述器
    for (PropertyDescriptor descriptor:propertyDescriptors) {
        
        //通过属性名去Map中找对应的key
        String name = descriptor.getName();
        
        //找到对应的key后赋值
        if (map.containsKey(name)){
            String value = map.get(name);
            
            //通过属性描述器获得写入属性的方法
            Method writeMethod = descriptor.getWriteMethod();

            //利用反射设置Value
            writeMethod.invoke(person, value);
        }
    }   
}

Something which has a front did not speak, is obtained by the method of writing attribute property descriptor . In the api, the property has two methods described getWriteMethod, getReadMethod. It simply is to get get, set method. After using the reflection on it.

To add a number of introspection attributes is described by a number of attributes not to count, but through get, set method. Such as: setAge, getName is to the front of the set and get rid, get a property name.

Guess you like

Origin blog.csdn.net/ZackSock/article/details/100611768