PropertyDescriptor动态反射setter和getter设置对象属性
PropertyDescriptor
我们在开发的过程中,有时会需要动态地设置属性,也就是动态getter、 setter。
使用传统的反射 Method、Field等类去处理,需要对方法名进行大量拼接,比较麻烦。
可以使用 PropertyDescriptor.
常用方法:
- 构造方法:
PropertyDescriptor(String propertyName, Class<?> beanClass): 第一个参数是属性名称,第二个参数是Class类。
- 其他方法:
Method getReadMethod(): 返回 Method
propertyDescriptor.getReadMethod().invoke(Object obj, Object... args):
相当于 get方法。第一个参数是对象,后面的参数是属性名称
propertyDescriptor.getWriteMethod().invoke(Object obj): 相当于 set方法。
示例如下:
- 对象类 Person :
public class Person {
private int age;
private String name;
private String address;
//getter、 setter方法
}
- PropertyDescriptor使用如下:
public static void propertyDescriptorTest() {
try {
Person person = new Person();
//setter
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("address", Person.class);
propertyDescriptor.getWriteMethod().invoke(person, "深圳市");
//getter
String address = (String)propertyDescriptor.getReadMethod().invoke(person);
System.out.println(address);
} catch (Exception e) {
log.error("property Exception.",e);
}
}
BeanWrapper 和 BeanWrapperImpl
BeanWrapper和BeanWrapperImpl是 Spring的接口和类,可以通过BeanWrapper和BeanWrapperImpl获取 PropertyDescriptor。
- 构造方法:
BeanWrapperImpl(Object object): 参数为对象
- 其他方法:
PropertyDescriptor[] getPropertyDescriptors(): 用于获取对象的所有 PropertyDescriptor
PropertyDescriptor getPropertyDescriptor(String var1) throws InvalidPropertyException: 获取指定属性名称的PropertyDescriptor
BeanWrapperImpl示例如下:
- 通过 BeanWrapper获取 PropertyDescriptor:
public static void getPropertyByBeanWrapper() {
try {
Person person = new Person();
//初始化
BeanWrapper beanWrapper = new BeanWrapperImpl(person);
//setter
PropertyDescriptor propertyDescriptor = beanWrapper.getPropertyDescriptor("address");
propertyDescriptor.getWriteMethod().invoke(person, "深圳市");
//getter
String address = (String)propertyDescriptor.getReadMethod().invoke(person);
System.out.println(address);
} catch (Exception e) {
log.error("getPropertyNameDescriptor exception.", e);
}
}
- 通过 BeanWrapper 设置所有的属性:
public static void setAllProperty(Person person) {
BeanWrapper beanWrapper = new BeanWrapperImpl(person);
//获取所有的 propertyDescriptor
PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String name = propertyDescriptor.getName();
Class<?> propertyType = propertyDescriptor.getPropertyType();
boolean isNull = beanWrapper.getPropertyValue(name) == null;
//所有String类型的属性,如果为null,改为""
if (String.class == propertyType && isNull) {
beanWrapper.setPropertyValue(name, "");
}
//所有Integer类型的属性,如果为null,改为0
if (Integer.class == propertyType && isNull) {
beanWrapper.setPropertyValue(name, 0);
}
}
}
分类:
其他--java
, A1-java代码块积累
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-10-19 ElastaticSearch---- es聚合查询
2020-10-19 ElastaticSearch-----es的DSL查询与过滤
2020-10-19 ElastaticSearch ---- es基础概念及命令