Example usage for org.objectweb.asm Opcodes IFNULL

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

Introduction

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

Prototype

int IFNULL

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

Click Source Link

Usage

From source file:org.evosuite.testcase.ExecutionTracer.java

License:Open Source License

/**
 * Called by the instrumented code each time a new branch is taken
 * /*w  ww . j av  a  2s. c o  m*/
 * @param val
 *            a {@link java.lang.Object} object.
 * @param opcode
 *            a int.
 * @param branch
 *            a int.
 * @param bytecode_id
 *            a int.
 */
public static void passedBranch(Object val, int opcode, int branch, int bytecode_id) {
    ExecutionTracer tracer = getExecutionTracer();
    if (tracer.disabled)
        return;

    if (isThreadNeqCurrentThread())
        return;

    checkTimeout();

    double distance_true = 0;
    double distance_false = 0;
    switch (opcode) {
    case Opcodes.IFNULL:
        distance_true = val == null ? 0.0 : 1.0;
        break;
    case Opcodes.IFNONNULL:
        distance_true = val == null ? 1.0 : 0.0;
        break;
    default:
        logger.error("Warning: encountered opcode {}", opcode);
    }
    distance_false = distance_true == 0 ? 1.0 : 0.0;
    // enable();

    // logger.trace("Branch distance true: " + distance_true);
    // logger.trace("Branch distance false: " + distance_false);

    // Add current branch to control trace
    tracer.trace.branchPassed(branch, bytecode_id, distance_true, distance_false);
}

From source file:org.evosuite.TestSuiteGenerator.java

License:Open Source License

private void getBytecodeStatistics() {
    if (Properties.TRACK_BOOLEAN_BRANCHES) {
        int gradientBranchCount = ExecutionTraceImpl.gradientBranches.size() * 2;
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Gradient_Branches,
                gradientBranchCount);/*w  w w . j  a  va2  s .  c o  m*/
    }
    if (Properties.TRACK_COVERED_GRADIENT_BRANCHES) {
        int coveredGradientBranchCount = ExecutionTraceImpl.gradientBranchesCoveredTrue.size()
                + ExecutionTraceImpl.gradientBranchesCoveredFalse.size();
        ClientServices.getInstance().getClientNode()
                .trackOutputVariable(RuntimeVariable.Gradient_Branches_Covered, coveredGradientBranchCount);
    }
    if (Properties.BRANCH_COMPARISON_TYPES) {
        int cmp_intzero = 0, cmp_intint = 0, cmp_refref = 0, cmp_refnull = 0;
        int bc_lcmp = 0, bc_fcmpl = 0, bc_fcmpg = 0, bc_dcmpl = 0, bc_dcmpg = 0;
        for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT())
                .getAllBranches()) {
            int branchOpCode = b.getInstruction().getASMNode().getOpcode();
            int previousOpcode = -2;
            if (b.getInstruction().getASMNode().getPrevious() != null)
                previousOpcode = b.getInstruction().getASMNode().getPrevious().getOpcode();
            switch (previousOpcode) {
            case Opcodes.LCMP:
                bc_lcmp++;
                break;
            case Opcodes.FCMPL:
                bc_fcmpl++;
                break;
            case Opcodes.FCMPG:
                bc_fcmpg++;
                break;
            case Opcodes.DCMPL:
                bc_dcmpl++;
                break;
            case Opcodes.DCMPG:
                bc_dcmpg++;
                break;
            }
            switch (branchOpCode) {
            // copmpare int with zero
            case Opcodes.IFEQ:
            case Opcodes.IFNE:
            case Opcodes.IFLT:
            case Opcodes.IFGE:
            case Opcodes.IFGT:
            case Opcodes.IFLE:
                cmp_intzero++;
                break;
            // copmpare int with int
            case Opcodes.IF_ICMPEQ:
            case Opcodes.IF_ICMPNE:
            case Opcodes.IF_ICMPLT:
            case Opcodes.IF_ICMPGE:
            case Opcodes.IF_ICMPGT:
            case Opcodes.IF_ICMPLE:
                cmp_intint++;
                break;
            // copmpare reference with reference
            case Opcodes.IF_ACMPEQ:
            case Opcodes.IF_ACMPNE:
                cmp_refref++;
                break;
            // compare reference with null
            case Opcodes.IFNULL:
            case Opcodes.IFNONNULL:
                cmp_refnull++;
                break;

            }
        }
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Cmp_IntZero,
                cmp_intzero);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Cmp_IntInt,
                cmp_intint);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Cmp_RefRef,
                cmp_refref);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Cmp_RefNull,
                cmp_refnull);

        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.BC_lcmp, bc_lcmp);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.BC_fcmpl, bc_fcmpl);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.BC_fcmpg, bc_fcmpg);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.BC_dcmpl, bc_dcmpl);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.BC_dcmpg, bc_dcmpg);

        RuntimeVariable[] bytecodeVarsCovered = new RuntimeVariable[] { RuntimeVariable.Covered_lcmp,
                RuntimeVariable.Covered_fcmpl, RuntimeVariable.Covered_fcmpg, RuntimeVariable.Covered_dcmpl,
                RuntimeVariable.Covered_dcmpg, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntInt,
                RuntimeVariable.Covered_IntZero, RuntimeVariable.Covered_RefRef,
                RuntimeVariable.Covered_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsCovered) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredFalse)
                            + getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredTrue));
        }

        RuntimeVariable[] bytecodeVarsReached = new RuntimeVariable[] { RuntimeVariable.Reached_lcmp,
                RuntimeVariable.Reached_fcmpl, RuntimeVariable.Reached_fcmpg, RuntimeVariable.Reached_dcmpl,
                RuntimeVariable.Reached_dcmpg, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntInt,
                RuntimeVariable.Reached_IntZero, RuntimeVariable.Reached_RefRef,
                RuntimeVariable.Reached_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsReached) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionReached) * 2);
        }

    }

}

