前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java反射,代码优化

java反射,代码优化

作者头像
陈灬大灬海
发布2018-09-12 15:40:14
3800
发布2018-09-12 15:40:14
举报

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类放入固定的包名下面,只有根据数据库的表明以及对应包的路径名称来获取到全类名,之后获取到对应的字节码文件之后

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

反射用处着实很大。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档