/**
* 通过javabean调用对象的set方法
*
* @param object
* @param propertyName
* @param propertyValue
* @Author zy
*/
public static void setPropertyValue(Object object, String propertyName, Object propertyValue) throws Exception {
if (object == null)
throw new Exception("object 参数不能为空!");
if (propertyName == null)
throw new Exception("propertyName 参数不能为空!");
try {
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (propertyName.equals(propertyDescriptor.getName())) {
if (propertyDescriptor.getPropertyType() == null ||
!propertyDescriptor.getPropertyType().equals(propertyValue.getClass())) {
throw new Exception("propertyValue 类型不匹配");
}
propertyDescriptor.getWriteMethod().invoke(object, propertyValue);
break;
}
}
} catch (Exception e) {
throw e;
}
}
通过javabean调用对象的set方法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 怎样反思自己的教学——学习杜威《我们怎样思维》有感 2015-01-13 14:56:52 来源:普宁市第三中学网...
- 股市上上下下,一些炒短线的投资者吃了不少亏,这些投资者往往在大盘暴跌时出于恐慌全部出货,等到大盘猛涨之后,又全仓追...