Example usage for org.aspectj.apache.bcel.generic InstructionList getStart

List of usage examples for org.aspectj.apache.bcel.generic InstructionList getStart

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.generic InstructionList getStart.

Prototype

public InstructionHandle getStart() 

Source Link

Usage

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 a  va  2s .c  om*/
 * @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());
}

From source file:br.jabuti.probe.mobiledevice.HostProberInstrum.java

License:Open Source License

/**
 *  Instrumenta a classe base, methodo Main
 *//*ww  w. j  a v a  2 s .  com*/
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());

}