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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuyanlinglanq/article/details/79188433

      接上篇;另一个需求,若要保持历史数据不变,其中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方法,那样会写很多代码的。

猜你喜欢

转载自blog.csdn.net/liuyanlinglanq/article/details/79188433