BeanUtils.copyProperties忽略异常忽略空值

Posted 一个抓手

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BeanUtils.copyProperties忽略异常忽略空值相关的知识,希望对你有一定的参考价值。

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

/**
 * Bean属性拷贝工具
 *
 * @author 向振华
 * @date 2021/08/11 14:22
 */
@Slf4j
public class BeanUtils {

    public static void copyProperties(Object source, Object target) {
        copyProperties(source, target, true, (String[]) null);
    }

    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        copyProperties(source, target, true, ignoreProperties);
    }

    public static void copyProperties(Object source, Object target, boolean ignoreNullValue, String... ignoreProperties) {
        if (source == null || target == null) {
            log.error("Source or Target must not be null");
            return;
        }
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (ignoreNullValue && value == null) {
                                continue;
                            }
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Exception e) {
                            log.error("Could not copy property '" + targetPd.getName() + "' from source to target", e);
                        }
                    }
                }
            }
        }
    }
}

以上是关于BeanUtils.copyProperties忽略异常忽略空值的主要内容,如果未能解决你的问题,请参考以下文章

BeanUtils.copyProperties的用法

BeanUtils.copyProperties的用法

关于BeanUtils.copyProperties()用法和区别

使用 BeanUtils.copyProperties 复制特定字段?

BeanUtils.copyProperties VS PropertyUtils.copyProperties

Spring 之 BeanUtils.copyProperties(...) 源码简读