简介
本文介绍Java的PropertyDescriptor的用法。
PropertyDescriptor用于描述属性相关的信息,如对于读写方法的设置和读取,获取属性的类型等操作。通常用于通过反射获取对象方法。
构造方法
public PropertyDescriptor(String propertyName, // 属性的名称
Class<?> beanClass // bean的class类型
)
public PropertyDescriptor(String propertyName, // 属性名称
Class<?> beanClass, // bean的class类型
String readMethodName, // 读方法名称
String writeMethodName // 写方法名称
)
public PropertyDescriptor(String propertyName, // 属性名称
Method readMethod, // 读方法
Method writeMethod // 写方法
)
常用方法
| 方法 | 说明 |
| public synchronized Class<?> getPropertyType() {} | 获取属性的类型 |
| public synchronized Method getReadMethod() {} | 获取读方法 |
| public synchronized void setReadMethod(Method readMethod) {} | 设置读方法 |
| public synchronized Method getWriteMethod() {} | 获取写方法 |
| public synchronized void setWriteMethod(Method writeMethod){} | 设置写方法 |
| public void setPropertyEditorClass(Class<?> propertyEditorClass) {} | 设置属性可以使用的属性编辑器类型 |
| public Class<?> getPropertyEditorClass() {} | 获取当前设置的属性编辑器类型 |
| public PropertyEditor createPropertyEditor(Object bean) {} | 创建bean对象对当前属性的属性编辑器 |
示例
工具类
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class PropertyUtil {
public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) {
//构建一个可变字符串用来构建方法名称
StringBuffer sb = new StringBuffer();
Method setMethod = null;
Method getMethod = null;
PropertyDescriptor pd = null;
try {
//根据字段名来获取字段
Field f = clazz.getDeclaredField(propertyName);
if (f != null) {
//构建方法的后缀
String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
sb.append("set" + methodEnd);
//构建set方法
setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{f.getType()});
sb.delete(0, sb.length());
sb.append("get" + methodEnd);
//构建get 方法
getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{});
//构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return pd;
}
public static void setProperty(Object obj, String propertyName, Object value) {
Class clazz = obj.getClass();//获取对象的类型
//获取 clazz 类型中的 propertyName 的属性描述器
PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
//从属性描述器中获取 set 方法
Method setMethod = pd.getWriteMethod();
try {
//调用 set 方法将传入的value值保存属性中去
setMethod.invoke(obj, new Object[]{value});
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object getProperty(Object obj, String propertyName) {
//获取对象的类型
Class clazz = obj.getClass();
//获取 clazz 类型中的 propertyName 的属性描述器
PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
//从属性描述器中获取 get 方法
Method getMethod = pd.getReadMethod();
Object value = null;
try {
//调用方法获取方法的返回值
value = getMethod.invoke(clazz, new Object[]{});
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
测试类
package com.example.a;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) {
Object obj = new TestBean();
Class clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
//写数据
for (Field f : fields) {
try {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
//获得写方法
Method writeMethod = pd.getWriteMethod();
//因为知道是int类型的属性,所以传个int过去就是了。其他情况需要判断下参数类型
writeMethod.invoke(obj, 2);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//读数据
for (Field f : fields) {
try {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
//获得读方法
Method readMethod = pd.getReadMethod();
//因为知道是int类型的属性,所以转换成integer就是了。也可以不转换直接打印
Integer num = (Integer) readMethod.invoke(obj);
System.out.println(num);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}

请先 !