From f6f657001d022e9dc6998047e46c54df45b2f7e5 Mon Sep 17 00:00:00 2001 From: Thomas Heigl Date: Wed, 5 Apr 2023 10:41:13 +0200 Subject: [PATCH] #864 Fix serialization of Kotlin lambdas --- .../esotericsoftware/kryo/util/DefaultGenerics.java | 5 +++-- .../{LambdaTest.kt => SerializationTest.kt} | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) rename test-kotlin/com.esotericsoftware.kryo/{LambdaTest.kt => SerializationTest.kt} (90%) diff --git a/src/com/esotericsoftware/kryo/util/DefaultGenerics.java b/src/com/esotericsoftware/kryo/util/DefaultGenerics.java index eeb22735f..7b07f0f18 100644 --- a/src/com/esotericsoftware/kryo/util/DefaultGenerics.java +++ b/src/com/esotericsoftware/kryo/util/DefaultGenerics.java @@ -93,8 +93,9 @@ public Class nextGenericClass () { @Override public int pushTypeVariables (GenericsHierarchy hierarchy, GenericType[] args) { - // Do not store type variables if hierarchy is empty or we do not have arguments for all root parameters. - if (hierarchy.total == 0 || hierarchy.rootTotal > args.length) return 0; + // Do not store type variables if hierarchy is empty, or we do not have arguments for all root parameters, + // or we have more arguments than the hierarchy has parameters. + if (hierarchy.total == 0 || hierarchy.rootTotal > args.length || args.length > hierarchy.counts.length) return 0; int startSize = this.argumentsSize; diff --git a/test-kotlin/com.esotericsoftware.kryo/LambdaTest.kt b/test-kotlin/com.esotericsoftware.kryo/SerializationTest.kt similarity index 90% rename from test-kotlin/com.esotericsoftware.kryo/LambdaTest.kt rename to test-kotlin/com.esotericsoftware.kryo/SerializationTest.kt index d6f504f31..dbd9bec5f 100644 --- a/test-kotlin/com.esotericsoftware.kryo/LambdaTest.kt +++ b/test-kotlin/com.esotericsoftware.kryo/SerializationTest.kt @@ -4,16 +4,12 @@ import com.esotericsoftware.kryo.io.Input import com.esotericsoftware.kryo.io.Output import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNotSame -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test -class LambdaTest { - class Example(private val p: (Long) -> String) { - constructor() : this({ it.toString() }) - } +class SerializationTest { + // https://github.com/EsotericSoftware/kryo/issues/864 @Test - @Disabled("Expected to fail") fun testLambda() { val kryo = Kryo().apply { isRegistrationRequired = false @@ -33,5 +29,9 @@ class LambdaTest { assertEquals(example::class.java, deserialized::class.java) assertNotSame(example, deserialized) } + + class Example(private val p: (Long) -> String) { + constructor() : this({ it.toString() }) + } }