Example usage for org.objectweb.asm.tree AbstractInsnNode getType

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getType

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getType.

Prototype

public abstract int getType();

Source Link

Document

Returns the type of this instruction.

Usage

From source file:analysis.ReferenceGenerator.java

License:Open Source License

private void refInstructions(InsnList insnList) {
    AbstractInsnNode insn;

    insn = insnList.getFirst();/* w  ww  .  jav a 2 s  . com*/

    Object cst;

    while (insn != null) {
        switch (insn.getType()) {
        case AbstractInsnNode.FIELD_INSN:
            //addTypeClassRef(Type.getType(((FieldInsnNode)insn).desc));
            addFieldRef(((FieldInsnNode) insn).owner, ((FieldInsnNode) insn).name);
            break;
        /*case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
                   
           break;*/
        /*case AbstractInsnNode.LDC_INSN:
           cst = ((LdcInsnNode)insn).cst;
           if(cst instanceof Type) {
              addTypeClassRef((Type)cst);
           }
           break;*/
        case AbstractInsnNode.METHOD_INSN:
            addMethodRef(((MethodInsnNode) insn).owner, ((MethodInsnNode) insn).name,
                    ((MethodInsnNode) insn).desc);
            break;
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            addTypeClassRef(Type.getType(((MultiANewArrayInsnNode) insn).desc));
            break;
        case AbstractInsnNode.TYPE_INSN:
            addTypeClassRef(Type.getType(((TypeInsnNode) insn).desc));
            break;
        }
        insn = insn.getNext();
    }
}

