private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
//获取目标对象的各属性信息
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
PropertyDescriptor[] var7 = targetPds;
int var8 = targetPds.length;
//遍历各属性
for(int var9 = 0; var9 < var8; ++var9) {
PropertyDescriptor targetPd = var7[var9];
//获取目标对象该属性的写方法,即set方法
Method writeMethod = targetPd.getWriteMethod();
//判断该属性是否在不被copy的属性集合中
if (
一探Spring中BeanUtils的copyProperties方法
最新推荐文章于 2023-06-18 17:44:31 发布
本文探讨了Spring中BeanUtils.copyProperties方法的工作原理,指出成功复制属性需满足的三个条件:属性名相同,源对象需有get方法,目标对象需有set方法且类型匹配。即使是内部类或泛型不同,只要运行时类型兼容,仍可进行复制。参考自www.jianshu.com/p/357b55852efc。
摘要由CSDN通过智能技术生成