SpringのBeanUtils.copyPropertiesメソッド
6885 ワード
主なソースコードは以下の通りです:
public abstract class BeanUtils {
//
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
public static void copyProperties(Object source, Object target, Class> editable) throws BeansException {
copyProperties(source, target, editable, (String[]) null);
}
public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
copyProperties(source, target, null, ignoreProperties);
}
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;
}
// target , class
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
//
List ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
// target
for (PropertyDescriptor targetPd : targetPds) {
//
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
// target , source
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
// source
Method readMethod = sourcePd.getReadMethod();
// ,
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
// public , accessible=true
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// public , accessible=true
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
//target.setXXX(source.getXXX())
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
public static PropertyDescriptor[] getPropertyDescriptors(Class> clazz) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getPropertyDescriptors();
}
}