Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update KotlinUnsafeCastOperatorFilter for Kotlin 1.5 #1178

Merged
merged 3 commits into from Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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