From source file:org.evosuite.TestSuiteGeneratorHelper.java

License:Open Source License

static void getBytecodeStatistics() {
    if (Properties.TRACK_BOOLEAN_BRANCHES) {
        int gradientBranchCount = ExecutionTraceImpl.gradientBranches.size() * 2;
        ClientServices.track(RuntimeVariable.Gradient_Branches, gradientBranchCount);
    }/*from w w  w .  j a v a 2s.co  m*/
    if (Properties.TRACK_COVERED_GRADIENT_BRANCHES) {
        int coveredGradientBranchCount = ExecutionTraceImpl.gradientBranchesCoveredTrue.size()
                + ExecutionTraceImpl.gradientBranchesCoveredFalse.size();
        ClientServices.track(RuntimeVariable.Gradient_Branches_Covered, coveredGradientBranchCount);
    }
    if (Properties.BRANCH_COMPARISON_TYPES) {
        int cmp_intzero = 0, cmp_intint = 0, cmp_refref = 0, cmp_refnull = 0;
        int bc_lcmp = 0, bc_fcmpl = 0, bc_fcmpg = 0, bc_dcmpl = 0, bc_dcmpg = 0;
        for (Branch b : BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT())
                .getAllBranches()) {
            int branchOpCode = b.getInstruction().getASMNode().getOpcode();
            int previousOpcode = -2;
            if (b.getInstruction().getASMNode().getPrevious() != null) {
                previousOpcode = b.getInstruction().getASMNode().getPrevious().getOpcode();
            }
            switch (previousOpcode) {
            case Opcodes.LCMP:
                bc_lcmp++;
                break;
            case Opcodes.FCMPL:
                bc_fcmpl++;
                break;
            case Opcodes.FCMPG:
                bc_fcmpg++;
                break;
            case Opcodes.DCMPL:
                bc_dcmpl++;
                break;
            case Opcodes.DCMPG:
                bc_dcmpg++;
                break;
            }
            switch (branchOpCode) {
            // copmpare int with zero
            case Opcodes.IFEQ:
            case Opcodes.IFNE:
            case Opcodes.IFLT:
            case Opcodes.IFGE:
            case Opcodes.IFGT:
            case Opcodes.IFLE:
                cmp_intzero++;
                break;
            // copmpare int with int
            case Opcodes.IF_ICMPEQ:
            case Opcodes.IF_ICMPNE:
            case Opcodes.IF_ICMPLT:
            case Opcodes.IF_ICMPGE:
            case Opcodes.IF_ICMPGT:
            case Opcodes.IF_ICMPLE:
                cmp_intint++;
                break;
            // copmpare reference with reference
            case Opcodes.IF_ACMPEQ:
            case Opcodes.IF_ACMPNE:
                cmp_refref++;
                break;
            // compare reference with null
            case Opcodes.IFNULL:
            case Opcodes.IFNONNULL:
                cmp_refnull++;
                break;

            }
        }
        ClientServices.track(RuntimeVariable.Cmp_IntZero, cmp_intzero);
        ClientServices.track(RuntimeVariable.Cmp_IntInt, cmp_intint);
        ClientServices.track(RuntimeVariable.Cmp_RefRef, cmp_refref);
        ClientServices.track(RuntimeVariable.Cmp_RefNull, cmp_refnull);

        ClientServices.track(RuntimeVariable.BC_lcmp, bc_lcmp);
        ClientServices.track(RuntimeVariable.BC_fcmpl, bc_fcmpl);
        ClientServices.track(RuntimeVariable.BC_fcmpg, bc_fcmpg);
        ClientServices.track(RuntimeVariable.BC_dcmpl, bc_dcmpl);
        ClientServices.track(RuntimeVariable.BC_dcmpg, bc_dcmpg);

        RuntimeVariable[] bytecodeVarsCovered = new RuntimeVariable[] { RuntimeVariable.Covered_lcmp,
                RuntimeVariable.Covered_fcmpl, RuntimeVariable.Covered_fcmpg, RuntimeVariable.Covered_dcmpl,
                RuntimeVariable.Covered_dcmpg, RuntimeVariable.Covered_IntInt, RuntimeVariable.Covered_IntInt,
                RuntimeVariable.Covered_IntZero, RuntimeVariable.Covered_RefRef,
                RuntimeVariable.Covered_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsCovered) {
            ClientServices.track(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredFalse)
                            + getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionCoveredTrue));
        }

        RuntimeVariable[] bytecodeVarsReached = new RuntimeVariable[] { RuntimeVariable.Reached_lcmp,
                RuntimeVariable.Reached_fcmpl, RuntimeVariable.Reached_fcmpg, RuntimeVariable.Reached_dcmpl,
                RuntimeVariable.Reached_dcmpg, RuntimeVariable.Reached_IntInt, RuntimeVariable.Reached_IntInt,
                RuntimeVariable.Reached_IntZero, RuntimeVariable.Reached_RefRef,
                RuntimeVariable.Reached_RefNull };

        for (RuntimeVariable bcvar : bytecodeVarsReached) {
            ClientServices.track(bcvar,
                    getBytecodeCount(bcvar, ExecutionTraceImpl.bytecodeInstructionReached) * 2);
        }

    }

}

From source file:org.graalvm.compiler.replacements.test.DeoptimizeOnExceptionTest.java

License:Open Source License

private static byte[] makeClazz() {
    // Code generated the class below using asm.
    String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/');
    final ClassWriter w = new ClassWriter(0);
    w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object",
            new String[] { "java/lang/Runnable" });
    MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {});
    mv.visitCode();//w ww. j  a v a2 s  .c  o m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(10, 10);
    mv.visitEnd();

    mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.JSR, l1);
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ASTORE, 1);

    Label lElse = new Label();
    Label lEnd = new Label();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
    mv.visitInsn(Opcodes.POP2);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    mv.visitInsn(Opcodes.DUP);
    mv.visitJumpInsn(Opcodes.IFNULL, lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false);
    mv.visitJumpInsn(Opcodes.GOTO, lEnd);
    mv.visitLabel(lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false);
    mv.visitLabel(lEnd);

    mv.visitVarInsn(Opcodes.RET, 1);
    mv.visitMaxs(10, 10);
    mv.visitEnd();
    return w.toByteArray();
}

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

License:Open Source License

/**
 * <pre>/* ww w. ja v  a 2 s  . c om*/
 * open class Open {
 *     open fun foo(a: Int = 42) {
 *     }
 * }
 * </pre>
 */
