Example usage for org.objectweb.asm Opcodes ATHROW

List of usage examples for org.objectweb.asm Opcodes ATHROW

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ATHROW.

Prototype

int ATHROW

To view the source code for org.objectweb.asm Opcodes ATHROW.

Click Source Link

Usage

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;// www  .j a v a  2s. c  om
    }

    // 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

/**
 * @return number of instructions inside given "catch-any" handler
 *///from  ww  w.  j  a va  2s.c o m
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.FinallyFilterTest.java

License:Open Source License

/**
 * <pre>//from  www .  ja  v  a2 s  .  co m
 *   try {
 *     ...
 *     if (...) {
 *       ...
 *       return;
 *     } else {
 *       ...
 *       return;
 *     }
 *   } finally {
 *     ...
 *   }
 * </pre>
 */
@Test
public void should_analyze_control_flow() {
    final Label start1 = new Label();
    final Label end1 = new Label();
    final Label start2 = new Label();
    final Label end2 = new Label();
    final Label finallyStart = new Label();

    m.visitTryCatchBlock(start1, end1, finallyStart, null);
    m.visitTryCatchBlock(start2, end2, finallyStart, null);

    m.visitLabel(start1);
    // jump to another region associated with same handler:
    m.visitJumpInsn(Opcodes.IFEQ, start2);
    m.visitInsn(Opcodes.NOP);
    m.visitLabel(end1);

    m.visitInsn(Opcodes.NOP); // finally block
    shouldMergeLast();
    m.visitInsn(Opcodes.RETURN);

    m.visitLabel(start2);
    m.visitInsn(Opcodes.NOP);
    m.visitLabel(end2);
    m.visitInsn(Opcodes.NOP); // finally block
    shouldMergeLast();
    m.visitInsn(Opcodes.RETURN);

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally block
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();

    execute();
}

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

License:Open Source License

@Test
public void javac_try_catch_finally() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(catchStart, catchEnd, finallyStart, null);
    m.visitTryCatchBlock(tryStart, tryEnd, finallyStart, null);

    m.visitLabel(tryStart);/*from w  w w.  java2s . c  om*/
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.NOP); // catch body
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitInsn(Opcodes.ATHROW);

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

@Test
public void ecj_try_catch_finally() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();
    final Label after = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, catchEnd, finallyStart, null);

    m.visitLabel(tryStart);//from ww  w.  j a v a  2s.c o  m
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.POP);
    m.visitInsn(Opcodes.NOP); // catch body
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, after);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitLabel(after);
    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

/**
 * javac 1.5 - 1.7//from ww  w.  j  ava2 s. co m
 */
@Test
public void javac_empty_catch() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, tryEnd, finallyStart, null);
    m.visitTryCatchBlock(catchStart, catchEnd, finallyStart, null);
    // actually one more useless TryCatchBlock for ASTORE in finally

    m.visitLabel(tryStart);
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();

    m.visitLabel(catchStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

/**
 * javac >= 1.8/*from w w  w  .  j a va 2 s. c  o m*/
 *
 * Probably related to https://bugs.openjdk.java.net/browse/JDK-7093325
 */
@Test
public void javac_8_empty_catch() throws Exception {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, tryEnd, finallyStart, null);

    m.visitLabel(tryStart);
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();
    shouldIgnoreLast();

    m.visitLabel(catchStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    execute();
}

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

License:Open Source License

@Test
public void ecj_empty_catch() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();
    final Label after = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, catchEnd, finallyStart, null);

    m.visitLabel(tryStart);//from w  ww .  java2  s.  c  om
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.POP);
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, after);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitLabel(after);
    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

@Test
public void should_filter_suspending_lambdas_generated_by_Kotlin_1_3_30() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "invokeSuspend",
            "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    m.visitLabel(new Label());
    final Range range1 = new Range();
    range1.fromInclusive = m.instructions.getLast();
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt",
            "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);
    m.visitVarInsn(Opcodes.ASTORE, 4);//from  ww  w.  j a v a2 s . c  o m

    m.visitVarInsn(Opcodes.ALOAD, 0);
    // line of "runBlocking"
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "label", "I");
    final Label dflt = new Label();
    final Label state0 = new Label();
    final Label state1 = new Label();
    m.visitTableSwitchInsn(0, 1, dflt, state0, state1);

    m.visitLabel(state0);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V",
                false);
        range1.toInclusive = m.instructions.getLast();
    }

    // line before "suspendingFunction"
    m.visitInsn(Opcodes.NOP);

    // line of "suspendingFunction"
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false);

    m.visitInsn(Opcodes.DUP);
    final Range range2 = new Range();
    range2.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label continuationLabelAfterLoadedResult = new Label();
    m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult);
    // line of "runBlocking"
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(state1);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "I$0", "I");
    m.visitVarInsn(Opcodes.ISTORE, 3);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V",
                false);
    }
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(continuationLabelAfterLoadedResult);

    // line after "suspendingFunction"
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(dflt);
    final Range range0 = new Range();
    range0.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
            "(Ljava/lang/String;)V", false);
    m.visitInsn(Opcodes.ATHROW);
    range0.toInclusive = m.instructions.getLast();

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2);
}

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

License:Open Source License

/**
 * <pre>//from   w ww  .j av a 2s  .com
 *     runBlocking {
 *         val x = 42
 *         nop(x)
 *         suspendingFunction()
 *         nop(x)
 *     }
 * </pre>
 */
@Test
public void should_filter_suspending_lambdas() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "invokeSuspend",
            "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    m.visitLabel(new Label());
    final Range range1 = new Range();
    range1.fromInclusive = m.instructions.getLast();
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt",
            "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);
    m.visitVarInsn(Opcodes.ASTORE, 4);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    // line of "runBlocking"
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "label", "I");
    final Label dflt = new Label();
    final Label state0 = new Label();
    final Label state1 = new Label();
    m.visitTableSwitchInsn(0, 1, dflt, state0, state1);

    m.visitLabel(state0);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        range1.toInclusive = m.instructions.getLast();
        m.visitLabel(label);
    }

    // line before "suspendingFunction"
    m.visitInsn(Opcodes.NOP);

    // line of "suspendingFunction"
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false);

    m.visitInsn(Opcodes.DUP);
    final Range range2 = new Range();
    range2.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label continuationLabelAfterLoadedResult = new Label();
    m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult);
    // line of "runBlocking"
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(state1);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "I$0", "I");
    m.visitVarInsn(Opcodes.ISTORE, 3);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        m.visitLabel(label);
    }
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(continuationLabelAfterLoadedResult);

    // line after "suspendingFunction"
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(dflt);
    final Range range0 = new Range();
    range0.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
            "(Ljava/lang/String;)V", false);
    m.visitInsn(Opcodes.ATHROW);
    range0.toInclusive = m.instructions.getLast();

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2);
}