Example usage for org.aspectj.apache.bcel.generic MethodGen getInstructionList

List of usage examples for org.aspectj.apache.bcel.generic MethodGen getInstructionList

Introduction

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

Prototype

public InstructionList getInstructionList() 

Source Link

Usage

From source file:br.jabuti.graph.datastructure.ig.InstructionGraph.java

License:Open Source License

/** <P>Constructor that creates a graph from a method code<br></p>
        /*from w  w  w  . j av a2  s  .  c  o m*/
 @param mg The method for wich the graph will be created
 */
public InstructionGraph(MethodGen mg) {
    meth = mg;
    il = mg.getInstructionList();
    if (il == null) {
        return;
    } // no code (abstract or native method)

    // cria os nos do grafo e
    // coloca uma instrucao em cada no do grafo

    Hashtable verify = new Hashtable(); // serve para achar o no do grafo
    // dada uma certa instrucao.
    InstructionHandle ih = null;

    for (ih = il.getStart(); ih != null; ih = ih.getNext()) {
        InstructionNode in = new InstructionNode(ih);

        add(in);
        verify.put(ih, in);
    }

    HashMap jumpsAndEntries = new HashMap();
    CodeExceptionGen[] ceg = meth.getExceptionHandlers();

    // acerta os edges principais e secundarios de cada no
    for (int i = 0; i < size(); i++) {
        // include edges from each node to its exception handlers
        InstructionNode vf = (InstructionNode) elementAt(i);

        for (int j = 0; j < ceg.length; j++) {
            int init, fim;

            init = ceg[j].getStartPC().getPosition();
            fim = ceg[j].getEndPC().getPosition();
            if (vf.ih.getPosition() >= init && vf.ih.getPosition() <= fim) {
                addSecondaryEdge(vf, (InstructionNode) verify.get(ceg[j].getHandlerPC()));
            }
        }

        // depois acerta os edges principais.
        InstructionHandle[] nx = InstructCtrl.nextToExec(vf.ih);

        if (vf.ih.getInstruction() instanceof JsrInstruction) {
            // Coloca o proximo fisico como proximo no grafo
            InstructionHandle nih = (InstructionHandle) vf.ih.getNext();

            addPrimaryEdge(vf, (GraphNode) verify.get(nih));
            // mapeamento JSR -- Entrada
            jumpsAndEntries.put(vf.ih, nx[0]);
        } else {
            for (int j = 0; j < nx.length; j++) {
                addPrimaryEdge(vf, (GraphNode) verify.get(nx[j]));
            }
        }
    }

    int lastsize = 0, cursize = 0;
    int cont;
    Vector ventry = new Vector();

    ventry.add(verify.get(il.getStart()));
    Hashtable entriesDom = new Hashtable();

    // aqui calcula os nos dominados por cada entrada;
    do {
        cont = 0;
        lastsize = cursize;
        cursize = ventry.size();
        for (int i = lastsize; i < cursize; i++) {
            InstructionNode entr = (InstructionNode) ventry.elementAt(i);

            setEntryNode(entr);
            // Calcula os dominators
            RRDominator rrd = new RRDominator("Dominator");

            roundRobinAlgorithm(rrd, true);
            HashSet edm = new HashSet();

            entriesDom.put(entr, edm);
            for (int j = 0; j < size(); j++) {
                InstructionNode in = (InstructionNode) elementAt(j);
                HashSet dom = (HashSet) in.getUserData("Dominator");

                if (dom != null && dom.contains(entr)) {
                    edm.add(in);
                    if (in.ih.getInstruction() instanceof JsrInstruction) {
                        ventry.add(verify.get(jumpsAndEntries.get(in.ih)));
                        cont++;
                    }
                }
            }
        }

    } while (cont > 0);

    InstructionNode[] entries = (InstructionNode[]) ventry.toArray(new InstructionNode[0]);

    Vector aux = new Vector();

    aux.addAll((HashSet) entriesDom.get(entries[0]));
    lastsize = 0;
    cursize = 0;

    do {
        cont = 0;
        lastsize = cursize;
        cursize = aux.size();

        for (int i = lastsize; i < cursize; i++) {
            InstructionNode isJmp = (InstructionNode) aux.elementAt(i);

            if (!(isJmp.ih.getInstruction() instanceof JsrInstruction)) {
                continue;
            }
            InstructionNode retTarget = (InstructionNode) getArrivingNodesByPrimaryEdge(isJmp).iterator()
                    .next();

            removePrimaryEdge(isJmp, retTarget);
            InstructionNode retNode = null;

            cont++;
            // achou JSR. deve duplicar o seu destino
            // entr eh o no destino do JSP
            InstructionHandle ihEntr = (InstructionHandle) jumpsAndEntries.get(isJmp.ih);
            InstructionNode entr = (InstructionNode) verify.get(ihEntr);
            // entrSet eh o conjunto de nos dominados pelo no de entrada
            HashSet entrSet = (HashSet) entriesDom.get(entr);
            Iterator in = entrSet.iterator();
            Hashtable auxVerify = new Hashtable();

            while (in.hasNext()) { // duplica cada elemento no conjunto
                InstructionNode inSet = (InstructionNode) in.next();
                InstructionNode newNode = new InstructionNode(inSet.ih);

                // insere no grapho
                add(newNode);

                // seta qual eh o jsr correspondente
                newNode.setDomEntry(isJmp);

                aux.add(newNode);
                auxVerify.put(inSet, newNode);
                if (newNode.ih.getInstruction() instanceof RET) {
                    retNode = newNode;
                }
            }

            // agora precisa fazer as ligacoes
            in = entrSet.iterator();
            while (in.hasNext()) {
                InstructionNode inSet = (InstructionNode) in.next();
                InstructionNode newNode = (InstructionNode) auxVerify.get(inSet);
                GraphNode[] nx = (GraphNode[]) getArrivingNodesByPrimaryEdge(inSet).toArray(new GraphNode[0]);

                for (int j = 0; j < nx.length; j++) {
                    InstructionNode nxin = (InstructionNode) auxVerify.get(nx[j]);

                    addPrimaryEdge(newNode, nxin);
                }
                nx = (GraphNode[]) getArrivingNodesBySecondaryEdge(inSet).toArray(new GraphNode[0]);
                for (int j = 0; j < nx.length; j++) {
                    InstructionNode nxin = (InstructionNode) auxVerify.get(nx[j]);

                    // se nxin eh null significa que o no nao estah no mesmo
                    // conjunto. Para corrigir isso no final eh feito
                    // tratamento especial (ver *** )
                    if (nxin != null) {
                        addSecondaryEdge(newNode, nxin);
                    }
                }
            }
            // liga agora o JSR e o RET, se existir
            addPrimaryEdge(isJmp, (InstructionNode) auxVerify.get(entr));
            if (retNode != null) {
                addPrimaryEdge(retNode, retTarget);
            }
        }
    } while (cont > 0);

    // Faz interseccao com vetor aux
    for (int i = 1; i < entries.length; i++) {
        HashSet edm = (HashSet) entriesDom.get(entries[i]);
        Iterator it = edm.iterator();

        while (it.hasNext()) {
            InstructionNode in = (InstructionNode) it.next();

            removeNode(in);
        }
    }

    // ***
    removeEntryNodes();
    setEntryNode(entries[0]);

    // acha Depth firs tree
    GraphNode[] dft = findDFTNodes(true);

    for (int i = 0; i < dft.length; i++) {
        // include edges from each node to its exception handlers
        InstructionNode vf = (InstructionNode) dft[i];

        nextException: for (int j = 0; j < ceg.length; j++) {
            int init, fim;

            init = ceg[j].getStartPC().getPosition();
            fim = ceg[j].getEndPC().getPosition();
            InstructionHandle handler = ceg[j].getHandlerPC();

            if (vf.ih.getPosition() >= init && vf.ih.getPosition() <= fim) {
                Set<GraphNode> vnx = getArrivingNodesBySecondaryEdge(vf);
                Iterator<GraphNode> k = vnx.iterator();
                while (k.hasNext()) {
                    InstructionNode nx = (InstructionNode) k.next();
                    if (nx.ih == handler) {
                        continue nextException;
                    }
                }

                // procura quem trata da interrupo
                for (int l = vnx.size() - 1; l >= 0; l--) {
                    InstructionNode dftk = (InstructionNode) dft[l];
                    if (dftk.ih.getPosition() >= init && dftk.ih.getPosition() <= fim) {
                        Set<GraphNode> vx = getArrivingNodesBySecondaryEdge(dft[l]);
                        Iterator<GraphNode> z = vx.iterator();
                        InstructionNode hdl = null;
                        while (z.hasNext()) {
                            hdl = (InstructionNode) z.next();
                            if (hdl.ih == handler) {
                                break;
                            }
                        }
                        vf.addSecNext(hdl);
                        break;
                    }
                }
            }

        }
    }
}

