Skip to content

Instantly share code, notes, and snippets.

@xcaspar
Created July 20, 2015 08:42
Show Gist options
  • Save xcaspar/bf8d8bd891ead2dec3d3 to your computer and use it in GitHub Desktop.
Save xcaspar/bf8d8bd891ead2dec3d3 to your computer and use it in GitHub Desktop.

BeanUtils.copyProperties方法问题

之前使用流式build模式来创建bean对象,但是在使用BeanUtils.copyProperties方法时出现异常。
查看此方法的源码发现,此方法会对源对象和目的对象做一次可读可写判断:

getPropertyUtils().isReadable(orig, name) && getPropertyUtils().isWriteable(dest, name)

如果操作的对象不符合JAVA BEAN规范,就会返回false.
在PropertyDescriptor类的getWriteMethod方法中有此判断:

if (writeMethod != null) { 
    if (!writeMethod.getReturnType().equals(void.class)) { 
        writeMethod = null; 
    } 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment