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

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

Introduction

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

Prototype

public InstructionHandle[] getInstructionHandles() 

Source Link

Usage

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.// w w w.  j  a  va  2  s.c  o 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.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 w w  w.j  a  v a 2s  .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.util.InstructCtrl.java

License:Open Source License

/** Given a MethodGen object and an integer representing an offset,
* finds the InstructionHandle corresponding to that offset
*
* @param mg The MethodGen where to search the instruction
* @param offset The offset of the instruction
*//*from   w w  w  . j ava  2  s  .c  om*/
static public InstructionHandle findInstruction(MethodGen mg, int offset) {
    InstructionList il = mg.getInstructionList();

    int[] v = il.getInstructionPositions();

    return InstructionList.findHandle(il.getInstructionHandles(), v, v.length, offset);
}