@Test
public void should_filter_open_functions() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_SYNTHETIC, "foo$default",
            "(LOpen;IILjava/lang/Object;)V", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
    {
        m.visitVarInsn(Opcodes.ALOAD, 3);
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, label);
        m.visitTypeInsn(Opcodes.NEW, "java/lang/UnsupportedOperationException");
        m.visitInsn(Opcodes.DUP);
        m.visitLdcInsn("Super calls with default arguments not supported in this target, function: foo");
        m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
                "(Ljava/lang/String;)V", false);
        m.visitInsn(Opcodes.ATHROW);
        m.visitLabel(label);
    }
    {
        m.visitVarInsn(Opcodes.ILOAD, 2);
        m.visitInsn(Opcodes.ICONST_1);
        m.visitInsn(Opcodes.IAND);
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        // default argument
        m.visitLdcInsn(Integer.valueOf(42));
        m.visitVarInsn(Opcodes.ISTORE, 1);
        m.visitLabel(label);

        m.visitVarInsn(Opcodes.ALOAD, 0);
        m.visitVarInsn(Opcodes.ILOAD, 1);
        m.visitMethodInsn(Opcodes.INVOKESPECIAL, "Open", "foo", "(I)V", false);
        m.visitInsn(Opcodes.RETURN);
    }

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.getFirst(), m.instructions.get(6)),
            new Range(m.instructions.get(11), m.instructions.get(11)));
}

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

License:Open Source License

/**
 * ECJ for//from w  w  w.  jav  a  2 s .c o  m
 *
 * <pre>
 *     try (r0 = ...; r1 = ...; r2= ...) {
 *         ...
 *     } finally (...) {
 *         ...
 *     }
 *     ...
 * </pre>
 *
 * generates
 *
 * <pre>
 *     ACONST_NULL
 *     ASTORE primaryExc
 *     ACONST_NULL
 *     ASTORE suppressedExc
 *     ...
 *     ASTORE r1
 *     ...
 *     ASTORE r2
 *     ...
 *     ASTORE r3
 *
 *     ... // body
 *
 *     ALOAD r3
 *     IFNULL r2_close
 *     ALOAD r3
 *     INVOKEVIRTUAL close:()V
 *     GOTO r2_close
 *
 *     ASTORE primaryExc
 *     ALOAD r3
 *     IFNULL n
 *     ALOAD r3
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     r2_close:
 *     ALOAD r2
 *     IFNULL r1_close
 *     ALOAD r2
 *     INVOKEVIRTUAL close:()V
 *     GOTO r1_close
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD r2
 *     IFNULL n
 *     ALOAD r2
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     r1_close:
 *     ALOAD r1
 *     IFNULL after
 *     ALOAD r1
 *     INVOKEVIRTUAL close:()V
 *     GOTO after
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD r1
 *     IFNULL n
 *     ALOAD r1
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ... // additional handlers for catch blocks and finally on exceptional path
 *
 *     after:
 *     ... // finally on normal path
 *     ...
 * </pre>
 */
