将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
2015-12-29 16:48
656 查看
/***
* 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
*
* @param source
* 要赋值的源对象
* @param target
* 被赋值的目标对象
* @param ignoreProperties
* 被忽略赋值的属性数组
* @throws Exception
*
*/
@SuppressWarnings("unused")
public static void copyProperties(Object source, Object target, String ignoreProperties[]) throws Exception {
Class targetClass = target.getClass();
Class sourceClass = source.getClass();
// 得到目标对象和源对象的属性数组
PropertyDescriptor targetPds[] = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
PropertyDescriptor sourcetPds[] = Introspector.getBeanInfo(sourceClass).getPropertyDescriptors();
// 将忽略字段的数组转化为list
List<String> ignoreList = ignoreProperties == null ? null : Arrays.asList(ignoreProperties);
// 把源对象的所有属性放的map中
Map<String, PropertyDescriptor> sourcePropertyMap = new HashMap<String, PropertyDescriptor>();
for (int i = 0; i < sourcetPds.length; i++) {
PropertyDescriptor pd = sourcetPds[i];
sourcePropertyMap.put(pd.getName(), pd);
}
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];
if (targetPd.getWriteMethod() == null || ignoreProperties != null && ignoreList.contains(targetPd.getName()))
continue;
PropertyDescriptor sourcePd = sourcePropertyMap.get(targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null)
continue;
try {
// 得到源对象对应的属性值
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
readMethod.setAccessible(true);
Object value = readMethod.invoke(source, new Object[0]);
// 将源对象的属性值赋值给目标对象对应的属性
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))
writeMethod.setAccessible(true);
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
throw new IllegalArgumentException("Could not copy properties from source to target", ex);
}
}
}
* 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
*
* @param source
* 要赋值的源对象
* @param target
* 被赋值的目标对象
* @param ignoreProperties
* 被忽略赋值的属性数组
* @throws Exception
*
*/
@SuppressWarnings("unused")
public static void copyProperties(Object source, Object target, String ignoreProperties[]) throws Exception {
Class targetClass = target.getClass();
Class sourceClass = source.getClass();
// 得到目标对象和源对象的属性数组
PropertyDescriptor targetPds[] = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
PropertyDescriptor sourcetPds[] = Introspector.getBeanInfo(sourceClass).getPropertyDescriptors();
// 将忽略字段的数组转化为list
List<String> ignoreList = ignoreProperties == null ? null : Arrays.asList(ignoreProperties);
// 把源对象的所有属性放的map中
Map<String, PropertyDescriptor> sourcePropertyMap = new HashMap<String, PropertyDescriptor>();
for (int i = 0; i < sourcetPds.length; i++) {
PropertyDescriptor pd = sourcetPds[i];
sourcePropertyMap.put(pd.getName(), pd);
}
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];
if (targetPd.getWriteMethod() == null || ignoreProperties != null && ignoreList.contains(targetPd.getName()))
continue;
PropertyDescriptor sourcePd = sourcePropertyMap.get(targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null)
continue;
try {
// 得到源对象对应的属性值
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
readMethod.setAccessible(true);
Object value = readMethod.invoke(source, new Object[0]);
// 将源对象的属性值赋值给目标对象对应的属性
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))
writeMethod.setAccessible(true);
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
throw new IllegalArgumentException("Could not copy properties from source to target", ex);
}
}
}
相关文章推荐
- prepareStatement的用法和解释
- 各种排序算法的分析及java实现
- 【Java EE 学习 80 上】【WebService】
- PreparedStatement的用法
- PHPStudy+eclipse环境配置
- Spring项目中使用Junit单元测试并配置数据源的问题
- Java反射
- java图片添加水印实例代码分享
- 设计模式之简单工厂模式
- java变量的初始化顺序
- Selenium-webdriver(JAVA)代码总结
- eclipse安装插件jad
- 配置Eclipse远程调试weblogic应用的环境
- Spring+Quartz任务调度(一)
- 【转】 java获得项目绝对路径
- spring事务管理实例备注
- java 设置字体
- hive复杂类型与java类型的对应
- Java读写txt文件时防止中文乱码问题出现的方法介绍
- 【Android实测】eclipse run as timeout的解决办法