From source file:br.jabuti.graph.datastructure.ig.InstructionGraph.java

License:Open Source License

/** A driver for testing the class. Creates the graph and calls
 * {@link InstructionGraph#calcStack}. The arguments determine
 * to which methods to apply.//from  w  ww. ja v a2  s  .com
 *
 * @param args[0] A file name. Can be a classfile, a jar file or a
 * zip file. If jar or zip, the second and third arguments does not apply.
 * In this case, the output is only the name of the classes and of the
 * methods in the class. The test is applyied in all the listed
 * methods. If this is a single class file name, the output will be
 * the complete graph (as presented by {InstructionNode#print}) for
 * each selected method.
 * @param args[1] The name of a method. The test is applyed to all
 * methods in the class that match this name.
 * @param args[2] The signature of a method. Used to select one
 * between several homonymous methods.
 */

public static void main(String args[]) throws Exception {
    boolean all = true;
    String filename = args[0];
    ZipFile jf = null;

    if (filename.endsWith(".jar")) {
        jf = new JarFile(filename);
    } else if (filename.endsWith(".zip")) {
        jf = new ZipFile(filename);
    }

    if (jf == null) {
        JavaClass java_class;

        java_class = new ClassParser(filename).parse(); // May throw IOException

        ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
        Method[] methods = java_class.getMethods();

        for (int i = 0; i < methods.length; i++) {
            if (args.length >= 2 && (!args[1].equals(methods[i].getName()))) {
                continue;
            }
            if (args.length >= 3 && (!args[2].equals(methods[i].getSignature()))) {
                continue;
            }
            System.out.println("--------------------------");
            System.out.println(methods[i].getName());
            System.out.println(methods[i].getSignature());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], java_class.getClassName(), cp);

            if (mg.getInstructionList() == null) {
                continue;
            }
            InstructionGraph g = new InstructionGraph(mg);

            // g.calcReqLocal();
            g.calcStack(all);
            g.print(System.out);
        }
        return;
    }

    Enumeration en = jf.entries();
    ZipEntry ze = null;

    while (en.hasMoreElements()) {
        ze = (ZipEntry) en.nextElement();
        if (!ze.getName().endsWith(".class")) {
            System.out.println("Not a class file: " + ze.getName());
            continue;
        }

        System.out.println("\n\n**************************");
        System.out.println(ze.getName());
        System.out.println("**************************");

        JavaClass java_class;

        java_class = new ClassParser(jf.getInputStream(ze), ze.getName()).parse(); // May throw IOException

        ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
        Method[] methods = java_class.getMethods();

        for (int i = 0; i < methods.length; i++) {
            System.out.println("Memory : " + Runtime.getRuntime().freeMemory());
            System.out.println("--------------------------");
            System.out.println(methods[i].getName());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], java_class.getClassName(), cp);

            if (mg.getInstructionList() == null) {
                continue;
            }
            InstructionGraph g = new InstructionGraph(mg);

            // g.calcReqLocal();
            g.calcStack(all);
            System.out.println("Memory : " + Runtime.getRuntime().freeMemory());
            g = null;
            System.out.println("Collecting garbage...");
            System.out.println();

        }
    }
}

