Skip to content
Go back

Java中的字段拷贝复制

Updated:  at  12:41 AM

工作中记录一次排错

copyProperties方法

Spring的BeanUtils源码:

if (readMethod != null &&
        ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
    try {
        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
            readMethod.setAccessible(true);
        }
        Object value = readMethod.invoke(source);
        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
            writeMethod.setAccessible(true);
        }
        writeMethod.invoke(target, value);
    }
    catch (Throwable ex) {
        throw new FatalBeanException(
                "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
    }
}					

BeanUtils::copyProperties 源码:

// Convert the specified value to the required type and store it
if (index >= 0) {                    // Destination must be indexed
    value = convertForCopy(value, type.getComponentType());
    try {
        getPropertyUtils().setIndexedProperty(target, propName,
                                         index, value);
    } catch (final NoSuchMethodException e) {
        throw new InvocationTargetException
            (e, "Cannot set " + propName);
    }
}

Suggest Changes

Previous Post
获取Type类型的常见方法
Next Post
ThreadPoolExecutor的重要特性和概念