List of usage examples for org.aspectj.apache.bcel.generic InstructionList getEnd
public InstructionHandle getEnd()
From source file:br.jabuti.probe.desktop.DefaultProbeInsert.java
License:Open Source License
/** * This method wraps a given method code between two pieces of code: one to be executed before * the method and other after, as a finaly clause. The instrumentation will use the next 2 free * local variable, so the code to be inserted should not use them. * /*from w w w. j av a 2 s . c o m*/ * @param jv - The JavaClass where to find the method * @param name - the method name * @param sig - method signature * @param cfg - the control flow graph of the method * @param before - the code to be inserted before the method code * @param after - the code to be inserted after the method */ static public JavaClass wrapMethod(JavaClass jv, String name, String sig, // CFG cfg, String before, String after) { ClassGen cg = new ClassGen(jv); ConstantPoolGen cp = cg.getConstantPool(); Method[] methods = cg.getMethods(); int i; for (i = 0; i < methods.length; i++) { String n = methods[i].getName(); String s = methods[i].getSignature(); if (s.equals(sig) && n.equals(name)) break; } if (i >= methods.length) return jv; MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cp); try { int nextLocal = mg.getMaxLocals(); ASMInstrumenter gi = new ASMInstrumenter(mg, cg, cp); InstructionList iList = mg.getInstructionList(); // pega instrucao inicial InstructionHandle first = iList.getStart(); // System.out.println("First: " + first); // System.out.println(iList); gi.insertBefore(first, " NOP " + (before == null ? "" : before)); // System.out.println(iList); // iList = mg.getInstructionList(); InstructionHandle preambulo = iList.getStart(); // System.out.println("Preambulo: " + preambulo); // ultima instrucao do metodo InstructionHandle last = iList.getEnd(); // System.out.println("Last: " + last); // comeca a inserir o tratador de instrucoes gi.insertAfter(last, " astore " + (nextLocal + 1)); // iList = mg.getInstructionList(); InstructionHandle catcher = iList.getEnd(); gi.insertAfter(catcher, " jsr l1 aload " + (nextLocal + 1) + " athrow " + " l1: NOP "); // System.out.println("catcher: " + catcher); // comeca a inserir o codigo do finally // iList = mg.getInstructionList(); InstructionHandle subroutine = iList.getEnd(); // System.out.println("subroutine: " + subroutine); String finali = " astore " + nextLocal; finali += after; finali += " ret " + nextLocal; gi.insertAfter(subroutine, finali); // iList = mg.getInstructionList(); InstructionHandle nx = first, curr; mg.getMethod(); // para cada return no codigo faz uma chamada aa subrotina do { curr = nx; BranchInstruction jsr = new JSR(subroutine); Instruction ins = curr.getInstruction(); if (ins instanceof ReturnInstruction) { gi.insertBefore(curr, jsr); } nx = curr.getNext(); } while (curr != last); // e finalmente, coloca o tratador de excees para todo // o cdigo orignal iList = mg.getInstructionList(); mg.addExceptionHandler(preambulo, last, catcher, (ObjectType) null); } catch (ParseException e) { System.err.println("Parser error " + e.getMessage()); } mg.setMaxStack(); mg.setMaxLocals(); methods[i] = mg.getMethod(); cg.setMethods(methods); return (cg.getJavaClass()); }