@Test
public void ecj() {
    final Range range0 = new Range();
    final Range range1 = new Range();

    final Label handler = new Label();
    m.visitTryCatchBlock(handler, handler, handler, null);

    // primaryExc = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    // suppressedExc = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 2);

    // body
    m.visitInsn(Opcodes.NOP);

    final Label l4 = new Label();
    final Label l7 = new Label();
    final Label end = new Label();
    { // nextIsEcjClose("r0")
        m.visitVarInsn(Opcodes.ALOAD, 5);
        range0.fromInclusive = m.instructions.getLast();
        m.visitJumpInsn(Opcodes.IFNULL, l4);
        m.visitVarInsn(Opcodes.ALOAD, 5);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
    }
    m.visitJumpInsn(Opcodes.GOTO, l4);
    range0.toInclusive = m.instructions.getLast();
    // catch (any primaryExc)
    m.visitLabel(handler);
    range1.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 1);
    { // nextIsEcjCloseAndThrow("r0")
        m.visitVarInsn(Opcodes.ALOAD, 5);
        Label l11 = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, l11);
        m.visitVarInsn(Opcodes.ALOAD, 5);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
        m.visitLabel(l11);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    m.visitLabel(l4);
    { // nextIsEcjClose("r1")
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitJumpInsn(Opcodes.IFNULL, l7);
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
    }
    m.visitJumpInsn(Opcodes.GOTO, l7);
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    { // nextIsEcjCloseAndThrow("r1")
        m.visitVarInsn(Opcodes.ALOAD, 4);
        final Label l14 = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, l14);
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
        m.visitLabel(l14);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    m.visitLabel(l7);
    { // nextIsEcjClose("r2")
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitJumpInsn(Opcodes.IFNULL, end);
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
        m.visitJumpInsn(Opcodes.GOTO, end);
    }
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    { // nextIsEcjCloseAndThrow("r2")
        m.visitVarInsn(Opcodes.ALOAD, 3);
        final Label l18 = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, l18);
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun2$Resource", "close", "()V", false);
        m.visitLabel(l18);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    // throw primaryExc
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    // additional handlers
    m.visitInsn(Opcodes.NOP);

    filter.filter(m, context, output);

    assertIgnored(range0, range1);
}

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

License:Open Source License

/**
 * ECJ for/*  ww  w . ja  v  a2s  .c  om*/
 *
 * <pre>
 *     try (r1 = ...; r2 = ...; r3 = ...) {
 *         return ...
 *     } finally {
 *         ...
 *     }
 * </pre>
 *
 * generates
 *
 * <pre>
 *     ACONST_NULL
 *     astore primaryExc
 *     ACONST_NULL
 *     astore suppressedExc
 *
 *     ...
 *     ASTORE r1
 *     ...
 *     ASTORE r2
 *     ...
 *     ASTORE r3
 *
 *     ... // body
 *
 *     ALOAD r3
 *     IFNULL n
 *     ALOAD r3
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD r2
 *     IFNULL n
 *     ALOAD r2
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD r1
 *     IFNULL n
 *     ALOAD r1
 *     INVOKEVIRTUAL close:()V
 *     n:
 *
 *     ... // finally on normal path
 *     ARETURN
 *
 *     ASTORE primaryExc
 *     ALOAD r3
 *     IFNULL n
 *     ALOAD r3
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO  e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ  e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD r2
 *     IFNULL n
 *     ALOAD r2
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD r1
 *     IFNULL n
 *     ALOAD r1
 *     INVOKEVIRTUAL close:()V
 *     n:
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ASTORE suppressedExc
 *     ALOAD primaryExc
 *     IFNONNULL s
 *     ALOAD suppressedExc
 *     ASTORE primaryExc
 *     GOTO e
 *     s:
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     IF_ACMPEQ e
 *     ALOAD primaryExc
 *     ALOAD suppressedExc
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     e:
 *
 *     ALOAD primaryExc
 *     ATHROW
 *
 *     ... // additional handlers for catch blocks and finally on exceptional path
 * </pre>
 */
