Skip to content

Commit

Permalink
Update KotlinUnsafeCastOperatorFilter for Kotlin 1.5 (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Apr 30, 2021
1 parent 722acd9 commit b23461d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Expand Up @@ -36,8 +36,8 @@ public void should_filter() {
final Label label = new Label();

m.visitInsn(Opcodes.DUP);
final AbstractInsnNode expectedFrom = m.instructions.getLast();
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");
Expand All @@ -59,8 +59,8 @@ public void should_filter_Kotlin_1_4() {
final Label label = new Label();

m.visitInsn(Opcodes.DUP);
final AbstractInsnNode expectedFrom = m.instructions.getLast();
m.visitJumpInsn(Opcodes.IFNONNULL, label);
final AbstractInsnNode expectedFrom = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException");
m.visitInsn(Opcodes.DUP);
m.visitLdcInsn("null cannot be cast to non-null type kotlin.String");
Expand All @@ -77,33 +77,48 @@ public void should_filter_Kotlin_1_4() {
}

/**
* For
*
* <pre>
* fun f(s: String?): String {
* return s as String
* }
* </pre>
*
* bytecode generated by Kotlin compiler version 1.4 is different from
* bytecode generated by version 1.5, unfortunately bytecode generated by
* later is the same as bytecode that both versions generate for
*
* <pre>
* fun f(o: Any?) {
* if (o == null)
* throw NullPointerException("null cannot be cast to non-null type")
* fun f(s: String?): String {
* if (s == null)
* throw NullPointerException("null cannot be cast to non-null type kotlin.String")
* return s
* }
* </pre>
*/
@Test
public void should_not_filter() {
public void should_filter_Kotlin_1_5() {
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
m.visitVarInsn(Opcodes.ALOAD, 1);

final Label label = new Label();
m.visitJumpInsn(Opcodes.IFNONNULL, label);
final AbstractInsnNode expectedFrom = m.instructions.getLast();
m.visitTypeInsn(Opcodes.NEW, "java/lang/NullPointerException");
m.visitInsn(Opcodes.DUP);
m.visitLdcInsn("null cannot be cast to non-null type");
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);
m.visitInsn(Opcodes.RETURN);
m.visitVarInsn(Opcodes.ALOAD, 0);

filter.filter(m, context, output);

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

@Test
Expand Down
Expand Up @@ -41,11 +41,10 @@ private static class Matcher extends AbstractMatcher {
public void match(final String exceptionType,
final AbstractInsnNode start, final IFilterOutput output) {

if (Opcodes.DUP != start.getOpcode()) {
if (Opcodes.IFNONNULL != start.getOpcode()) {
return;
}
cursor = start;
nextIs(Opcodes.IFNONNULL);
final JumpInsnNode jumpInsnNode = (JumpInsnNode) cursor;
nextIsType(Opcodes.NEW, exceptionType);
nextIs(Opcodes.DUP);
Expand Down
3 changes: 2 additions & 1 deletion org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -30,7 +30,8 @@ <h3>New Features</h3>
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1132">#1132</a>).</li>
<li>Branch added by the Kotlin compiler version 1.4.0 and above for "unsafe" cast
operator is filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1143">#1143</a>).</li>
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1143">#1143</a>,
<a href="https://github.com/jacoco/jacoco/issues/1178">#1178</a>).</li>
<li><code>synthetic</code> methods added by the Kotlin compiler version 1.5.0 and
above for <code>private</code> suspending functions are filtered out
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1174">#1174</a>).</li>
Expand Down

0 comments on commit b23461d

Please sign in to comment.