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

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

Introduction

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

Prototype

public int[] getInstructionPositions() 

Source Link

Document

Get positions (offsets) of all instructions in the list.

Usage

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  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
*///ww w .  ja v  a  2s  . com
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);
}