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

Add filter for Kotlin inline value classes #1475

Merged
merged 4 commits into from
May 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2009, 2024 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.test.validation.kotlin;

import org.jacoco.core.test.validation.ValidationTestBase;
import org.jacoco.core.test.validation.kotlin.targets.KotlinInlineClassTarget;

/**
* Test of code coverage in {@link KotlinInlineClassTarget}.
*/
public class KotlinInlineClassTest extends ValidationTestBase {

public KotlinInlineClassTest() {
super(KotlinInlineClassTarget.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2009, 2024 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.test.validation.kotlin.targets

import org.jacoco.core.test.validation.targets.Stubs.nop

/**
* Test target for `inline class`.
*/
object KotlinInlineClassTarget {

interface Base {
fun base()
}

@JvmInline
value class I1(val value: String) : Base { // assertEmpty()

init { // assertEmpty()
nop() // assertFullyCovered()
} // assertEmpty()

constructor() : this("") { // assertFullyCovered()
nop() // assertFullyCovered()
} // assertEmpty()

val length: Int // assertEmpty()
get() = value.length // assertFullyCovered()

fun f(p: String) { // assertEmpty()
nop(p) // assertFullyCovered()
} // assertFullyCovered()

fun f(p: I1) { // assertEmpty()
nop(p) // assertFullyCovered()
} // assertFullyCovered()

override fun base() { // assertEmpty()
nop() // assertFullyCovered()
} // assertFullyCovered()

} // assertEmpty()

@JvmInline
value class I2(val value: String) { // assertEmpty()

override fun toString(): String { // assertEmpty()
return "Value: $value" // assertNotCovered()
} // assertEmpty()

} // assertEmpty()

@JvmStatic
fun main(args: Array<String>) {
val i = I1()
i.value
i.length
i.f("")
i.f(i)
i.base()

I2("")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* Copyright (c) 2009, 2024 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;

import org.junit.Test;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
* Unit tests for {@link KotlinInlineClassFilter}.
*/
public class KotlinInlineClassFilterTest extends FilterTestBase {

private final IFilter filter = new KotlinInlineClassFilter();

/**
* <pre>
* &#064;kotlin.jvm.JvmInline
* value class Example(val value: String)
* </pre>
*/
@Test
public void should_filter() {
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
context.classAnnotations.add("Lkotlin/jvm/JvmInline;");
final MethodNode m = new MethodNode(0, "getValue",
"()Ljava/lang/String;", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

/**
* <pre>
* &#064;kotlin.jvm.JvmInline
* value class Example(val value: String) {
* fun f() { ... }
* }
* </pre>
*/
@Test
public void should_not_filter_static() {
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
context.classAnnotations.add("Lkotlin/jvm/JvmInline;");
final MethodNode m = new MethodNode(Opcodes.ACC_STATIC, "f-impl", "()V",
null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

/**
* <pre>
* data class Example(val value: String)
* </pre>
*/
@Test
public void should_not_filter_when_no_JvmInline_annotation() {
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
final MethodNode m = new MethodNode(0, "getValue",
"()Ljava/lang/String;", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_when_not_kotlin() {
context.classAnnotations.add("Lkotlin/jvm/JvmInline;");
final MethodNode m = new MethodNode(0, "getValue",
"()Ljava/lang/String;", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static IFilter all() {
new KotlinWhenStringFilter(),
new KotlinUnsafeCastOperatorFilter(),
new KotlinNotNullOperatorFilter(),
new KotlinInlineClassFilter(),
new KotlinDefaultArgumentsFilter(), new KotlinInlineFilter(),
new KotlinCoroutineFilter(), new KotlinDefaultMethodsFilter(),
new KotlinComposeFilter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* Copyright (c) 2009, 2024 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
* Filters methods that Kotlin compiler generates for inline classes.
*
* For
*
* <pre>
* &#064;kotlin.jvm.JvmInline
* value class Example(val value: String) : Base {
* fun f(p: String) { ... }
* fun f(p: Example) { ... }
* override fun base() { ... }
* }
* </pre>
*
* Kotlin compiler produces
*
* <pre>
* &#064;kotlin.jvm.JvmInline
* class Example implements Base {
* private final String value;
* public String getValue() { return value; }
*
* private synthetic Example(String value) { this.value = value; }
*
* public static String constructor-impl(String value) { ... }
*
* public static void f-impl(String value, String p) { ... }
*
* public static void f-ulP-heY(String value, String p) { ... }
*
* public void base() { base-impl(value); }
* public static void base-impl(String value) { ... }
*
* public String toString() { return toString-impl(value); }
* public static String toString-impl(String value) { ... }
*
* public boolean equals(Object other) { return equals-impl(value, other); }
* public static boolean equals-impl(String value, Object other) { ... }
*
* public int hashCode() { return hashCode-impl(value); }
* public static int hashCode-impl(String value) { ... }
*
* public final synthetic String unbox-impl() { return value; }
* public static synthetic Example box-impl(String value) { return new Example(value); }
*
* public static equals-impl0(String value1, String value2) { ... }
* }
* </pre>
*
* Except getter all non-synthetic non-static methods delegate to corresponding
* static methods. Non-static methods are provided for interoperability with
* Java and can not be invoked from Kotlin without reflection and so should be
* filtered out.
*/
final class KotlinInlineClassFilter implements IFilter {

public void filter(final MethodNode methodNode,
final IFilterContext context, final IFilterOutput output) {
if (!KotlinGeneratedFilter.isKotlinClass(context)) {
return;
}
if (!context.getClassAnnotations().contains("Lkotlin/jvm/JvmInline;")) {
return;
}
if ((methodNode.access & Opcodes.ACC_STATIC) != 0) {
return;
}
output.ignore(methodNode.instructions.getFirst(),
methodNode.instructions.getLast());
}

}
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 @@ -25,6 +25,9 @@ <h3>New Features</h3>
<li>Part of bytecode generated by the Kotlin Compose compiler plugin is
filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1616">#1616</a>).</li>
<li>Part of bytecode generated by the Kotlin compiler for inline value classes is
filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1475">#1475</a>).</li>
</ul>

<h2>Release 0.8.12 (2024/03/31)</h2>
Expand Down