如何优化自己写的烂代码路程之一,不同实体list之间的复制?

为什么要有这篇文章?

        前段时间写导入功能,因为导入的数据来源有多种,所以在导入表(叫做A表)之前做了个前置表,前置表(叫做B表)比要导入的表多了个字段用于区分数据来源。A表和B表大部分字段相同,但也有细微的一些区别。整体导入的做法是,我通过工具类+注解的方式获取excel表格里面的数据内容,然后通过list集合和map集合把所有要做的校验查出来。在循环里面去做每个字段的校验,把错误的实体copy下来,最后就是set操作,然后copy主子表的list去批量插入数据库。这个是整体的逻辑。

实现

        首先用的是BeanUtils的copyproperties方法,但是copy后都是空值。

        最后历经一点小挫折的实现方式,献上代码

public class BeanConverUtil {
    /**
     * 单个类转换
     *
     * @param sourceObj
     * @param targetClass
     * @param <T>
     * @return
     */
    public static <T> T conver(Object sourceObj, Class<T> targetClass) {
        if (sourceObj == null || targetClass == null) {
            return null;
        }
        T targetObj = null;
        try {
            targetObj = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("sourceObj:{},targetClass:{}", sourceObj, targetClass, e);
            return null;
        }
        BeanUtils.copyProperties(sourceObj, targetObj);
        return targetObj;
    }

    /**
     * List之间转换
     *
     * @param sourceList
     * @param targetClass
     * @param <T>
     * @return
     */
    public static <T> List<T> converList(List<?> sourceList, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(sourceList) || targetClass == null) {
            return Collections.emptyList();
        }
        return sourceList.stream().map(sourceObj -> conver(sourceObj, targetClass)).collect(Collectors.toList());
    }
}

看到这里我的理解就是BeanUtil的copyproperties只能copy单个实体,但是具体是不是我还是需要去看一下具体的实现是怎么样的,那就上代码(作者只截取了copyproperties的一段代码)

private static void copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        if (editable != null) {
            if (!editable.isInstance(target)) {
                throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
            }

            actualEditable = editable;
        }

        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        PropertyDescriptor[] var7 = targetPds;
        int var8 = targetPds.length;

        for(int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = var7[var9];
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                PropertyDescriptor sourcePd = 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 (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }

                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }

    }
解读
  • 这个方法有四个参数,分别是数据源,数据归宿,可编辑类,忽略属性
  • 在方法里,首先校验了数据源,数据归宿不能为空,获取数据归宿对象的数据描述符数组,如果制定了要忽略的属性,将其转换成一个list,通过遍历目标对象的属性描述符数组,依次处理每个属性。
  • 对于每个属性,首先获取其对应的写入方法(单个属性的set,我们可以先这样理解),如果该方法不存在则忽略(这一点就是两个不同实体能copy的要点)
  • 获取源对象的读取方法(单个属性的get)并进行一些校验,例如判断写入的参数类型是否可赋值给读取方法的返回类型,如果通过,可以通过反射调用读取方法获取源对象的属性值
  • 最后通过反射调用写入方法,将源对象的属性值设置到目标对象中,如果在这个过程中出现异常,将抛出一个FatalBeanException异常,标识无法完成属性的复制操作。
结尾

        给自己加个油,希望我编写代码越来越好,越来越实用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值