java反射获取方法上的注解_java 反射获取属性上的注解来判断是否操作该值

Field f = Test.class.getDeclaredField("a");

boolean falg = f.getAnnotation(Bean.class);

@see获取含有注解属性 https://www.cnblogs.com/aademeng/articles/11218079.html

PropertyDescriptor类:(属性描述器)

PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:

1. getPropertyType(),获得属性的Class对象;

2. getReadMethod(),获得用于读取属性值的方法;

3. getWriteMethod(),获得用于写入属性值的方法;

4. hashCode(),获取对象的哈希值;

5. setReadMethod(Method readMethod),设置用于读取属性值的方法;

6. setWriteMethod(Method writeMethod),设置用于写入属性值的方法。

String fieldName = field.getName();

// 获取bean的某个属性的描述符

PropertyDescriptor pd = new PropertyDescriptor(fieldName, cla);

// 获得用于读取属性值的方法

Method readMethod = pd.getReadMethod();

// 读取属性值

Object val = readMethod.invoke(obj);

@see属性描述器 https://blog.csdn.net/weixin_42069143/article/details/82119724

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值