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);
		}
	}
}

posted on   乐之者v  阅读(809)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.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基础概念及命令
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示