From source file:br.usp.each.saeg.badua.core.internal.instr.CoverageMethodTransformer.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void transform(final MethodNode methodNode) {

    final DefUseAnalyzer analyzer = new DefUseAnalyzer();
    try {/*w  ww  .  j  a  va  2  s.  c  om*/
        analyzer.analyze(className, methodNode);
    } catch (final AnalyzerException e) {
        throw new RuntimeException(e);
    }

    final DefUseFrame[] frames = analyzer.getDefUseFrames();
    final Variable[] variables = analyzer.getVariables();
    final int[][] successors = analyzer.getSuccessors();
    final int[][] predecessors = analyzer.getPredecessors();
    final int[][] basicBlocks = analyzer.getBasicBlocks();
    final int[] leaders = analyzer.getLeaders();

    final DefUseChain[] chains = DefUseChain.toBasicBlock(
            new DepthFirstDefUseChainSearch().search(frames, variables, successors, predecessors), leaders,
            basicBlocks);

    this.chains = chains;
    if (chains.length == 0)
        return;

    // basic block definitions
    final Set<Variable>[] defs = (Set<Variable>[]) new Set<?>[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {
        defs[b] = new HashSet<Variable>();
        for (final int insnIndex : basicBlocks[b]) {
            defs[b].addAll(frames[insnIndex].getDefinitions());
        }
    }

    // bit-sets
    final BitSet[] potcov = new BitSet[basicBlocks.length];
    final BitSet[] potcovpuse = new BitSet[basicBlocks.length];
    final BitSet[] born = new BitSet[basicBlocks.length];
    final BitSet[] disabled = new BitSet[basicBlocks.length];
    final BitSet[] sleepy = new BitSet[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {

        potcov[b] = new BitSet(chains.length);
        potcovpuse[b] = new BitSet(chains.length);
        born[b] = new BitSet(chains.length);
        disabled[b] = new BitSet(chains.length);
        sleepy[b] = new BitSet(chains.length);

        for (int i = 0; i < chains.length; i++) {

            final DefUseChain chain = chains[i];

            if (chain.target != -1 ? chain.target == b : chain.use == b) {
                potcov[b].set(i);
                if (chain.target != -1) {
                    potcovpuse[b].set(i);
                }
            }

            if (chain.def == b) {
                born[b].set(i);
            }

            if (chain.def != b && defs[b].contains(variables[chain.var])) {
                disabled[b].set(i);
            }

            if (chain.target != -1) {
                if (chain.use != b) {
                    sleepy[b].set(i);
                }
            }

        }
    }

    // first/last valid instructions
    final AbstractInsnNode[] first = new AbstractInsnNode[basicBlocks.length];
    final AbstractInsnNode[] last = new AbstractInsnNode[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {
        for (final int insnIndex : basicBlocks[b]) {
            final AbstractInsnNode insn = methodNode.instructions.get(insnIndex);

            // skip
            switch (insn.getType()) {
            case AbstractInsnNode.LABEL:
            case AbstractInsnNode.FRAME:
            case AbstractInsnNode.LINE:
                continue;
            }

            if (first[b] == null) {
                first[b] = insn;
            }
            last[b] = insn;
        }
    }

    AbstractInsnNode insn = methodNode.instructions.getFirst();
    final int windows = (chains.length + 63) / 64;
    final int[] indexes = new int[windows];
    for (int w = 0; w < windows; w++) {
        indexes[w] = idGen.nextId();
        LabelFrameNode.insertBefore(insn, methodNode.instructions, init(methodNode, w));
    }

    for (int b = 0; b < basicBlocks.length; b++) {

        final long[] lPotcov = BitSetUtils.toLongArray(potcov[b], windows);
        final long[] lPotcovpuse = BitSetUtils.toLongArray(potcovpuse[b], windows);
        final long[] lBorn = BitSetUtils.toLongArray(born[b], windows);
        final long[] lDisabled = BitSetUtils.toLongArray(disabled[b], windows);
        final long[] lSleepy = BitSetUtils.toLongArray(sleepy[b], windows);

        for (int w = 0; w < windows; w++) {

            final int nPredecessors = predecessors[basicBlocks[b][0]].length;
            final Probe p = probe(methodNode, w, nPredecessors == 0);

            p.potcov = lPotcov[w];
            p.potcovpuse = lPotcovpuse[w];
            p.born = lBorn[w];
            p.disabled = lDisabled[w];
            p.sleepy = lSleepy[w];
            p.singlePredecessor = nPredecessors == 1;

            LabelFrameNode.insertBefore(first[b], methodNode.instructions, p);

        }
        if (isReturn(last[b].getOpcode())) {
            for (int w = 0; w < windows; w++) {
                final Probe p = update(methodNode, w, indexes[w]);
                LabelFrameNode.insertBefore(last[b], methodNode.instructions, p);
            }
        }
    }

    // Finally, update the frames
    while (insn != null) {
        if (insn instanceof FrameNode) {
            final FrameNode frame = (FrameNode) insn;
            frame.local = new ArrayList<Object>(frame.local);
            int size = 0;
            for (final Object obj : frame.local) {
                size++;
                if (obj.equals(Opcodes.DOUBLE) || obj.equals(Opcodes.LONG)) {
                    size++;
                }
            }
            while (size < methodNode.maxLocals) {
                frame.local.add(Opcodes.TOP);
                size++;
            }
            final Integer type = typeOfVars();
            for (int i = 0; i < windows; i++) {
                frame.local.add(type);
                frame.local.add(type);
                frame.local.add(type);
            }
        }
        insn = insn.getNext();
    }

    methodNode.maxLocals = methodNode.maxLocals + windows * numOfVars();
    methodNode.maxStack = methodNode.maxStack + 6;
}

From source file:br.usp.each.saeg.badua.core.internal.instr.LabelFrameNode.java

License:Open Source License

private static LabelFrameNode create(final AbstractInsnNode location) {
    AbstractInsnNode insn = location.getPrevious();
    if (insn == null) {
        return null;
    }/*from ww  w .j  av  a 2  s . com*/
    while (true) {
        switch (insn.getType()) {
        case AbstractInsnNode.LABEL:
            return create((LabelFrameNode) insn);
        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
            insn = insn.getPrevious();
            continue;
        default:
            return null;
        }
    }
}

From source file:cl.inria.stiq.instrumenter.BCIUtils.java

License:Open Source License

private static void printFrames(MethodNode aNode, Frame[] aFrames) {
    int bcIndex = 1;

    for (int i = 0; i < aFrames.length; i++) {
        Frame theFrame = aFrames[i];
        AbstractInsnNode theInsn = aNode.instructions.get(i);

        switch (theInsn.getType()) {
        case AbstractInsnNode.INSN:
        case AbstractInsnNode.INT_INSN:
        case AbstractInsnNode.VAR_INSN:
        case AbstractInsnNode.TYPE_INSN:
        case AbstractInsnNode.FIELD_INSN:
        case AbstractInsnNode.METHOD_INSN:
        case AbstractInsnNode.JUMP_INSN:
        case AbstractInsnNode.LDC_INSN:
        case AbstractInsnNode.IINC_INSN:
        case AbstractInsnNode.TABLESWITCH_INSN:
        case AbstractInsnNode.LOOKUPSWITCH_INSN:
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor();
            theInsn.accept(theTraceVisitor);
            StringWriter theWriter = new StringWriter();
            theTraceVisitor.print(new PrintWriter(theWriter));
            String theTraced = theWriter.toString().replace("\n", "");
            System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced);
            bcIndex++;//from w  w  w. j av  a2 s .c  o m
            break;

        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
        case AbstractInsnNode.LABEL:
            break;
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

public boolean collectCodeBlocks() {
    final int numIns = mn.instructions.size();

    codeBlocks[0] = FrameInfo.FIRST;//  ww  w .  ja  v  a  2 s  .c o m
    for (int i = 0; i < numIns; i++) {
        final Frame f = frames[i];
        if (f != null) { // reachable ?
            AbstractInsnNode in = mn.instructions.get(i);
            if (in.getType() == AbstractInsnNode.METHOD_INSN
                    || in.getType() == AbstractInsnNode.INVOKE_DYNAMIC_INSN) {
                Boolean susp = true;
                if (in.getType() == AbstractInsnNode.METHOD_INSN) {
                    final MethodInsnNode min = (MethodInsnNode) in;
                    int opcode = min.getOpcode();

                    if (isReflectInvocation(min.owner, min.name))
                        db.log(LogLevel.DEBUG,
                                "Reflective method call at instruction %d is assumed suspendable", i);
                    else if (isMethodHandleInvocation(min.owner, min.name))
                        db.log(LogLevel.DEBUG,
                                "MethodHandle invocation at instruction %d is assumed suspendable", i);
                    else if (isInvocationHandlerInvocation(min.owner, min.name))
                        db.log(LogLevel.DEBUG,
                                "InvocationHandler invocation at instruction %d is assumed suspendable", i);
                    else {
                        SuspendableType st = db.isMethodSuspendable(min.owner, min.name, min.desc, opcode);
                        if (st == SuspendableType.NON_SUSPENDABLE)
                            susp = false;
                        else if (st == null) {
                            db.log(LogLevel.WARNING,
                                    "Method not found in class - assuming suspendable: %s#%s%s (at%s#%s)",
                                    min.owner, min.name, min.desc, className, mn.name);
                            susp = true;
                        } else if (susp)
                            db.log(LogLevel.DEBUG, "Method call at instruction %d to %s#%s%s is suspendable", i,
                                    min.owner, min.name, min.desc);
                        if (st == SuspendableType.SUSPENDABLE_SUPER)
                            this.hasSuspendableSuperCalls = true;
                    }
                } else { // invoke dynamic
                    final InvokeDynamicInsnNode idin = (InvokeDynamicInsnNode) in;
                    if (idin.bsm.getOwner().equals("java/lang/invoke/LambdaMetafactory")) { // lambda
                        db.log(LogLevel.DEBUG, "Lambda at instruction %d", i);
                        susp = false;
                    } else
                        db.log(LogLevel.DEBUG,
                                "InvokeDynamic Method call at instruction %d to is assumed suspendable", i);
                }

                if (susp) {
                    FrameInfo fi = addCodeBlock(f, i);
                    splitTryCatch(fi);
                } else {
                    if (in.getType() == AbstractInsnNode.METHOD_INSN) {// not invokedynamic
                        final MethodInsnNode min = (MethodInsnNode) in;
                        db.log(LogLevel.DEBUG, "Method call at instruction %d to %s#%s%s is not suspendable", i,
                                min.owner, min.name, min.desc);
                        int blockingId = isBlockingCall(min);
                        if (blockingId >= 0 && !isAllowedToBlock(className, mn.name)) {
                            int mask = 1 << blockingId;
                            if (!db.isAllowBlocking()) {
                                throw new UnableToInstrumentException(
                                        "blocking call to " + min.owner + "#" + min.name + min.desc, className,
                                        mn.name, mn.desc);
                            } else if ((warnedAboutBlocking & mask) == 0) {
                                warnedAboutBlocking |= mask;
                                db.log(LogLevel.WARNING, "Method %s#%s%s contains potentially blocking call to "
                                        + min.owner + "#" + min.name + min.desc, className, mn.name, mn.desc);
                            }
                        }
                    }
                }
            }
        }
    }
    addCodeBlock(null, numIns);

    return numCodeBlocks > 1;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.FloatAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(final MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode instruction = instructions.get(i);
        final int res = Arrays.binarySearch(FLOAT_OPERATIONS, instruction.getOpcode());
        boolean isProblem = res >= 0;
        if (!isProblem && instruction.getType() == AbstractInsnNode.METHOD_INSN) {
            final String owner = ((MethodInsnNode) instruction).owner;
            isProblem = FLOAT_CLASS.equals(owner);
        }// ww  w  .  j a  v a2 s. c om
        if (isProblem) {
            final Problem problem = new Problem(instruction);
            if (!problems.contains(problem)) {
                problems.add(problem);
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InnerClassAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(MethodNode methodNode) {
    final LinkedList<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode.getOpcode() == Opcodes.INVOKESTATIC) {
            final MethodInsnNode method = (MethodInsnNode) insnNode;
            if (isStaticAccess(method)) {
                if (confirmParentClassAccess(methodNode, method)) {
                    final AbstractInsnNode inst = retrieveMethodOrField(method);
                    final int type = inst.getType();
                    if (type == AbstractInsnNode.FIELD_INSN || type == AbstractInsnNode.METHOD_INSN) {
                        final Problem problem = new Problem(inst);
                        problems.add(problem);
                    }//from  w ww .  j  a v a 2 s .c  o  m
                }
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InnerClassAnalyzer.java

License:Apache License

/**
 * Checks if a method of an inner class accesses the private content of an outer class (field or method)
 * @param methodNode/*from  www.  jav  a2 s .  c o m*/
 * @param method
 * @return True if the outer access is confirmed, false otherwise.
 */
private boolean confirmParentClassAccess(final MethodNode methodNode, final MethodInsnNode method) {
    final String methodNodeName = methodNode.name;
    AbstractInsnNode prev = method.getPrevious();
    boolean result = false;
    while (!result) {
        if (methodNodeName.startsWith("<init>")) {
            if (prev.getOpcode() == Opcodes.ALOAD) {
                final VarInsnNode varInsn = (VarInsnNode) prev;
                result = varInsn.var == 1;
            }
        } else {
            if (prev.getOpcode() == Opcodes.GETFIELD) {
                final FieldInsnNode getField = (FieldInsnNode) prev;
                final String field = getField.owner + getField.name;
                final String testField = mInnerClass.name + "this$0";
                result = field.equals(testField);
            }
        }
        if (prev.getType() == AbstractInsnNode.LINE) {
            break;
        } else {
            prev = prev.getPrevious();
        }
    }
    return result;
}

From source file:com.android.tools.klint.checks.ControlFlowGraph.java

License:Apache License

/** Adds an exception try block node to this graph */
protected void exception(@NonNull AbstractInsnNode from, @NonNull TryCatchBlockNode tcb) {
    // Add tcb's to all instructions in the range
    LabelNode start = tcb.start;/*from w  ww  .j a  v  a2 s .c  o  m*/
    LabelNode end = tcb.end; // exclusive

    // Add exception edges for all method calls in the range
    AbstractInsnNode curr = start;
    Node handlerNode = getNode(tcb.handler);
    while (curr != end && curr != null) {
        if (curr.getType() == AbstractInsnNode.METHOD_INSN) {
            // Method call; add exception edge to handler
            if (tcb.type == null) {
                // finally block: not an exception path
                getNode(curr).addSuccessor(handlerNode);
            }
            getNode(curr).addExceptionPath(handlerNode);
        }
        curr = curr.getNext();
    }
}

From source file:com.android.tools.klint.client.api.AsmVisitor.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API uses raw types
void runClassDetectors(ClassContext context) {
    ClassNode classNode = context.getClassNode();

    for (Detector detector : mAllDetectors) {
        detector.beforeCheckFile(context);
    }// ww w.  j  a  v a 2s.  c  om

    for (Detector detector : mFullClassChecks) {
        Detector.ClassScanner scanner = (Detector.ClassScanner) detector;
        scanner.checkClass(context, classNode);
        detector.afterCheckFile(context);
    }

    if (!mMethodNameToChecks.isEmpty() || !mMethodOwnerToChecks.isEmpty()
            || mNodeTypeDetectors != null && mNodeTypeDetectors.length > 0) {
        List methodList = classNode.methods;
        for (Object m : methodList) {
            MethodNode method = (MethodNode) m;
            InsnList nodes = method.instructions;
            for (int i = 0, n = nodes.size(); i < n; i++) {
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode call = (MethodInsnNode) instruction;

                    String owner = call.owner;
                    List<ClassScanner> scanners = mMethodOwnerToChecks.get(owner);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }

                    String name = call.name;
                    scanners = mMethodNameToChecks.get(name);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }
                }

                if (mNodeTypeDetectors != null && type < mNodeTypeDetectors.length) {
                    List<ClassScanner> scanners = mNodeTypeDetectors[type];
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkInstruction(context, classNode, method, instruction);
                        }
                    }
                }
            }
        }
    }

    for (Detector detector : mAllDetectors) {
        detector.afterCheckFile(context);
    }
}