Example usage for org.objectweb.asm Opcodes NEW

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

Introduction

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

Prototype

int NEW

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

Click Source Link

Usage

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

License:Open Source License

@Test
public void nextIsType() {
    m.visitInsn(Opcodes.NOP);//  w  ww . j a  v a2  s.c o m
    m.visitTypeInsn(Opcodes.NEW, "descriptor");

    // should set cursor to null when opcode mismatch
    matcher.cursor = m.instructions.getFirst();
    matcher.nextIsType(Opcodes.CHECKCAST, "descriptor");
    assertNull(matcher.cursor);

    // should set cursor to null when descriptor mismatch
    matcher.cursor = m.instructions.getFirst();
    matcher.nextIsType(Opcodes.NEW, "another_descriptor");
    assertNull(matcher.cursor);

    // should set cursor to next instruction when match
    matcher.cursor = m.instructions.getFirst();
    matcher.nextIsType(Opcodes.NEW, "descriptor");
    assertSame(m.instructions.getLast(), matcher.cursor);

    // should not do anything when cursor is null
    matcher.cursor = null;
    matcher.nextIsType(Opcodes.NEW, "descriptor");
}

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   w  w  w.ja  v a2 s .  com*/

    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>// w  w w . j  a v  a 2 s . c o m
 *     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);
}

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

License:Open Source License

/**
 * <pre>//from w  w w.j  a v a 2  s .  co  m
 *     suspend fun example() {
 *         suspendingFunction()
 *         nop()
 *     }
 * </pre>
 */
@Test
public void should_filter_suspending_functions() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_STATIC, "example",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    final int continuationArgumentIndex = 0;
    final int continuationIndex = 2;

    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    final Range range1 = new Range();
    range1.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.INSTANCEOF, "ExampleKt$example$1");
    final Label createStateInstance = new Label();
    m.visitJumpInsn(Opcodes.IFEQ, createStateInstance);

    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    m.visitTypeInsn(Opcodes.CHECKCAST, "ExampleKt$example$1");
    m.visitVarInsn(Opcodes.ASTORE, continuationIndex);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I");

    m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE));
    m.visitInsn(Opcodes.IAND);
    m.visitJumpInsn(Opcodes.IFEQ, createStateInstance);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitInsn(Opcodes.DUP);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I");

    m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE));
    m.visitInsn(Opcodes.ISUB);
    m.visitFieldInsn(Opcodes.PUTFIELD, "ExampleKt$example$1", "label", "I");

    final Label afterCoroutineStateCreated = new Label();
    m.visitJumpInsn(Opcodes.GOTO, afterCoroutineStateCreated);

    m.visitLabel(createStateInstance);

    m.visitTypeInsn(Opcodes.NEW, "ExampleKt$example$1");
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "ExampleKt$example$1", "<init>",
            "(Lkotlin/coroutines/Continuation;)V", false);

    m.visitVarInsn(Opcodes.ASTORE, continuationIndex);

    m.visitLabel(afterCoroutineStateCreated);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "result", "Ljava/lang/Object;");
    m.visitVarInsn(Opcodes.ASTORE, 1);

    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt",
            "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);

    // line of "fun"
    m.visitVarInsn(Opcodes.ASTORE, 3);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "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 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, 3);
    final Label continuationLabelAfterLoadedResult = new Label();
    m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult);
    // line of "fun"
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(state1);

    {
        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);
}

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

License:Open Source License

/**
 * <pre>//from  w ww. j a v  a  2 s.  co  m
 * 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.KotlinUnsafeCastOperatorFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final Label label = new Label();

    m.visitInsn(Opcodes.DUP);/*  w  w  w  . j  a  va  2 s .  c  om*/
    m.visitJumpInsn(Opcodes.IFNONNULL, label);
    final AbstractInsnNode expectedFrom = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "kotlin/TypeCastException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("null cannot be cast to non-null type kotlin.String");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/TypeCastException", "<init>", "(Ljava/lang/String;)V",
            false);
    m.visitInsn(Opcodes.ATHROW);
    final AbstractInsnNode expectedTo = m.instructions.getLast();
    m.visitLabel(label);

    filter.filter(m, context, output);

    assertIgnored(new Range(expectedFrom, expectedTo));
}

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

License:Open Source License

@Test
public void should_filter_implicit_else() {
    final Label label = new Label();

    final Range range1 = new Range();

    m.visitInsn(Opcodes.NOP);//  ww  w  .j  a v a 2  s . co  m

    m.visitJumpInsn(Opcodes.IFEQ, label);
    range1.fromInclusive = m.instructions.getLast();
    range1.toInclusive = m.instructions.getLast();

    m.visitInsn(Opcodes.NOP);

    final Range range2 = new Range();
    m.visitLabel(label);
    range2.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
    m.visitInsn(Opcodes.DUP);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
    m.visitInsn(Opcodes.ATHROW);
    range2.toInclusive = m.instructions.getLast();

    filter.filter(m, context, output);

    assertIgnored(range1, range2);
    assertNoReplacedBranches();
}

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

License:Open Source License

@Test
public void should_not_filter_explicit_else() {
    final Label label = new Label();

    m.visitInsn(Opcodes.NOP);//from   w w w  . j  a  va2 s.  c om

    m.visitJumpInsn(Opcodes.IFEQ, label);

    m.visitInsn(Opcodes.NOP);

    m.visitLabel(label);
    m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
    m.visitInsn(Opcodes.DUP);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
    m.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Throwable");
    m.visitInsn(Opcodes.ATHROW);

    filter.filter(m, context, output);

    assertIgnored();
    assertNoReplacedBranches();
}

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

License:Open Source License

@Test
public void should_filter_implicit_default() {
    final Label case1 = new Label();
    final Label caseDefault = new Label();
    final Label after = new Label();

    m.visitInsn(Opcodes.NOP);/*from ww w.j  a  v  a2  s . co  m*/

    m.visitTableSwitchInsn(0, 0, caseDefault, case1);
    final AbstractInsnNode switchNode = m.instructions.getLast();
    final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.ICONST_1);
    newTargets.add(m.instructions.getLast());
    m.visitJumpInsn(Opcodes.GOTO, after);

    final Range range1 = new Range();
    m.visitLabel(caseDefault);
    range1.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
    m.visitInsn(Opcodes.DUP);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    m.visitLabel(after);

    filter.filter(m, context, output);

    assertIgnored(range1);
    assertReplacedBranches(switchNode, newTargets);
}

From source file:org.jacoco.core.internal.instr.DuplicateFrameEliminatorTest.java

License:Open Source License

@Test
public void testTypeInsn() {
    testInstructionBetweenFrames(new TypeInsnNode(Opcodes.NEW, "java/lang/Object"));
}