From source file:br.jabuti.instrumenter.bytecode.bcel.ASMInstrumenter.java

License:Open Source License

/**
 * This is a test driver. It takes a class name on args[0], inserts
 * some instructions in several points of each method in the class
 * and then dump the instrumented class to "new_"<original_name> 
 * file//www  . ja v  a  2s.co m
 */

public static void main(String args[]) throws Exception {
    // o melhor eh chamar com java ... ASMInstrumenter samples\arquivo.class
    // assim ele vai criar um novo arquivo new_samples\arquivo.class que
    // se pode testar

    JavaClass java_class;

    if ((java_class = Repository.lookupClass(args[0])) == null) {
        java_class = new ClassParser(args[0]).parse();
    } // May throw IOException

    ClassGen cg = new ClassGen(java_class);
    ConstantPoolGen cp = cg.getConstantPool();
    Method[] methods = cg.getMethods();

    for (int i = 0; i < methods.length; i++) {
        try {
            System.out.println("\n\n--------------------------");
            System.out.println(methods[i].getName());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cp);
            ASMInstrumenter gi = new ASMInstrumenter(mg, cg, cp);
            int nvars = mg.getMaxLocals() + 10;

            String s = "GETSTATIC java.lang.System out \"Ljava/io/PrintStream;\"  " + "astore " + nvars + " ";
            String s2 = "aload " + nvars + " ";
            String s3 = "LDC \"Entrando no metodo " + mg.getName() + "\" ";
            String s4 = "LDC \"Saindo do metodo " + mg.getName() + "\\n \" ";
            String s5 = "invokevirtual java.io.PrintStream println " + "\"(Ljava/lang/Object;)V\" ";

            gi.insertBefore(mg.getInstructionList().getStart(), s + s2 + s3 + s5);
            gi.insertBefore(mg.getInstructionList().getEnd(), s2 + s4 + s5);
            methods[i] = mg.getMethod();
        } catch (ParseException e) {
            System.err.println("Parser error " + e.getMessage());
        }
    }
    cg.setMethods(methods);
    java_class = cg.getJavaClass();
    java_class.dump("new_" + args[0]);
}

