Skip to content

Commit

Permalink
Update filter for Kotlin 1.5 suspending functions (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Apr 13, 2021
1 parent b68fe1a commit 86dc5fd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public void should_not_filter_synthetic_constructor_containing_default_arguments
assertIgnored();
}

/**
* For private suspending function Kotlin compiler versions prior to 1.5
* produce package-local synthetic method that should not be filtered
*
* <pre>
* private suspend fun example() {
* }
* </pre>
*
* @see #should_filter_synthetic_methods_whose_name_starts_with_access_dollar_even_if_last_argument_is_kotlin_coroutine_continuation()
*/
@Test
public void should_not_filter_synthetic_methods_whose_last_argument_is_kotlin_coroutine_continuation() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION,
Expand All @@ -145,4 +156,36 @@ public void should_not_filter_synthetic_methods_whose_last_argument_is_kotlin_co
assertIgnored();
}

/**
* For private suspending function Kotlin compiler versions starting from
* 1.5 produce additional public synthetic method with name starting with
* "access$" that should be filtered
*
* <pre>
* private suspend fun example() {
* }
* </pre>
*
* @see #should_not_filter_synthetic_methods_whose_last_argument_is_kotlin_coroutine_continuation()
*/
@Test
public void should_filter_synthetic_methods_whose_name_starts_with_access_dollar_even_if_last_argument_is_kotlin_coroutine_continuation() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL
| Opcodes.ACC_SYNTHETIC,
"access$example",
"(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", null,
null);
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitMethodInsn(Opcodes.INVOKESTATIC, "ExampleKt", "example",
"(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false);
m.visitInsn(Opcodes.RETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
*/
public final class KotlinCoroutineFilter implements IFilter {

static boolean isLastArgumentContinuation(final MethodNode methodNode) {
static boolean isImplementationOfSuspendFunction(
final MethodNode methodNode) {
if (methodNode.name.startsWith("access$")) {
return false;
}
final Type methodType = Type.getMethodType(methodNode.desc);
final int lastArgument = methodType.getArgumentTypes().length - 1;
return lastArgument >= 0 && "kotlin.coroutines.Continuation".equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void filter(final MethodNode methodNode,
return;
}

if (KotlinCoroutineFilter.isLastArgumentContinuation(methodNode)) {
if (KotlinCoroutineFilter
.isImplementationOfSuspendFunction(methodNode)) {
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ <h3>New Features</h3>
<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>
<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>
<li>Branches added by the Kotlin compiler version 1.4.20 and above for suspending
lambdas are filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1149">#1149</a>).</li>
Expand Down

0 comments on commit 86dc5fd

Please sign in to comment.