8000 Add filter for bytecode generated by Kotlin serialization compiler plugin by Godin · Pull Request #1885 · jacoco/jacoco · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add filter for bytecode generated by Kotlin serialization compiler plugin #1885

8000
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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions org.jacoco.core.test.validation.kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-core-jvm</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>

<build>
Expand All @@ -60,6 +65,18 @@
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2009, 2025 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.KotlinSerializableTarget;
import org.junit.Test;

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

public KotlinSerializableTest() {
super(KotlinSerializableTarget.class);
}

@Test
public void test_method_count() {
assertMethodCount(
/* main + 3 constructors + 3 getters + 1 method in companion */8);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2009, 2025 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 kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* Test target with [Serializable] class.
*/
object KotlinSerializableTarget {

@Serializable // assertEmpty()
data class Example( // assertFullyCovered()
@SerialName("d") val data: String // assertFullyCovered()
) // assertEmpty()

@Serializable(with = CustomSerializer::class) // assertEmpty()
data class ExampleWithCustomSerializer( // assertFullyCovered()
val data: String // assertFullyCovered()
) // assertEmpty()

object CustomSerializer : KSerializer<ExampleWithCustomSerializer> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Example", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: ExampleWithCustomSerializer) = encoder.encodeString(value.data)
override fun deserialize(decoder: Decoder): ExampleWithCustomSerializer =
ExampleWithCustomSerializer(decoder.decodeString())
}

data class ExampleWithHandWrittenCompanion(
val data: String
) {
companion object {
fun serializer(): KSerializer<ExampleWithCustomSerializer> = CustomSerializer // assertNotCovered()
< 10000 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> }
}

@JvmStatic
fun main(args: Array<String>) {
Example("").data
ExampleWithCustomSerializer("").data
ExampleWithHandWrittenCompanion("")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2009, 2025 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.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
* Unit test for {@link KotlinSerializableFilter}.
*/
public class KotlinSerializableFilterTest extends FilterTestBase {

private final IFilter filter = new KotlinSerializableFilter();

/**
* <pre>
* &#064;kotlinx.serialization.Serializable
* data class Example(val data: String)
* </pre>
*/
@Test
public void should_filter_synthetic_writeSelf_method() {
final MethodNode m = new MethodNode(
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC
| Opcodes.ACC_SYNTHETIC,
"write$Self$pkg",
"(Lpkg$Example;Lkotlinx/serialization/encoding/CompositeEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;)V",
null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

/**
* <pre>
* &#064;kotlinx.serialization.Serializable
* data class Example(val data: String)
* </pre>
*/
@Test
public void should_filter_synthetic_constructor() {
final MethodNode m = new MethodNode(
Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>",
"(ILjava/lang/String;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V",
null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

/**
* <pre>
* &#064;kotlinx.serialization.Serializable // line 1
* data class Example(val data: String)
* </pre>
*/
@Test
public void should_filter_generated_serializer_method() {
context.className = "Example$Companion";

final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
"<init>", "()V", null, null);
// no line numbers
filter.filter(initMethod, context, output);

final MethodNode m = new MethodNode(
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
"()Lkotlinx/serialization/KSerializer;",
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
final Label label0 = new Label();
m.visitLabel(label0);
m.visitLineNumber(1, label0);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$$serializer", "INSTANCE",
"LExample$$serializer;");
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

/**
* <pre>
* &#064;kotlinx.serialization.Serializable
* data class Example(val data: String) {
* companion object // line 2
* }
* </pre>
*/
@Test
public void should_filter_generated_serializer_method_in_hand_written_companion() {
context.className = "Example$Companion";

final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
"<init>", "()V", null, null);
final Label initMethodLineNumberLabel = new Label();
initMethod.visitLabel(initMethodLineNumberLabel);
initMethod.visitLineNumber(2, initMethodLineNumberLabel);
filter.filter(initMethod, context, output);

final MethodNode m = new MethodNode(
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
"()Lkotlinx/serialization/KSerializer;",
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
final Label label0 = new Label();
m.visitLabel(label0);
m.visitLineNumber(2, label0);
m.visitFieldInsn(Opcodes.GETSTATIC, "Example$$serializer", "INSTANCE",
"LExample$$serializer;");
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

/**
* <pre>
* data class Example(val data: String) {
* companion object { // line 2
* fun serializer(): KSerializer&lt;Example&gt; = CustomSerializer
* }
* }
* </pre>
*/
@Test
public void should_not_filter_hand_written_serializer_method() {
context.className = "Example$Companion";

final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
"<init>", "()V", null, null);
final Label initMethodLineNumberLabel = new Label();
initMethod.visitLabel(initMethodLineNumberLabel);
initMethod.visitLineNumber(2, initMethodLineNumberLabel);
filter.filter(initMethod, context, output);

final MethodNode m = new MethodNode(
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
"()Lkotlinx/serialization/KSerializer;",
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
final Label label0 = new Label();
m.visitLabel(label0);
m.visitLineNumber(3, label0);
m.visitFieldInsn(Opcodes.GETSTATIC, "CustomSerializer", "INSTANCE",
"LCustomSerializer;");
m.visitTypeInsn(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertIgnored(m);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2009, 2025 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.tree.MethodNode;

/**
* Filters methods generated by <a href=
* "https://github.com/JetBrains/kotlin/tree/v2.1.20/plugins/kotlinx-serialization">Kotlin
* serialization compiler plugin</a>.
*/
final class KotlinSerializableFilter implements IFilter {

public void filter(final MethodNode methodNode,
final IFilterContext context, final IFilterOutput output) {
// TODO
}

}
Loading
0