当前位置 博文首页 > xzh_blog:BeanUtils.copyProperties忽略异常、忽略空值

    xzh_blog:BeanUtils.copyProperties忽略异常、忽略空值

    作者:[db:作者] 时间:2021-08-23 19:11

    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);
                            }
                        }
                    }
                }
            }
        }
    }

    cs