Example usage for org.objectweb.asm.tree.analysis Analyzer getHandlers

List of usage examples for org.objectweb.asm.tree.analysis Analyzer getHandlers

Introduction

In this page you can find the example usage for org.objectweb.asm.tree.analysis Analyzer getHandlers.

Prototype

public List<TryCatchBlockNode> getHandlers(final int insnIndex) 

Source Link

Document

Returns the exception handlers for the given instruction.

Usage

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//from   ww w .  j  ava 2 s.  co  m
        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");
            }
        }
    }

}