@Test
public void ecj_noFlowOut() {
    final Range range0 = new Range();
    final Range range1 = new Range();

    final Label handler = new Label();
    m.visitTryCatchBlock(handler, handler, handler, null);

    // primaryExc = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    // suppressedExc = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 2);

    // body
    m.visitInsn(Opcodes.NOP);

    { // nextIsEcjClose("r0")
        final Label label = new Label();
        m.visitVarInsn(Opcodes.ALOAD, 5);
        range0.fromInclusive = m.instructions.getLast();
        m.visitJumpInsn(Opcodes.IFNULL, label);
        m.visitVarInsn(Opcodes.ALOAD, 5);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        m.visitLabel(label);
    }
    { // nextIsEcjClose("r1")
        final Label label = new Label();
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitJumpInsn(Opcodes.IFNULL, label);
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        m.visitLabel(label);
    }
    { // nextIsEcjClose("r2")
        final Label label = new Label();
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitJumpInsn(Opcodes.IFNULL, label);
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        range0.toInclusive = m.instructions.getLast();
        m.visitLabel(label);
    }

    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    // catch (any primaryExc)
    m.visitLabel(handler);
    range1.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 1);
    { // nextIsEcjCloseAndThrow("r0")
        m.visitVarInsn(Opcodes.ALOAD, 5);
        final Label throwLabel = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 5);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        m.visitLabel(throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    { // nextIsEcjCloseAndThrow("r1")
        m.visitVarInsn(Opcodes.ALOAD, 4);
        final Label throwLabel = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 4);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        m.visitLabel(throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    { // nextIsEcjCloseAndThrow("r2")
        m.visitVarInsn(Opcodes.ALOAD, 3);
        final Label throwLabel = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 3);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource", "close", "()V", false);
        m.visitLabel(throwLabel);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.ATHROW);
    }
    { // nextIsEcjSuppress
        m.visitVarInsn(Opcodes.ASTORE, 2);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        final Label suppressStart = new Label();
        m.visitJumpInsn(Opcodes.IFNONNULL, suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitVarInsn(Opcodes.ASTORE, 1);
        final Label suppressEnd = new Label();
        m.visitJumpInsn(Opcodes.GOTO, suppressEnd);
        m.visitLabel(suppressStart);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitJumpInsn(Opcodes.IF_ACMPEQ, suppressEnd);
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed",
                "(Ljava/lang/Throwable;)V", false);
        m.visitLabel(suppressEnd);
    }
    // throw primaryExc
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    // additional handlers
    m.visitInsn(Opcodes.NOP);

    filter.filter(m, context, output);

    assertIgnored(range0, range1);
}

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

License:Open Source License

/**
 * <pre>/*w ww .j a  v  a2  s  .  com*/
 *   try (r = open()) {
 *     ...
 *   }
 * </pre>
 */
@Test
public void with_null_check() {
    final Range range1 = new Range();
    final Range range2 = new Range();

    final Label e = new Label();
    final Label t = new Label();

    final Label handler = new Label();
    m.visitTryCatchBlock(handler, handler, handler, "java/lang/Throwable");

    m.visitInsn(Opcodes.NOP);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    range1.fromInclusive = m.instructions.getLast();
    m.visitJumpInsn(Opcodes.IFNULL, e);
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Resource", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, e);
    range1.toInclusive = m.instructions.getLast();

    m.visitLabel(handler);
    range2.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 1);
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitJumpInsn(Opcodes.IFNULL, t);
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Resource", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, t);

    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitLabel(t);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.ATHROW);
    range2.toInclusive = m.instructions.getLast();

    m.visitLabel(e);

    filter.filter(m, context, output);

    assertIgnored(range1, range2);
}

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

License:Open Source License

