Skip to content

Commit ca2120b

Browse files
committed
Replace KFunction acquisition process with one corresponding to the value class.
1 parent bf266d3 commit ca2120b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
4747
?: key.kotlin.let { javaClassToKotlin.putIfAbsent(key, it) ?: it }
4848

4949
fun kotlinFromJava(key: Constructor<Any>): KFunction<Any>? = javaConstructorToKotlin.get(key)
50-
?: key.kotlinFunction?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
50+
?: key.getKotlinConstructor()?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
5151

5252
fun kotlinFromJava(key: Method): KFunction<*>? = javaMethodToKotlin.get(key)
5353
?: key.kotlinFunction?.let { javaMethodToKotlin.putIfAbsent(key, it) ?: it }
@@ -89,4 +89,25 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
8989

9090
fun isKotlinGeneratedMethod(key: AnnotatedMethod, calc: (AnnotatedMethod) -> Boolean): Boolean = kotlinGeneratedMethod.get(key)
9191
?: calc(key).let { kotlinGeneratedMethod.putIfAbsent(key, it) ?: it }
92+
93+
companion object {
94+
private fun Constructor<Any>.getKotlinConstructor(): KFunction<Any>? {
95+
kotlinFunction?.apply { return this }
96+
97+
// The javaConstructor that corresponds to the KFunction of the constructor that
98+
// takes value class as an argument is a synthetic constructor.
99+
// Therefore, in Kotlin 1.5.30, KFunction cannot be obtained from a constructor that is processed
100+
// by jackson-module-kotlin.
101+
// To deal with this situation, a synthetic constructor is obtained and a KFunction is obtained from it.
102+
return try {
103+
// The arguments of the synthetic constructor are the normal constructor arguments
104+
// with the DefaultConstructorMarker appended to the end.
105+
declaringClass
106+
.getDeclaredConstructor(*parameterTypes, Constants.DEFAULT_CONSTRUCTOR_MARKER)
107+
.kotlinFunction
108+
} catch (t: Throwable) {
109+
null
110+
}
111+
}
112+
}
92113
}

0 commit comments

Comments
 (0)