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類放入固定的包名下面,只有根據數據庫的表明以及對應包的路徑名稱來獲取到全類名,之后獲取到對應的字節碼文件之后
獲取實例獲取對應的屬性,進行一系列的操作。
反射用處著實很大。
轉載于:https://www.cnblogs.com/chywx/p/9458543.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java反射,代码优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cookie的生命周期问题
- 下一篇: javascript闭包,你大爷永远是你