/**
 * javac 9 for//from  w w w . ja  va2  s. c o  m
 *
 * <pre>
 *     try (r0 = open(...); r1 = new ...) {
 *         return ...
 *     } finally {
 *         ...
 *     }
 * </pre>
 *
 * generates
 *
 * <pre>
 *     ...
 *     ASTORE r0
 *     ACONST_NULL
 *     ASTORE primaryExc0
 *
 *     ...
 *     ASTORE r1
 *     ACONST_NULL
 *     ASTORE primaryExc1
 *
 *     ... // body
 *
 *     ALOAD primaryExc1
 *     ALOAD r1
 *     INVOKESTATIC $closeResource:(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V
 *
 *     ALOAD r0
 *     IFNULL n
 *     ALOAD primaryExc0
 *     ALOAD r0
 *     INVOKESTATIC $closeResource:(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V
 *     n:
 *
 *     ... // finally on normal path
 *     ARETURN
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc1
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t
 *     ALOAD primaryExc1
 *     ALOAD r1
 *     INVOKESTATIC  $closeResource:(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc0
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t
 *     ALOAD r0
 *     IFNULL n
 *     ALOAD primaryExc0
 *     ALOAD r0
 *     INVOKESTATIC  $closeResource:(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V
 *     n:
 *     ALOAD t
 *     ATHROW
 *
 *     ... // additional handlers for catch blocks and finally on exceptional path
 * </pre>
 */
@Test
public void javac9() {
    final Range range0 = new Range();
    final Range range1 = new Range();
    final Range range2 = new Range();
    final Range range3 = new Range();

    final Label handler1 = new Label();
    m.visitTryCatchBlock(handler1, handler1, handler1, "java/lang/Throwable");

    final Label handler2 = new Label();
    m.visitTryCatchBlock(handler2, handler2, handler2, "java/lang/Throwable");

    // r0 = open(...)
    m.visitVarInsn(Opcodes.ASTORE, 1);

    // primaryExc0 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 2);

    // r1 = new ..
    m.visitVarInsn(Opcodes.ASTORE, 3);

    // primaryExc1 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 4);

    // body
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 5);

    // $closeResource(primaryExc1, r1)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    range0.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "Fun", "$closeResource",
            "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", false);
    range0.toInclusive = m.instructions.getLast();

    // if (r0 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.fromInclusive = m.instructions.getLast();
    final Label l11 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l11);
    // $closeResource(primaryExc0, r0)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "Fun", "$closeResource",
            "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", false);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(l11);

    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitInsn(Opcodes.ARETURN);

    // catch (Throwable t)
    m.visitLabel(handler1);
    range1.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 5);
    // primaryExc1 = t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitVarInsn(Opcodes.ASTORE, 4);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 6);
    // $closeResource(primaryExc1, r1)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "Fun", "$closeResource",
            "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", false);
    m.visitVarInsn(Opcodes.ALOAD, 6);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    // catch (Throwable t)
    m.visitLabel(handler2);
    range3.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 3);
    // primaryExc0 = t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 7);
    // if (r0 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    final Label l14 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l14);
    // $closeResource(primaryExc0, r0)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "Fun", "$closeResource",
            "(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V", false);
    m.visitLabel(l14);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 7);
    m.visitInsn(Opcodes.ATHROW);
    range3.toInclusive = m.instructions.getLast();

    m.visitVarInsn(Opcodes.ASTORE, 8);
    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitVarInsn(Opcodes.ALOAD, 8);
    m.visitInsn(Opcodes.ATHROW);

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2, range3);
}

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

License:Open Source License

