BeanCopier 进行copy时如何对源对象为null的属性不进行拷贝?

解决方案 »

  1.   

        public static String[] getNullPropertyNames (Object source) {
            final BeanWrapper src = new BeanWrapperImpl(source);
            java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();        Set<String> emptyNames = new HashSet<String>();
            for(java.beans.PropertyDescriptor pd : pds) {
                Object srcValue = src.getPropertyValue(pd.getName());
                if (srcValue == null) emptyNames.add(pd.getName());
            }
            String[] result = new String[emptyNames.size()];
            return emptyNames.toArray(result);
        }    public static void copyPropertiesIgnoreNull(Object src, Object target){
            BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
        }
      

  2.   

    首先感谢您的问答,但这是 BeanUtils ,我想问的是BeanCopier ,
      

  3.   

    private static final Map<String, BeanCopier> beanCopierCache = new ConcurrentHashMap<>();
        private static String generateKey(Class<?> class1, Class<?> class2) {
            return class1.toString() + class2.toString();
        }
        
        private static BeanCopier getBeanCopier(Class<?> sourceClass, Class<?> targetClass) {
            String beanKey = generateKey(sourceClass, targetClass);
            BeanCopier copier = null;
            if (!beanCopierCache.containsKey(beanKey)) {
                copier = BeanCopier.create(sourceClass, targetClass, true);
                beanCopierCache.put(beanKey, copier);
            } else {
                copier = beanCopierCache.get(beanKey);
            }
            return copier;
        }
        

    public interface Convert<T,E>{
    public Return<T> convert(T t,E e);
    }

    public static <T,E> T convert(Class<T> cls,E e) {
    return convert(cls,e,null).getRetObject();
    }

    public static <T,E> Return<T> convert(Class<T> cls,E e,Convert<T,E> convert) {
    if(e == null) {
    return null;
    }
    T t = (T) ConstructorAccess.get(cls).newInstance();
    BeanCopier copier = getBeanCopier(e.getClass(), t.getClass());
    copier.copy(e, t,null);
    if(convert != null) {
    return convert.convert(t,e);
    }
    return new Return<>(t);
    }

    public static void copy(Object target,Object source) {
    copy(target,source,false);
    }

    public static void copy(Object target,Object source,boolean cover) {
    BeanCopier copier = getBeanCopier(source.getClass(), target.getClass());
    copier.copy(source,target, (value, t, context)->{
    if(!cover && value == null) {
    return BeanMap.create(target).get(getPropertyName(String.valueOf(context)));
    }
    return value;
    });


    private static String getPropertyName(String methodName) {//setAge ---> age
    char[] newChar = new char[methodName.length() - 3];
    System.arraycopy(methodName.toCharArray(), 3, newChar, 0, methodName.length() - 3);
    newChar[0] = Character.toLowerCase(newChar[0]);
    return String.valueOf(newChar);
    }