利用反射给JavaBean中的属性进行读写操作类PropertyDescriptor

  • 概述
    PropertyDescriptor描述Java Bean中通过一对存储器方法(getter / setter)导出的一个属性。我们可以通过该PropertyDescriptor对bean中的该属性进行读取和写入操作,也可以设置其getter / setter。

  • 关键接口及内部属性

public PropertyDescriptor(String name, Class<?> beanClass) throws IntrospectionException
public PropertyDescriptor(String name, Class<?> beanClass, String getMethodName, String setMethodName) throws IntrospectionException
public PropertyDescriptor(String name, Method readMethod, Method writeMethod) throws IntrospectionException

public Class<?> getPropertyType()
public Method getReadMethod()
public Method getWriteMethod()

public void setReadMethod(Method readMethod) throws IntrospectionException
public void setWriteMethod(Method writeMethod)
public boolean equals(Object o)&nbsp;

相关的PropertyDescriptor内部属性如下:
Class<?> propertyType; //该属性的类型
Method getMethod; //getter
Method setMethod; //setter
还有继承自其父类FeatureDescriptor的功能,用于指定该属性的编程名称

  • 简单应用
    Person类如下:
package com.cwind.property;

public class Person {
        private String name ;
        private int age ;
       
        public Person(){ this.name = ""; this.age = 0; }
        public Person(String name, int age) { super(); this.name = name; this. age = age; }

        public String getName() { return name; }
        public void setName(String name) { this. name = name; }

        public int getAge() { return age; }
        public void setAge(int age) { this. age = age; }
       
        public String getNameInUpperCase(){
               return this .name .toUpperCase();
       }
        public void setNameToLowerCase(String name){
               this.name = name.toLowerCase();
       }
}

该类中除了name和age两个属性的标准getter和setter之外,还有增加了一个获取大写name的get方法和一个将name设置为小写的set方法。
在测试类中,首先获得这两个方法对象。

Class personClass = Class.forName("com.cwind.property.Person");
Method read = personClass.getMethod("getNameInUpperCase", null);
Method write = personClass.getMethod("setNameToLowerCase", String.class );

//然后可以通过两种方式构造PropertyDescriptor
PropertyDescriptor prop1 = new PropertyDescriptor( "name", Person.class );     //使用其标准getter和setter
PropertyDescriptor prop2 = new PropertyDescriptor( "name", read, write);     //使用read和write两个方法对象所自定义的getter和setter

//下面构建一个Person对象
Person person = new Person("Kobe" , 36);
System. out.println(prop1.getReadMethod().invoke(person, null));     // --实际调用Person.getName(), result: Kobe
System. out.println(prop2.getReadMethod().invoke(person, null));     // --实际调用Person.getNameInUpperCase(), result: KOBE
                     
prop1.getWriteMethod().invoke(person, "James");     // --实际调用Person.setName(), person.name被设置为James
prop2.getWriteMethod().invoke(person, "James");     // --实际调用Person.setNameToLowerCase(), person.name被设置为james

  • 利用PropertyDescriptor类抽象公共方法
/**
     * @Methodname:converter 数据转换工具将value转为name
     * 
     * 将List<SysDictionaryDo> dicList对象里的value属性值
     * 赋值给 List<T> list对象里的field属性
     */
    public static <T> void converter(List<T> list, List<SysDictionaryDo> dicList, String field) throws Exception {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        PropertyDescriptor pd = new PropertyDescriptor(field, list.get(0).getClass());
        Method readMethod = pd.getReadMethod();
        Method writeMethod = pd.getWriteMethod();
        for (int i = 0; i < list.size(); i++) {
            T obj = list.get(i);
            String key = (String) readMethod.invoke(obj);
            for (SysDictionaryDo dic : dicList) {
                if (dic.getValue().equals(key)) {
                    writeMethod.invoke(obj, dic.getName());
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/chinasi2012/article/details/85322013