Skip to content

Commit

Permalink
Update KotlinUnsafeCastOperatorFilter for Kotlin 1.6 (#1266)
Browse files Browse the repository at this point in the history
Co-authored-by: Evgeny Mandrikov <mandrikov@gmail.com>
Co-authored-by: Lukas Rössler <lukas.roessler@mysugr.com>
  • Loading branch information
3 people committed Dec 22, 2021
1 parent ac756b6 commit 2036acc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Expand Up @@ -121,6 +121,31 @@ public void should_filter_Kotlin_1_5() {
assertIgnored(new Range(expectedFrom, expectedTo));
}

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

@Test
public void should_not_filter_when_not_kotlin() {
m.visitInsn(Opcodes.DUP);
Expand Down
Expand Up @@ -46,6 +46,12 @@ public void match(final String exceptionType,
}
cursor = start;
final JumpInsnNode jumpInsnNode = (JumpInsnNode) cursor;
final AbstractInsnNode next = cursor.getNext();
if (next != null && next.getOpcode() == Opcodes.POP) {
// Since Kotlin 1.6.0 - see
// https://github.com/JetBrains/kotlin/commit/041773fd2584bc279813361eb7fc11ae84c214fd
next();
}
nextIsType(Opcodes.NEW, exceptionType);
nextIs(Opcodes.DUP);
nextIs(Opcodes.LDC);
Expand Down
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -29,6 +29,9 @@ <h3>New Features</h3>
<li>Part of bytecode generated by the Java compilers for <code>assert</code>
statement is filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1196">#1196</a>).</li>
<li>Branch added by the Kotlin compiler version 1.6.0 and above for "unsafe" cast
operator is filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1266">#1266</a>).</li>
</ul>

<h3>Fixed bugs</h3>
Expand Down

0 comments on commit 2036acc

Please sign in to comment.