Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

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

License:Open Source License

/**
 * <pre>//from  w w  w  . j ava  2 s  . c o 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.KotlinLateinitFilterTest.java

License:Open Source License

@Test
public void testLateinitBranchIsFiltered() {
    final Label l1 = new Label();
    final Label l2 = new Label();

    m.visitLabel(l1);/*from   ww w .j av a2  s  .  c o  m*/
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitFieldInsn(Opcodes.GETFIELD, "com/better/alarm/background/VibrationService", "wakeLock",
            "Landroid/os/PowerManager$WakeLock;");
    m.visitInsn(Opcodes.DUP);
    m.visitJumpInsn(Opcodes.IFNONNULL, l2);

    final AbstractInsnNode expectedFrom = m.instructions.getLast();

    m.visitLdcInsn("wakelock");
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/jvm/internal/Intrinsics",
            "throwUninitializedPropertyAccessException", "(Ljava/lang/String;)V", false);
    final AbstractInsnNode expectedTo = m.instructions.getLast();
    m.visitLabel(l2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/os/PowerManager$WakeLock", "acquire", "", false);

    filter.filter(m, context, output);

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

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

License:Open Source License

/**
 * <pre>/*from w w  w .j av a  2 s . c  o  m*/
 *     return x!!.length
 * </pre>
 */
@Test
public void should_filter() {
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.DUP);

    final Range range = new Range();
    final Label label = new Label();
    m.visitJumpInsn(Opcodes.IFNONNULL, label);
    range.fromInclusive = m.instructions.getLast();
    // no line number here and hence no probe
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "throwNpe", "()V", false);
    range.toInclusive = m.instructions.getLast();

    m.visitLabel(label);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
    m.visitInsn(Opcodes.IRETURN);

    filter.filter(m, context, output);

    assertIgnored(range);
}

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);
    m.visitJumpInsn(Opcodes.IFNONNULL, label);
    final AbstractInsnNode expectedFrom = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "kotlin/TypeCastException");
    m.visitInsn(Opcodes.DUP);/*from w w  w . j a va 2 s  .c om*/
    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);//from w  w  w.j  a v  a  2  s . c o  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  ava2  s. c o  m

    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);//  w w  w  .ja  v  a  2 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.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label case2 = new Label();
    final Label case3 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();
    final Label h2 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);//w ww  . j  av a2s . com
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitTableSwitchInsn(97, 98, caseDefault, h1, h2);
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("\0a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "\0a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case2);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);

    m.visitLabel(h2);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "b", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case3);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);
    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case2);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case3);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}

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

License:Open Source License

@Test
public void should_filter_when_default_is_first() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);/*from  w ww  .j a  v  a  2 s  .  co  m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitLookupSwitchInsn(caseDefault, new int[] { 97 }, new Label[] { h1 });
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}

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

License:Open Source License

@Test
public void javac() {
    final Label start = new Label();
    final Label end = new Label();
    final Label handler = new Label();
    final Label handlerEnd = new Label();
    m.visitTryCatchBlock(start, end, handler, null);
    m.visitTryCatchBlock(handler, handlerEnd, handler, null);

    m.visitVarInsn(Opcodes.ALOAD, 0);//  w  ww. j a v  a 2s  .co m
    m.visitFieldInsn(Opcodes.GETFIELD, "Fun", "lock", "Ljava/lang/Object;");
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    m.visitInsn(Opcodes.MONITORENTER);
    m.visitLabel(start);
    m.visitInsn(Opcodes.NOP);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.MONITOREXIT);
    m.visitLabel(end);
    final Label exit = new Label();
    m.visitJumpInsn(Opcodes.GOTO, exit);
    m.visitLabel(handler);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.MONITOREXIT);
    m.visitLabel(handlerEnd);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitInsn(Opcodes.ATHROW);
    m.visitLabel(exit);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertIgnored(new Range((LabelNode) handler.info, ((LabelNode) exit.info).getPrevious()));
}