Skip to content

Commit 3f259ba

Browse files
authored
Fix NoSuchMethodException in runtime for Kotlin 1.4 (#557)
Issue #556
1 parent cbd8364 commit 3f259ba

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

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

+31-25
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,40 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
6565
// Find a serializer to handle the case where the getter returns an unboxed value from the value class.
6666
override fun findSerializer(am: Annotated): StdSerializer<*>? = when (am) {
6767
is AnnotatedMethod -> {
68-
val getter = am.member.apply {
69-
// If the return value of the getter is a value class,
70-
// it will be serialized properly without doing anything.
71-
if (this.returnType.isUnboxableValueClass()) return null
72-
}
73-
74-
val kotlinProperty = getter
75-
.declaringClass
76-
.kotlin
77-
.let {
78-
// KotlinReflectionInternalError is raised in GitHub167 test,
79-
// but it looks like an edge case, so it is ignored.
80-
try {
81-
it.memberProperties
82-
} catch (e: Error) {
83-
null
68+
when (KotlinVersion.CURRENT.isAtLeast(1, 5)) {
69+
true -> {
70+
val getter = am.member.apply {
71+
// If the return value of the getter is a value class,
72+
// it will be serialized properly without doing anything.
73+
if (this.returnType.isUnboxableValueClass()) return null
8474
}
85-
}?.find { it.javaGetter == getter }
8675

87-
(kotlinProperty?.returnType?.classifier as? KClass<*>)
88-
?.takeIf { it.isValue }
89-
?.java
90-
?.let { outerClazz ->
91-
val innerClazz = getter.returnType
92-
93-
ValueClassStaticJsonValueSerializer.createdOrNull(outerClazz, innerClazz)
94-
?: @Suppress("UNCHECKED_CAST") ValueClassBoxSerializer(outerClazz, innerClazz)
76+
val kotlinProperty = getter
77+
.declaringClass
78+
.kotlin
79+
.let {
80+
// KotlinReflectionInternalError is raised in GitHub167 test,
81+
// but it looks like an edge case, so it is ignored.
82+
try {
83+
it.memberProperties
84+
} catch (e: Error) {
85+
null
86+
}
87+
}?.find { it.javaGetter == getter }
88+
89+
(kotlinProperty?.returnType?.classifier as? KClass<*>)
90+
?.takeIf { it.isValue }
91+
?.java
92+
?.let { outerClazz ->
93+
val innerClazz = getter.returnType
94+
95+
ValueClassStaticJsonValueSerializer.createdOrNull(outerClazz, innerClazz)
96+
?: @Suppress("UNCHECKED_CAST") ValueClassBoxSerializer(outerClazz, innerClazz)
97+
}
9598
}
99+
// Kotlin 1.4 and lower doesn't have value classes and we avoid the NoSuchMethodException on it.isValue
100+
else -> null
101+
}
96102
}
97103
// Ignore the case of AnnotatedField, because JvmField cannot be set in the field of value class.
98104
else -> null

0 commit comments

Comments
 (0)