Skip to content
Go back

Java中的字段拷贝复制

Published:  at  10:10 AM

Java

copyProperties方法

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);
    }
}					
// 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
Java中截取子字符串
Next Post
快速创建一个spring-boot-starter