BeanUtils拷贝忽略空值

动机:

BeanUtils是将一个对象拷贝到另外一个对象上去,但是如果source对象中如果有null值,会将原对象中有值的属性覆盖,这不是我们所希望的,我们只想拷贝非null的属性,所以写了以下工具类,注意:以下工具类没有实现深拷贝,只是浅拷贝能去除null

package com.tangyaya8.mmall.utils;

import com.google.common.collect.Lists;
import com.tangyaya8.mmall.pojo.User;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author tangxuejun
 * @version 2019-05-10 18:16
 */
public class CopyUtils {
    public static void copyBeanIgnoreNull(Object source, Object target, String... ignore) throws Exception {
        Objects.requireNonNull(source);
        Objects.requireNonNull(target);
        List<String> ignoreCopyProperties = Lists.newArrayList();
        if (ignore != null) {
            ignoreCopyProperties.addAll(Arrays.asList(ignore));
        }
        Field[] fields = source.getClass().getDeclaredFields();
        for (int i = fields.length - 1; i >= 0; i--) {
            Field field = fields[i];
            //打开私有访问
            boolean canAccess = field.canAccess(source);
            if (!canAccess) {
                field.setAccessible(true);
            }
            Object fieldValue = field.get(source);
            if (fieldValue == null) {
                ignoreCopyProperties.add(field.getName());
            }
            field.setAccessible(canAccess);
        }
        BeanUtils.copyProperties(source, target, ignoreCopyProperties.toArray(new String[]{}));
    }

    public static void main(String[] args) throws Exception {
        User user = new User();
        User user1 = new User();
        user.setUserName("tangbaobao");
        user1.setUserName("tangdandan");
        user1.setPassword("meme");
        copyBeanIgnoreNull(user,user1);
        System.out.println(user1);
    }
}

你可能感兴趣的:(问题总结)