zoukankan      html  css  js  c++  java
  • java反射,代码优化

    java的反射机制属实强大,能解决好些问题

    在接手别人写的代码的时候,有一个bean类的get方法特别low,我都看不下去

    重复代码写五遍,我都觉得太不合理。之后将其中代码抽取出来修改了下。

    public List<Map<String, String>> getNewAnswer() {
            List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
            Map<String, String> mapA = new HashMap<String, String>();
            Map<String, String> mapB = new HashMap<String, String>();
            Map<String, String> mapC = new HashMap<String, String>();
            Map<String, String> mapD = new HashMap<String, String>();
            Map<String, String> mapE = new HashMap<String, String>();
    
            String em = getExamMine();
            Boolean ba, bb, bc, bd, be;
            ba = bb = bc = bd = be = false;
            if(!StringUtils.isEmpty(em)) {
                ba = em.indexOf("A") != -1;
                bb = em.indexOf("B") != -1;
                bc = em.indexOf("C") != -1;
                bd = em.indexOf("D") != -1;
                be = em.indexOf("E") != -1;
            }
            mapA.put("name", "A");
            //开始 改变图片路径
            Matcher m = p.matcher(getAnswerA());
            if(m.find()) {
                mapA.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
            }else{
                mapA.put("value", getAnswerA());
            }
            mapA.put("checked", ba.toString());
            mapB.put("name", "B");
            m = p.matcher(getAnswerB());
            if(m.find()) {
                mapB.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
            }else{
                mapB.put("value", getAnswerB());
            }
            mapB.put("checked", bb.toString());
            mapC.put("name", "C");
            m = p.matcher(getAnswerC());
            if(m.find()) {
                mapC.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
            }else{
                mapC.put("value", getAnswerC());
            }
            mapC.put("checked", bc.toString());
            mapD.put("name", "D");
            m = p.matcher(getAnswerD());
            if(m.find()) {
                mapD.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
            }else{
                mapD.put("value", getAnswerD());
            }
            mapD.put("checked", bd.toString());
            mapE.put("name", "E");
            m = p.matcher(getAnswerE());
            if(m.find()) {
                mapE.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
            }else{
                mapE.put("value", getAnswerE());
            }
            mapE.put("checked", be.toString());
            temp.add(mapA);
            temp.add(mapB);
            temp.add(mapC);
            temp.add(mapD);
            temp.add(mapE);
            return temp;
        }

    将其中的相同代码抽出来有个问题,对于调用方法可咋整。

    比如getAnswerA getAnswerB,这怎么动态调用。反射这个时候就用到了。

    public Map<String, String> getAnswerMap(String option,Boolean f2){
            Map<String, String> map = new HashMap<String, String>();    
            String em = getExamMine();    
            Boolean flag = false;
            if(!StringUtils.isEmpty(em)) {
                flag = em.indexOf(option) != -1;
            }
            if(f2){
                map.put("checked", String.valueOf(false));
            }else{
                map.put("checked", flag.toString());
            }
            map.put("name", option);
            System.out.println(getAnswerA());
            String f = "getAnswer"+option;
            try {
                Method method = QuestionsDto.class.getDeclaredMethod(f, null);
                method.setAccessible(true);
                Object object = method.invoke(this, null);
                Matcher m = p.matcher(object.toString());
                if(m.find()) {
                    map.put("img","/webq/"+this.productType+"/"+m.group().substring(1, m.group().length()-1));
                }else{
                    map.put("value", object.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }

    getNewAnswer就变成了这

    public List<Map<String, String>> getNewAnswer() {
            List<Map<String, String>> temp = new ArrayList<Map<String, String>>(); 
            temp.add(getAnswerMap("A",true));
            temp.add(getAnswerMap("B",true));
            temp.add(getAnswerMap("C",true));
            temp.add(getAnswerMap("D",true));
            temp.add(getAnswerMap("E",true));
            return temp;
        }

    如此简练明了。

    之前使用的反射也挺多,有时候根据对应的全类名来获取,这个可用性很大。new对象的话再getclass最多就是用在别人封装好的方法

    这次使用的时候蒙了,从数据库查出来怎么在本类使用反射,this.getClass获取到的是空啊。一开始是根据属性取值,最后想了下,还没有调用set方法怎么能有值

    最后使用

    Method method = QuestionsDto.class.getDeclaredMethod(f, null);
                method.setAccessible(true);
                Object object = method.invoke(this, null);

    直接传this即可。

    之前做过一个跨域审批的功能,对于现有的类进行操作,没问题,之后让我修改为通用的,针对于不同的类也要求适用,蒙了。

    这个时候反射又用到了,可以将bean类放入固定的包名下面,只有根据数据库的表明以及对应包的路径名称来获取到全类名,之后获取到对应的字节码文件之后

    获取实例获取对应的属性,进行一系列的操作。

    反射用处着实很大。

  • 相关阅读:
    AN BD FD DD CD UT CT TT IT ST UAT OP
    log4j
    重构代码
    with as sql的优化
    svn locked,并且无法clean up 的解决方法
    去掉Eclipse中没用的workspace
    getHibernateTemplate()的get方法
    Java反射
    windows 右键新建html文档
    fiddler 针对单个接口打断点
  • 原文地址:https://www.cnblogs.com/chywx/p/9458543.html
Copyright © 2011-2022 走看看