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

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

Introduction

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

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:org.friz.transformers.LongShiftTransformer.java

License:Open Source License

@Override
public void transform(Collection<ClassElement> elements) {
    for (ClassElement element : elements) {
        for (MethodElement method : element.methods()) {
            InsnMatcher matcher = new InsnMatcher(method.insn());
            for (Iterator<AbstractInsnNode[]> it = matcher.match("pushinstruction ((LSHL)|(LSHR)|(LUSHR))"); it
                    .hasNext();) {//from  www. j  a v a 2 s .  c o  m
                AbstractInsnNode[] nodes = it.next();
                AbstractInsnNode node = nodes[0];
                if (node instanceof IntInsnNode || node instanceof LdcInsnNode
                        || (node instanceof InsnNode && (node.getOpcode() >= 2 && node.getOpcode() <= 8))) {
                    long push = InsnNodeUtility.getNumericPushValue(node);
                    long realpush = push & 0x3d;
                    if (push != realpush) {
                        method.insn().set(node, InsnNodeUtility.createNumericPushInsn(realpush));
                        pInc(0);
                    } else {
                        tInc(0);
                    }
                }
            }
        }
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static void filter(final IFilterOutput output, final List<TryCatchBlockNode> tryCatchBlocks,
        final TryCatchBlockNode catchAnyBlock) {
    final AbstractInsnNode e = next(catchAnyBlock.handler);
    final int size = size(e);
    if (size <= 0) {
        return;//from w  w  w  .j a va  2  s  .c o m
    }

    // Determine instructions inside regions
    final Set<AbstractInsnNode> inside = new HashSet<AbstractInsnNode>();
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            AbstractInsnNode i = t.start;
            while (i != t.end) {
                inside.add(i);
                i = i.getNext();
            }
        }
    }

    // Find and merge duplicates at exits of regions
    for (final TryCatchBlockNode t : tryCatchBlocks) {
        if (t.handler == catchAnyBlock.handler) {
            boolean continues = false;
            AbstractInsnNode i = t.start;

            while (i != t.end) {
                switch (i.getType()) {
                case AbstractInsnNode.FRAME:
                case AbstractInsnNode.LINE:
                case AbstractInsnNode.LABEL:
                    break;
                case AbstractInsnNode.JUMP_INSN:
                    final AbstractInsnNode jumpTarget = next(((JumpInsnNode) i).label);
                    if (!inside.contains(jumpTarget)) {
                        merge(output, size, e, jumpTarget);
                    }
                    continues = i.getOpcode() != Opcodes.GOTO;
                    break;
                default:
                    switch (i.getOpcode()) {
                    case Opcodes.IRETURN:
                    case Opcodes.LRETURN:
                    case Opcodes.FRETURN:
                    case Opcodes.DRETURN:
                    case Opcodes.ARETURN:
                    case Opcodes.RETURN:
                    case Opcodes.ATHROW:
                        continues = false;
                        break;
                    default:
                        continues = true;
                        break;
                    }
                    break;
                }
                i = i.getNext();
            }

            i = next(i);
            if (continues && !inside.contains(i)) {
                merge(output, size, e, i);
            }
        }

        if (t != catchAnyBlock && t.start == catchAnyBlock.start && t.end == catchAnyBlock.end) {
            final AbstractInsnNode i = next(next(t.handler));
            if (!inside.contains(i)) {
                // javac's empty catch - merge after ASTORE
                merge(output, size, e, i);
            }
        }
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static void merge(final IFilterOutput output, final int size, AbstractInsnNode e, AbstractInsnNode n) {
    if (!isSame(size, e, n)) {
        return;/*  w  ww  . j av  a 2 s.  c om*/
    }
    output.ignore(e, e);
    e = next(e);
    for (int i = 0; i < size; i++) {
        output.merge(e, n);
        e = next(e);
        n = next(n);
    }
    output.ignore(e, next(e));

    if (n != null && n.getOpcode() == Opcodes.GOTO) {
        // goto instructions at the end of non-executed duplicates
        // cause partial coverage of last line of finally block,
        // so should be ignored
        output.ignore(n, n);
    }
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

private static boolean isSame(final int size, AbstractInsnNode e, AbstractInsnNode n) {
    e = next(e);/*from   w  w  w. ja v  a 2s  .  c o  m*/
    for (int i = 0; i < size; i++) {
        if (n == null || e.getOpcode() != n.getOpcode()) {
            return false;
        }
        e = next(e);
        n = next(n);
    }
    return true;
}

From source file:org.jacoco.core.internal.analysis.filter.FinallyFilter.java

License:Open Source License

/**
 * @return number of instructions inside given "catch-any" handler
 *///from  w  ww.  j a  v a  2s  .com
private static int size(AbstractInsnNode i) {
    if (Opcodes.ASTORE != i.getOpcode()) {
        // when always completes abruptly
        return 0;
    }
    final int var = ((VarInsnNode) i).var;
    int size = -1;
    do {
        size++;
        i = next(i);
        if (i == null) {
            // when always completes abruptly
            return 0;
        }
    } while (!(Opcodes.ALOAD == i.getOpcode() && var == ((VarInsnNode) i).var));
    i = next(i);
    if (Opcodes.ATHROW != i.getOpcode()) {
        return 0;
    }
    return size;
}

From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenFilter.java

License:Open Source License

private static LabelNode getDefaultLabel(final AbstractInsnNode i) {
    switch (i.getOpcode()) {
    case Opcodes.LOOKUPSWITCH:
        return ((LookupSwitchInsnNode) i).dflt;
    case Opcodes.TABLESWITCH:
        return ((TableSwitchInsnNode) i).dflt;
    default:/*  w  ww.j a  v a2 s  .  com*/
        return null;
    }
}

From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenFilter.java

License:Open Source License

private static void ignoreDefaultBranch(final AbstractInsnNode switchNode, final IFilterOutput output) {
    final List<LabelNode> labels;
    if (switchNode.getOpcode() == Opcodes.LOOKUPSWITCH) {
        labels = ((LookupSwitchInsnNode) switchNode).labels;
    } else {/*from   w ww.j  a v a 2s. c  o m*/
        labels = ((TableSwitchInsnNode) switchNode).labels;
    }
    final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();
    for (final LabelNode label : labels) {
        newTargets.add(AbstractMatcher.skipNonOpcodes(label));
    }
    output.replaceBranches(switchNode, newTargets);
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchJavacFilter.java

License:Open Source License

/**
 * javac generates two switches. First one by {@link String#hashCode()}.
 * Number of handlers in the second switch is equal to number of handlers in
 * source code, so it is enough to completely filter-out first switch.
 * Handler for default case of the first switch - is the second switch.
 *///from   w  w w.  j ava  2  s.  com
private void filter(final AbstractInsnNode start, final IFilterOutput output) {
    final LabelNode dflt;
    if (start.getOpcode() == Opcodes.LOOKUPSWITCH) {
        dflt = ((LookupSwitchInsnNode) start).dflt;
    } else if (start.getOpcode() == Opcodes.TABLESWITCH) {
        dflt = ((TableSwitchInsnNode) start).dflt;
    } else {
        return;
    }
    if (new Matcher().match(start, dflt)) {
        output.ignore(start, dflt);
    }
}

From source file:org.jacoco.core.test.validation.java5.FinallyTest.java

License:Open Source License

private Set<String> getTagsWithGotos() throws IOException {
    final Set<String> gotoTags = new HashSet<String>();

    byte[] b = TargetLoader.getClassDataAsBytes(FinallyTarget.class);

    final ClassNode classNode = new ClassNode();
    InstrSupport.classReaderFor(b).accept(classNode, 0);
    for (final MethodNode m : classNode.methods) {
        if ("main".equals(m.name)) {
            // skip it
            continue;
        }/* w w  w . ja  va2  s  . co  m*/
        int lineNumber = -1;
        for (AbstractInsnNode i : m.instructions) {
            if (AbstractInsnNode.LINE == i.getType()) {
                lineNumber = ((LineNumberNode) i).line;
            }
            if (Opcodes.GOTO == i.getOpcode()) {
                String tag = tags.get(Integer.valueOf(lineNumber));
                if (tag == null) {
                    throw new AssertionError("No tag at line " + lineNumber);
                }
                gotoTags.add(tag);
            }
        }
    }

    return gotoTags;
}

From source file:org.jacoco.core.test.validation.java5.StructuredLockingTest.java

License:Open Source License

private void assertStructuredLocking(String owner, MethodNode mn) throws Exception {
    Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) {

        @Override/*w  w  w  . j av a  2  s .  c om*/
        protected Frame<BasicValue> newFrame(int nLocals, int nStack) {
            return new LockFrame(nLocals, nStack);
        }

        @Override
        protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) {
            return new LockFrame(src);
        }
    };

    Frame<BasicValue>[] frames = analyzer.analyze(owner, mn);

    // Make sure no locks are left when method exits:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        switch (insn.getOpcode()) {
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.DRETURN:
        case Opcodes.ARETURN:
        case Opcodes.RETURN:
            ((LockFrame) frames[i]).assertNoLock("Exit with lock");
            break;
        case Opcodes.ATHROW:
            List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
            if (handlers == null || handlers.isEmpty()) {
                ((LockFrame) frames[i]).assertNoLock("Exit with lock");
            }
            break;
        }
    }

    // Only instructions protected by a catch-all handler can hold locks:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        if (insn.getOpcode() > 0) {
            boolean catchAll = false;
            List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
            if (handlers != null) {
                for (TryCatchBlockNode node : handlers) {
                    catchAll |= node.type == null;
                }
            }
            if (!catchAll) {
                ((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock");
            }
        }
    }

}