Search in sources :

Example 86 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class StringSwitchFilterTest method should_filter_Kotlin_1_5.

/**
 * <pre>
 * fun example(p: String) {
 *   when (p) {
 *     "a" -> return
 *     "\u0000a" -> return
 *     "b" -> return
 *     "\u0000b" -> return
 *     "c" -> return
 *     "\u0000c" -> return
 *   }
 * }
 * </pre>
 */
@Test
public void should_filter_Kotlin_1_5() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "example", "()V", null, null);
    final Label h1 = new Label();
    final Label h2 = new Label();
    final Label h3 = new Label();
    final Label defaultCase = new Label();
    final Label case1 = new Label();
    final Label case2 = new Label();
    final Label case3 = new Label();
    final Label case4 = new Label();
    final Label case5 = new Label();
    final Label case6 = new Label();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitTableSwitchInsn(97, 99, defaultCase, h1, h2, h3);
    m.visitLabel(h1);
    final AbstractInsnNode expectedFromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case1);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("\u0000a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case2);
    m.visitJumpInsn(Opcodes.GOTO, defaultCase);
    m.visitLabel(h2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case3);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("\u0000b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case4);
    m.visitJumpInsn(Opcodes.GOTO, defaultCase);
    m.visitLabel(h3);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("c");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case5);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("\u0000c");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFNE, case6);
    m.visitJumpInsn(Opcodes.GOTO, defaultCase);
    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(case4);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case5);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case6);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(defaultCase);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    filter.filter(m, context, output);
    assertIgnored(new Range(expectedFromInclusive, expectedToInclusive));
    assertReplacedBranches(expectedFromInclusive.getPrevious(), expectedNewTargets);
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Label(org.objectweb.asm.Label) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 87 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class StringSwitchFilterTest method should_filter.

@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);
    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));
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Label(org.objectweb.asm.Label) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 88 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class KotlinWhenFilterTest method should_filter_implicit_default.

@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);
    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);
}
Also used : Label(org.objectweb.asm.Label) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 89 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class KotlinLateinitFilterTest method should_filter_Kotlin_1_5.

/**
 * <pre>
 * class Example {
 *   private lateinit var x: String
 *   fun example() = x
 * }
 * </pre>
 */
@Test
public void should_filter_Kotlin_1_5() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "example", "()Ljava/lang/String;", null, null);
    Label label = new Label();
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitFieldInsn(Opcodes.GETFIELD, "Example", "x", "Ljava/lang/String;");
    m.visitVarInsn(Opcodes.ASTORE, 1);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitJumpInsn(Opcodes.IFNONNULL, label);
    final AbstractInsnNode expectedFrom = m.instructions.getLast();
    m.visitLdcInsn("x");
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "throwUninitializedPropertyAccessException", "(Ljava/lang/String;)V", false);
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitInsn(Opcodes.ATHROW);
    final AbstractInsnNode expectedTo = m.instructions.getLast();
    m.visitLabel(label);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitInsn(Opcodes.ARETURN);
    filter.filter(m, context, output);
    assertIgnored(new Range(expectedFrom, expectedTo));
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Label(org.objectweb.asm.Label) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Test(org.junit.Test)

Example 90 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class KotlinUnsafeCastOperatorFilterTest method should_filter_Kotlin_1_6.

@Test
public void should_filter_Kotlin_1_6() {
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
    final Label label = new Label();
    m.visitInsn(Opcodes.DUP);
    m.visitJumpInsn(Opcodes.IFNONNULL, label);
    final AbstractInsnNode expectedFrom = m.instructions.getLast();
    m.visitInsn(Opcodes.POP);
    m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("null cannot be cast to non-null type kotlin.String");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/NullPointerException", "<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));
}
Also used : Label(org.objectweb.asm.Label) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Test(org.junit.Test)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11