按条件复制对象中的值(vo -> vo)复制Beanutils.copyProperties()方法

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));
                }
            }
        }
    }
相关文章
相关标签/搜索