java的反射机制属实强大,能解决好些问题
在接手别人写的代码的时候,有一个bean类的get方法特别low,我都看不下去
重复代码写五遍,我都觉得太不合理。之后将其中代码抽取出来修改了下。
public List>getNewAnswer() {
List> temp = new ArrayList>();
Map mapA = new HashMap();
Map mapB = new HashMap();
Map mapC = new HashMap();
Map mapD = new HashMap();
Map mapE = new HashMap();
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);returntemp;
}
将其中的相同代码抽出来有个问题,对于调用方法可咋整。
比如getAnswerA getAnswerB,这怎么动态调用。反射这个时候就用到了。
public MapgetAnswerMap(String option,Boolean f2){
Map map = new HashMap();
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();
}returnmap;
}
getNewAnswer就变成了这
public List>getNewAnswer() {
List> temp = new ArrayList>();
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));returntemp;
}
如此简练明了。
之前使用的反射也挺多,有时候根据对应的全类名来获取,这个可用性很大。new对象的话再getclass最多就是用在别人封装好的方法
这次使用的时候蒙了,从数据库查出来怎么在本类使用反射,this.getClass获取到的是空啊。一开始是根据属性取值,最后想了下,还没有调用set方法怎么能有值
最后使用
Method method = QuestionsDto.class.getDeclaredMethod(f, null);
method.setAccessible(true);
Object object= method.invoke(this, null);
直接传this即可。
之前做过一个跨域审批的功能,对于现有的类进行操作,没问题,之后让我修改为通用的,针对于不同的类也要求适用,蒙了。
这个时候反射又用到了,可以将bean类放入固定的包名下面,只有根据数据库的表明以及对应包的路径名称来获取到全类名,之后获取到对应的字节码文件之后
获取实例获取对应的属性,进行一系列的操作。
反射用处着实很大。