Skip to content

Commit 07b0296

Browse files
authored
Fix #284 (alt implementation for #80) (#287)
1 parent 1b2c942 commit 07b0296

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

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

+23-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package com.fasterxml.jackson.module.kotlin
22

33
import com.fasterxml.jackson.annotation.JsonCreator
44
import com.fasterxml.jackson.annotation.JsonProperty
5+
import com.fasterxml.jackson.databind.PropertyName
6+
import com.fasterxml.jackson.databind.cfg.MapperConfig
57
import com.fasterxml.jackson.databind.introspect.*
8+
import com.fasterxml.jackson.databind.util.BeanUtil
69
import java.lang.reflect.Constructor
710
import java.lang.reflect.Method
811
import kotlin.reflect.KClass
@@ -16,7 +19,6 @@ import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
1619
import kotlin.reflect.jvm.javaType
1720
import kotlin.reflect.jvm.kotlinFunction
1821

19-
2022
internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>) : NopAnnotationIntrospector() {
2123
/*
2224
override public fun findNameForDeserialization(annotated: Annotated?): PropertyName? {
@@ -29,14 +31,22 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
2931
override fun findImplicitPropertyName(member: AnnotatedMember): String? {
3032
if (member is AnnotatedParameter) {
3133
return findKotlinParameterName(member)
32-
} else if (member is AnnotatedMethod) {
33-
// 25-Oct-2019: [module-kotlin#80] Match "isGetter" with field with same name
34-
// since Kotlin generates accessor different from Java
35-
if (member.declaringClass.isKotlinClass()) {
36-
if (cache.isKotlinGeneratedMethod(member) { it.declaringClass.declaredFields.any {
37-
f -> f.name.startsWith("is") && f.name == member.name } }) {
38-
return member.name
39-
}
34+
}
35+
return null
36+
}
37+
38+
// since 2.11: support Kotlin's way of handling "isXxx" backed properties where
39+
// logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
40+
// (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
41+
// https://github.com/FasterXML/jackson-databind/issues/2527
42+
// for details)
43+
override fun findRenameByField(config: MapperConfig<*>,
44+
field: AnnotatedField, implName: PropertyName): PropertyName? {
45+
val origSimple = implName.simpleName
46+
if (field.declaringClass.isKotlinClass() && origSimple.startsWith("is")) {
47+
val mangledName: String? = BeanUtil.stdManglePropertyName(origSimple, 2)
48+
if ((mangledName != null) && !mangledName.equals(origSimple)) {
49+
return PropertyName.construct(mangledName)
4050
}
4151
}
4252
return null
@@ -48,10 +58,10 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
4858

4959
if (member is AnnotatedConstructor && !member.declaringClass.isEnum) {
5060
// if has parameters, is a Kotlin class, and the parameters all have parameter annotations, then pretend we have a JsonCreator
51-
if (member.getParameterCount() > 0 && member.getDeclaringClass().isKotlinClass()) {
61+
if (member.getParameterCount() > 0 && member.declaringClass.isKotlinClass()) {
5262
return cache.checkConstructorIsCreatorAnnotated(member) {
53-
val kClass = cache.kotlinFromJava(member.getDeclaringClass() as Class<Any>)
54-
val kConstructor = cache.kotlinFromJava(member.getAnnotated() as Constructor<Any>)
63+
val kClass = cache.kotlinFromJava(member.declaringClass as Class<Any>)
64+
val kConstructor = cache.kotlinFromJava(member.annotated as Constructor<Any>)
5565

5666
if (kConstructor != null) {
5767
val isPrimaryConstructor = kClass.primaryConstructor == kConstructor ||
@@ -107,7 +117,7 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
107117

108118
@Suppress("UNCHECKED_CAST")
109119
protected fun findKotlinParameterName(param: AnnotatedParameter): String? {
110-
if (param.getDeclaringClass().isKotlinClass()) {
120+
if (param.declaringClass.isKotlinClass()) {
111121
val member = param.getOwner().getMember()
112122
val name = if (member is Constructor<*>) {
113123
val ctor = (member as Constructor<Any>)

0 commit comments

Comments
 (0)