反射工作中的运用--减少重复的代码

      接上篇;另一个需求,若要保持历史数据不变,其中6天的数据来自历史数据,而剩下一天的数据来自数据库新的数据。那么我的近7天的记录就需要删除第一天的数据,加上最后一天的记录(新查询出来的)。

     由于列太多,我还是懒的一个一个写,所以想到用反射的方法,将实体中的所有get,set方法都获取到,然后遍历get方法,remove掉第一个(index=0),遍历set方法,将准备好的数据设置到属性上,该数据的获取,通过获取get方法获取到前6天的数据+最后一天的数据。

     获取的方式就是,若是remove删除,获取this的类。然后从class中获取所有属性(字段)(其中$assertionsDisabled是多余的),然后根据字段获取属性描述PropertyDescriptor,从属性描述获取getReadMethod,所有get方法。然后对于getTitle这个不用去掉数据,只用处理其他5个list就好。 

      核心代码就是

           obj = (List) getMethod.invoke(this);

           obj.remove(index);

     执行get方法,获取到值,然后移除掉List中指定的index的数据。

public ProductDetailVO remove(int index) {
        Class<? extends ProductDetailVO> testClass = this.getClass();
        //获得属性
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.getName().equals("$assertionsDisabled")) {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(field.getName(), testClass);
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                }
                //获得get方法
                assert pd != null;
                Method getMethod = pd.getReadMethod();
                //执行get方法返回一个Object
                if (!getMethod.getName().equals("getTitle")) {
                    List obj;
                    try {
                        obj = (List) getMethod.invoke(this);
                        obj.remove(index);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

        return this;
    }
      而set方法也基本类似,核心代码就是,执行当前实体的get方法,然后执行新的数据的get方法,然后把这两个值加一起,最后执行set方法,返回一个新的结果。

      obj = (List) getMethod.invoke(this);
      voObj = (List) getMethod.invoke(vo);
      obj.addAll(voObj);
      setMethod.invoke(this,obj);

 public ProductDetailVO add(List<ProductDetailVO> voList) {
        Class<? extends ProductDetailVO> testClass = this.getClass();
        //获得属性
        Field[] fields = this.getClass().getDeclaredFields();
        ProductDetailVO vo = voList.stream().filter(a -> a.getTitle().equals(this.getTitle())).findFirst().get();
        for (Field field : fields) {
            if (!field.getName().equals("$assertionsDisabled")) {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(field.getName(), testClass);
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                }
                //获得get方法
                assert pd != null;
                Method getMethod = pd.getReadMethod();
                Method setMethod = pd.getWriteMethod();
                //执行get方法返回一个Object
                if (!getMethod.getName().equals("getTitle")) {
                    List obj = null;
                    List voObj;
                    try {
                        obj = (List) getMethod.invoke(this);
                        voObj = (List) getMethod.invoke(vo);
                        obj.addAll(voObj);
                        setMethod.invoke(this,obj);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return this;
    }
    这样就不用给每个list都写一个remove方法,一个add方法,那样会写很多代码的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值