内省

Introseptor 类提供了一个标准的方式获取 Java Bean 所提供的 属性事件、和方法
并构建一个 BeanInfo 对象:

可以全面的描述所提供的Java Bean。

public class Person {

    private String name;
    private Integer age;

    // setter、getter

}

public class BeanInfoDemo {

    public static void main(String[] args) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

        // 获取Bean的PropertyDescriptor
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        Stream.of(propertyDescriptors).forEach(propertyDescriptor -> {
            System.out.println(propertyDescriptor);

            // 获取属性名
            String propertyName = propertyDescriptor.getName();

            // 对于属性age,设置类型转换
            if (propertyName.equals("age")) {
                propertyDescriptor.setPropertyEditorClass(AgeEditorClass.class);
            }
        });
    }

    static class AgeEditorClass extends PropertyEditorSupport {
        public void setAsText(String text) throws java.lang.IllegalArgumentException {
            this.setValue(Integer.valueOf(text));
        }
    }
}