private static void copyPropertiesByNotNull(Object source, Object target, List<String> copyProperties)
throws InvocationTargetException, IllegalAccessException
{
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
for (PropertyDescriptor targetPd : targetPds)
{
//只复制需要的字段,字段名称包含枚举值时就需要复制值
if (copyProperties.stream().anyMatch(vo -> targetPd.getName().toLowerCase().contains(vo.toLowerCase())))
{
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
Object sourceValue = sourcePd.getReadMethod().invoke(source);
Object targetValue = targetPd.getReadMethod().invoke(target);
//只有目标对象属性没值并且源对象属性有值才进行复制操作
if (targetValue == null && sourceValue != null)
{
targetPd.getWriteMethod().invoke(target, sourcePd.getReadMethod().invoke(source));
}
}
}
}
转载于:https://my.oschina.net/u/3768722/blog/3062991