From source file:br.jabuti.instrumenter.bytecode.bcel.Instrumenter.java

License:Open Source License

/** Creates the instrumenter for a given method 
        /*from  w  ww . ja  v  a2 s. com*/
 @param mg The method to be changed
 */
public Instrumenter(MethodGen mg, ConstantPoolGen c) {
    meth = mg;
    cg = c;
    il = mg.getInstructionList();
}

From source file:br.jabuti.lookup.java.bytecode.RClassCode.java

License:Open Source License

/** Retorna uma lista de metodos chamados por um dado metodo
 * desta classe.//from   ww w  . j  av  a 2 s  .  co m
 * @param assinatura - a assinatura do metodo que se deseja analisar
 * @return a lista de metodos chamados pelo metodo passado como argumento.
 * Se o metodo solicitado nao for encontrado na classe, retorna null.
 */
public String[] getCalledMethods(String assinatura) {
    JavaClass jc = this.getTheClass();
    Method[] mv = jc.getMethods();
    Method m = null;
    String met = new String();
    int i;
    for (i = 0; i < mv.length; i++) {
        m = mv[i];

        met = jc.getClassName() + "." + mv[i].getName() + mv[i].getSignature();
        System.out.println("Metodo Aplicao = " + met);
        System.out.println("Metodo Parametro = " + assinatura);

        if (met.equals(assinatura))
            break;
    }
    if (i == mv.length)
        return null;
    ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool());
    MethodGen mg = new MethodGen(m, jc.getClassName(), cp);

    InstructionList il = mg.getInstructionList();
    InstructionHandle[] ih = il.getInstructionHandles();
    Vector v = new Vector();

    for (int x = 0; x < ih.length; x++) {
        Instruction ins = ih[x].getInstruction();
        if (ins instanceof InvokeInstruction) {
            InvokeInstruction invoke = (InvokeInstruction) ins;
            String s = invoke.getClassName(cp) + "." + invoke.getMethodName(cp) + invoke.getSignature(cp);

            //System.out.println("gettype = " + invoke.getClassType(cp));
            System.out.println("metodo retornado = " + s);
            v.add(s);
        }
    }
    return (String[]) v.toArray(new String[0]);
}

From source file:br.jabuti.metrics.AbstractMetric.java

License:Open Source License

protected final double getNumberOfBytecodeInstructions(MethodGen mg) {
    InstructionList il = mg.getInstructionList();
    if (il == null)
        return 0.0;
    return (double) il.getLength();
}

From source file:br.jabuti.metrics.klass.MetricAMZNMS.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    double theValue = 0.0;
    RClass rc = prog.get(className);//w ww  .  java2s  .  c  om
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    int cont = 0;
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    Method[] methods = theClazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(methods[i], theClazz.getClassName(), cp);
        InstructionList instructions = mg.getInstructionList();
        Iterator<Instruction> instructionsIterator = instructions.iterator();
        while (instructionsIterator.hasNext()) {
            Instruction instruction = instructionsIterator.next();
            if (instruction instanceof InvokeInstruction) {
                theValue++;
            }
        }
        cont++;
    }
    if (cont == 0) {
        return -1.0;
    }
    return theValue / cont;
}

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 {/*from  ww w  . ja v a  2 s .com*/
            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.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 www. j av a 2s.  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());
}

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

License:Open Source License

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

}