/**
 * javac 7 and 8 for//  ww w .ja v  a  2  s .c o  m
 *
 * <pre>
 *     try (r0 = ...; r1 = ...) {
 *         return ...
 *     } finally {
 *         ...
 *     }
 * </pre>
 *
 * generate
 *
 * <pre>
 *     ...
 *     ASTORE r0
 *     ACONST_NULL
 *     ASTORE primaryExc0
 *
 *     ...
 *     ASTORE r1
 *     ACONST_NULL
 *     ASTORE primaryExc1
 *
 *     ... // body
 *
 *     ALOAD r1
 *     IFNULL n
 *     ALOAD primaryExc1
 *     IFNULL c
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     GOTO n
 *     ASTORE t
 *     ALOAD primaryExc1
 *     ALOAD t
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO n
 *     c:
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     n:
 *
 *     ALOAD r0
 *     IFNULL n
 *     ALOAD primaryExc0
 *     IFNULL c
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     GOTO n
 *     ASTORE t
 *     ALOAD primaryExc0
 *     ALOAD t
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO n
 *     c:
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     n:
 *
 *     ... // finally on normal path
 *     ARETURN
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc1
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t1
 *     ALOAD r1
 *     IFNULL e
 *     ALOAD primaryExc1
 *     IFNULL c
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     GOTO e
 *     ASTORE t2
 *     ALOAD primaryExc1
 *     ALOAD t2
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO e
 *     c:
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     e:
 *     ALOAD t1
 *     ATHROW
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc0
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t1
 *     ALOAD r0
 *     IFNULL e
 *     ALOAD primaryExc0
 *     IFNULL c
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     GOTO e
 *     ASTORE t2
 *     ALOAD primaryExc0
 *     ALOAD t2
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO e
 *     c:
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     e:
 *     ALOAD t1
 *     ATHROW
 *
 *     ... // additional handlers for catch blocks and finally on exceptional path
 * </pre>
 */
@Test
public void javac_7_8() {
    final Range range0 = new Range();
    final Range range1 = new Range();
    final Range range2 = new Range();
    final Range range3 = new Range();

    final Label handler1 = new Label();
    m.visitTryCatchBlock(handler1, handler1, handler1, "java/lang/Throwable");
    final Label handler2 = new Label();
    m.visitTryCatchBlock(handler2, handler2, handler2, "java/lang/Throwable");

    // r1 = ...
    m.visitVarInsn(Opcodes.ASTORE, 1);

    // primaryExc1 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 2);

    // r2 = ...
    m.visitVarInsn(Opcodes.ASTORE, 3);
    // primaryExc2 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 4);

    // body
    m.visitInsn(Opcodes.NOP);

    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 5);

    final Label l15 = new Label();
    // if (r2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 3);
    range0.fromInclusive = m.instructions.getLast();
    m.visitJumpInsn(Opcodes.IFNULL, l15);
    // if (primaryExc2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label l26 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l26);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l15);

    m.visitVarInsn(Opcodes.ASTORE, 6);
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitVarInsn(Opcodes.ALOAD, 6);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l15);

    m.visitLabel(l26);

    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    range0.toInclusive = m.instructions.getLast();
    m.visitLabel(l15);

    // if (r1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.fromInclusive = m.instructions.getLast();
    final Label l23 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l23);
    // if (primaryExc1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    final Label l27 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l27);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l23);

    m.visitVarInsn(Opcodes.ASTORE, 6);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 6);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l23);

    m.visitLabel(l27);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(l23);

    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    // catch (Throwable t)
    m.visitLabel(handler1);
    range1.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 5);
    // primaryExc2 = t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitVarInsn(Opcodes.ASTORE, 4);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 7);
    // if (r2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 3);
    final Label l28 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l28);
    // if (primaryExc2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label l29 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l29);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l28);

    m.visitVarInsn(Opcodes.ASTORE, 8);
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitVarInsn(Opcodes.ALOAD, 8);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l28);

    m.visitLabel(l29);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitLabel(l28);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 7);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    // catch (Throwable t)
    m.visitLabel(handler2);
    range3.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 3);
    // primaryExc2 = t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 9);
    // if (r1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    final Label l30 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l30);
    // if (primaryExc1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    final Label l31 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l31);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l30);

    m.visitVarInsn(Opcodes.ASTORE, 10);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 10);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l30);

    m.visitLabel(l31);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitLabel(l30);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 9);
    m.visitInsn(Opcodes.ATHROW);
    range3.toInclusive = m.instructions.getLast();

    m.visitVarInsn(Opcodes.ASTORE, 11);
    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitVarInsn(Opcodes.ALOAD, 11);
    m.visitInsn(Opcodes.ATHROW);

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2, range3);
}