Skip to content

Commit 89c4bd5

Browse files
authored
Fix NPE in KotlinSerializableFilter (#1970)
1 parent 0981128 commit 89c4bd5

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.test.validation.kotlin;
14+
15+
import org.jacoco.core.test.validation.ValidationTestBase;
16+
import org.jacoco.core.test.validation.kotlin.targets.KotlinSerializableEnumTarget;
17+
18+
/**
19+
* Test of code coverage in {@link KotlinSerializableEnumTarget}.
20+
*/
21+
public class KotlinSerializableEnumTest extends ValidationTestBase {
22+
23+
public KotlinSerializableEnumTest() {
24+
super(KotlinSerializableEnumTarget.class);
25+
}
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.test.validation.kotlin;
14+
15+
import org.jacoco.core.test.validation.ValidationTestBase;
16+
import org.jacoco.core.test.validation.kotlin.targets.KotlinSerializableSealedTarget;
17+
18+
/**
19+
* Test of code coverage in {@link KotlinSerializableSealedTarget}.
20+
*/
21+
public class KotlinSerializableSealedTest extends ValidationTestBase {
22+
23+
public KotlinSerializableSealedTest() {
24+
super(KotlinSerializableSealedTarget.class);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.test.validation.kotlin.targets
14+
15+
import kotlinx.serialization.Serializable
16+
17+
/**
18+
* Test target with [Serializable] `enum class`.
19+
*/
20+
object KotlinSerializableEnumTarget {
21+
22+
@Serializable
23+
enum class E {
24+
V
25+
}
26+
27+
@JvmStatic
28+
fun main(args: Array<String>) {
29+
E.V
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2025 Mountainminds GmbH & Co. KG and Contributors
3+
* This program and the accompanying materials are made available under
4+
* the terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.core.test.validation.kotlin.targets
14+
15+
import kotlinx.serialization.Serializable
16+
17+
/**
18+
* Test target with [Serializable] `sealed class`.
19+
*/
20+
object KotlinSerializableSealedTarget {
21+
22+
@Serializable
23+
private sealed class Sealed {
24+
@Serializable
25+
data class A(val data: String): Sealed()
26+
}
27+
28+
@JvmStatic
29+
fun main(args: Array<String>) {
30+
Sealed.A("").data
31+
}
32+
33+
}

org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinSerializableFilterTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,48 @@ public void should_not_filter_hand_written_serializer_method() {
171171
assertIgnored(m);
172172
}
173173

174+
/**
175+
* <pre>
176+
* &#064;kotlinx.serialization.Serializable // line 1
177+
* enum class Example {
178+
* V
179+
* }
180+
* </pre>
181+
*
182+
* <pre>
183+
* &#064;kotlinx.serialization.Serializable // line 1
184+
* sealed class Example {
185+
* }
186+
* </pre>
187+
*/
188+
@Test
189+
public void should_filter_generated_serializer_method_in_companions_of_enum_and_sealed_class() {
190+
context.className = "Example$Companion";
191+
192+
final MethodNode initMethod = new MethodNode(Opcodes.ACC_PRIVATE,
193+
"<init>", "()V", null, null);
194+
final Label initMethodLineNumberLabel = new Label();
195+
initMethod.visitLabel(initMethodLineNumberLabel);
196+
initMethod.visitLineNumber(1, initMethodLineNumberLabel);
197+
filter.filter(initMethod, context, output);
198+
199+
final MethodNode m = new MethodNode(
200+
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "serializer",
201+
"()Lkotlinx/serialization/KSerializer;",
202+
"()Lkotlinx/serialization/KSerializer<LExample;>;", null);
203+
final Label label0 = new Label();
204+
m.visitLabel(label0);
205+
m.visitLineNumber(1, label0);
206+
m.visitVarInsn(Opcodes.ALOAD, 0);
207+
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "Example$Companion",
208+
"get$cachedSerializer", "()Lkotlinx/serialization/KSerializer;",
209+
false);
210+
m.visitInsn(Opcodes.ARETURN);
211+
212+
filter.filter(m, context, output);
213+
214+
// FIXME https://github.com/jacoco/jacoco/issues/1971
215+
assertIgnored(m);
216+
}
217+
174218
}

org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinSerializableFilter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public boolean match(final MethodNode methodNode,
7676
cursor = methodNode.instructions.getFirst();
7777
nextIs(Opcodes.GETSTATIC);
7878
final FieldInsnNode getStaticInstruction = (FieldInsnNode) cursor;
79+
if (cursor == null) {
80+
return false;
81+
}
7982
final AbstractInsnNode lineNumberInstruction = cursor.getPrevious();
8083
nextIsType(Opcodes.CHECKCAST, "kotlinx/serialization/KSerializer");
8184
nextIs(Opcodes.ARETURN);

org.jacoco.doc/docroot/doc/changes.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ <h3>New Features</h3>
5151
composition is filtered out during generation of report
5252
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1911">#1911</a>).</li>
5353
<li>Methods generated by the Kotlin serialization compiler plugin are filtered out
54-
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1885">#1885</a>).</li>
54+
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1885">#1885</a>,
55+
<a href="https://github.com/jacoco/jacoco/issues/1970">#1970</a>).</li>
5556
</ul>
5657

5758
<h3>Fixed bugs</h3>

0 commit comments

Comments
 (0)