List of usage examples for org.aspectj.apache.bcel.generic ClassGen setConstantPool
public void setConstantPool(ConstantPool constant_pool)
From source file:br.jabuti.probe.desktop.DefaultProbeInsert.java
License:Open Source License
private JavaClass doDefaultInstrument(JavaClass java_class, String className) throws InvalidInstructionException, InvalidStackArgument { ClassGen cg = new ClassGen(java_class); ConstantPoolGen cp = cg.getConstantPool(); Method[] methods = cg.getMethods(); // System.out.println( "Instrumenting class: " + className + // " cfg option: " + typeOfCFG ); for (int i = 0; i < methods.length; i++) { try {// w w w.j a v a 2 s . c om MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cp); // System.out.println( "\tCurrent method: " + mg.getName() ); // does not instrument static initializations or abstract methods if ((methods[i].getName().equals("<clinit>")) || (methods[i].isAbstract())) { continue; } InstructionList il = mg.getInstructionList(); InstructionHandle[] ihVec = il.getInstructionHandles(); int[] ihOffset = il.getInstructionPositions(); ASMInstrumenter gi = new ASMInstrumenter(mg, cg, cp); int nextLocal = mg.getMaxLocals() + 1; CFG gfc = new CFG(mg, cg); // gfc.releaseInstructionGraph(); // free some memory HashSet jahFoi = new HashSet(); // controla quas nos jah foram instrumentados // insert probes at the end of each node in a constructor if (methods[i].getName().equals("<init>")) { CFGNode superNode = null; // acha a chamada ao super for (int m = 0; m < gfc.size(); m++) { CFGNode ey = (CFGNode) gfc.elementAt(m); if (ey instanceof CFGSuperNode) { superNode = ey; break; } } // acha os nos que veem antes da chamada ao super // System.out.println( "Instrumentando classe: " + cg.getClassName() ); // System.out.println( gfc ); gfc.findIDFT(false, superNode); InstructionHandle ih = InstructionList.findHandle(ihVec, ihOffset, ihOffset.length, superNode.getEnd()); gi.insertAfter(ih, "aload_0 " + // empilha o objeto " ldc \"" + className + "\"" + // empilha o nome da classe " ldc " + i + // empilha o numero do metodo " lconst_0 " + // empilha aninhamento que para construtor // eh sempre 0 " ldc " + // empilha o numero do no "\"" + superNode.getNumber() + "\" " + "invokestatic " + getProbeClass() + " probe \"(Ljava/lang/Object;" + "Ljava/lang/String;IJLjava/lang/Object;)V\""); jahFoi.add(ih); // coloca a instrumentacao em cada no for (int m = 0; m < gfc.size(); m++) { CFGNode ey = (CFGNode) gfc.elementAt(m); // if marked means it is befor the super if (ey.getMark()) { continue; } ih = InstructionList.findHandle(ihVec, ihOffset, ihOffset.length, ey.getStart()); // System.out.println("No: " + ey); // System.out.println("Inicio do noh: " + ih); // System.out.println("Inicio do noh: " + ey.getStart()); if (!jahFoi.contains(ih)) { jahFoi.add(ih); gi.insertBefore(ih, "aload_0 " + // empilha o objeto " ldc \"" + className + "\"" + // empilha o nome da classe " ldc " + i + // empilha o numero do metodo " lconst_0 " + // empilha aninhamento que para construtor // eh sempre 0 " ldc " + // empilha o numero do no "\"" + ey.getNumber() + "\" " + "invokestatic " + getProbeClass() + " probe \"(Ljava/lang/Object;" + "Ljava/lang/String;IJLjava/lang/Object;)V\""); } } } else { // in this case the method is an ordinary method String probStat = null, method = null; if (mg.isStatic()) { probStat = ""; method = ""; } else { probStat = "aload_0"; method = "Ljava/lang/Object;"; } for (int m = 0; m < gfc.size(); m++) { CFGNode gn = (CFGNode) gfc.elementAt(m); InstructionHandle ih = InstructionList.findHandle(ihVec, ihOffset, ihOffset.length, gn.getStart()); if (gfc.isEntryNode(gn)) { String s = "invokestatic " + getProbeClass() + " getNest \"()J\"" + " lstore " + nextLocal; gi.addBefore(ih, s); } String s = probStat + " ldc \"" + className + "\"" + // empilha o nome da classe " ldc " + i + // empilha o numero do metodo " lload " + nextLocal + // empilha nivel de aninhamento " ldc " + // empilha o numero do no "\"" + gn.getNumber() + "\" " + "invokestatic " + getProbeClass() + " probe \"(" + method + "Ljava/lang/String;IJLjava/lang/Object;)V\""; if (!jahFoi.contains(ih)) { jahFoi.add(ih); gi.insertBefore(ih, s); } } int stackSize = mg.getMaxStack(); // Tentativa de contornar os erros do BCEL que n<E3>o altera corretamente o // tamanho da pilha if (stackSize < 6) { mg.setMaxStack(stackSize + 6); } } methods[i] = mg.getMethod(); } catch (ParseException e) { System.out.println(className); System.err.println("Parser error " + e.getMessage()); } } int newIndex = cp.addUtf8(JABUTI_DEFAULT_INSTR_ATTRIBUTE); Attribute atr = new Unknown(newIndex, JABUTI_DEFAULT_INSTR_ATTRIBUTE.length(), JABUTI_DEFAULT_INSTR_ATTRIBUTE.getBytes(), cp.getConstantPool()); cg.setConstantPool(cp); cg.addAttribute(atr); cg.setMethods(methods); return (cg.getJavaClass()); }
From source file:br.jabuti.probe.mobiledevice.HostProbeInsert.java
License:Open Source License
protected JavaClass doClassInstrument(JavaClass java_class) throws InvalidInstructionException, InvalidStackArgument { ClassGen cg = new ClassGen(java_class); // procura a entrada da classe mucode.abstractions.MuAgent e // substitui por mobility.abstractions.MuAgent ConstantPoolGen cp = cg.getConstantPool(); int oldIndex = cp.lookupClass(MUCODE_AGENT); if (oldIndex >= 0) { int newIndex = cp.addUtf8(MY_AGENT_UTF8); ConstantClass oldConst = (ConstantClass) cp.getConstant(oldIndex); oldConst.setNameIndex(newIndex); cp.setConstant(oldIndex, oldConst); cg.setConstantPool(cp); }// ww w . ja va 2s .com int newIndex = cp.addUtf8(JABUTI_HOST_INSTR_ATTRIBUTE); Attribute atr = new Unknown(newIndex, JABUTI_HOST_INSTR_ATTRIBUTE.length(), JABUTI_HOST_INSTR_ATTRIBUTE.getBytes(), cp.getConstantPool()); cg.setConstantPool(cp); cg.addAttribute(atr); return (cg.getJavaClass()); }
From source file:br.jabuti.probe.mobiledevice.HostProberInstrum.java
License:Open Source License
/** * Instrumenta a classe base, methodo Main *//* w w w . ja v a 2s. c o m*/ public static JavaClass instrumentMain(JavaClass jv, String hostName, String projName, int delay) throws Exception { ClassGen cg = new ClassGen(jv); ConstantPoolGen cp = cg.getConstantPool(); Method[] methods = cg.getMethods(); int i = 0; for (i = 0; i < methods.length; i++) if (methods[i].getName().equals("main") && methods[i].getSignature().equals("([Ljava/lang/String;)V")) { break; } if (i >= methods.length) { System.out.println("Method static public main(String[]) not found"); System.exit(0); } MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cp); InstructionList il = mg.getInstructionList(); InstructionHandle last = il.getStart(); // InstructionHandle pen = last.getPrev(); // il.delete(il.getStart(), pen); ASMInstrumenter gi = new ASMInstrumenter(mg, cg, cp); gi.insertBefore(last, " ldc \"" + hostName + "\"" + // empilha o endereco do servidor de teste " ldc \"" + projName + "\"" + // empilha o nome projeto " ldc " + delay + // empilha o tempo de espera "aconst_null " + "invokestatic br.jabuti.probe.mobiledevice.mobile.HostProber" + " init \"(Ljava/lang/String;Ljava/lang/String;ILmucode/MuServer;)V\""); methods[i] = mg.getMethod(); cg.setConstantPool(cp); cg.setMethods(methods); return (cg.getJavaClass()); }