Spring的spring-beans库有一个org.springframework.beans.BeanUtils
类,提供了一个copyProperties
方法来将源对象实例复制到目标对象实例。然而,它只能为对象的第一级字段做到这点。以下是我的解决方案,基于BeanUtils.copyProperties
,递归执行复制每个子对象,包括集合和映射。
package my.utility;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class BeanUtils extends org.springframework.beans.BeanUtils {
public static void copyPropertiesNotNull(Object source, Object target) throws BeansException {
copyPropertiesNotNull(source, target, null, (String[]) null);
}
private static void setAccessible(Method method) {
if (!Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
method.setAccessible(true);
}
}
private static void copyPropertiesNotNull(Object source, Object target, Class<?> editable, 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);
for (PropertyDescriptor targetPropertyDescriptor : targetPds) {
Method targetWriteMethod = targetPropertyDescriptor.getWriteMethod();
if (targetWriteMethod != null
&& (ignoreList == null || !ignoreList.contains(targetPropertyDescriptor.getName()))) {
PropertyDescriptor sourcePropertyDescriptor =
getPropertyDescriptor(source.getClass(), targetPropertyDescriptor.getName());
if (sourcePropertyDescriptor != null) {
Method sourceReadMethod = sourcePropertyDescriptor.getReadMethod();
if (sourceReadMethod != null &&
ClassUtils.isAssignable(
targetWriteMethod.getParameterTypes()[0], sourceReadMethod.getReturnType())) {
try {
Method targetReadMethod = targetPropertyDescriptor.getReadMethod();
setAccessible(sourceReadMethod);
setAccessible(targetWriteMethod);
Object sourceValue = sourceReadMethod.invoke(source);
if (sourceValue != null && targetReadMethod != null) {
setAccessible(targetReadMethod);
Object targetValue = targetReadMethod.invoke(target);
if (targetValue == null) {
targetWriteMethod.invoke(target, sourceValue);
} else if(targetValue instanceof Collection<?>) {
((Collection) targetValue).addAll((Collection) sourceValue);
} else if (targetValue instanceof Map<?,?>) {
((Map) targetValue).putAll((Map) sourceValue);
} else {
copyPropertiesNotNull(sourceValue, targetValue, editable, ignoreProperties);
}
}
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPropertyDescriptor.getName() +
"' from source to target", ex);
}
}
}
}
}
}
}