Class clazz = sourceObj.getClass();

1、获取所有属性

BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

2、获取指定属性
PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);

Method getMethod = pd.getReadMethod();// 获得get方法
Object val = getMethod.invoke(sourceObj);// 执行get方法返回值

Method setMethod = pd.getWriteMethod();//获得set方法

setMethod .invoke(sourceObj, newVal);

 

相关文章:

  • 2021-08-06
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
猜你喜欢
  • 2022-12-23
  • 2021-08-30
  • 2021-08-07
  • 2021-11-06
  • 2022-12-23
  • 2021-12-10
  • 2021-12-27
相关资源
相似解决方案