Skip to content

Commit

Permalink
Fixed to use definition names in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed Jan 10, 2023
1 parent a8323d3 commit b1d0761
Showing 1 changed file with 12 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.PropertyName
import com.fasterxml.jackson.databind.cfg.MapperConfig
import com.fasterxml.jackson.databind.introspect.Annotated
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor
import com.fasterxml.jackson.databind.introspect.AnnotatedField
import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector
import com.fasterxml.jackson.databind.util.BeanUtil
import kotlinx.metadata.jvm.fieldSignature
import kotlinx.metadata.jvm.getterSignature
import java.lang.reflect.Constructor
import java.lang.reflect.Method
import java.util.Locale
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
Expand All @@ -30,35 +28,18 @@ import kotlin.reflect.jvm.kotlinFunction
internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>) : NopAnnotationIntrospector() {
// since 2.4
override fun findImplicitPropertyName(member: AnnotatedMember): String? = when (member) {
is AnnotatedMethod -> if (member.name.contains('-') && member.parameterCount == 0) {
when {
member.name.startsWith("get") -> member.name.substringAfter("get")
member.name.startsWith("is") -> member.name.substringAfter("is")
else -> null
}?.replaceFirstChar { it.lowercase(Locale.getDefault()) }?.substringBefore('-')
} else null
is AnnotatedParameter -> findKotlinParameterName(member)
else -> null
}
is AnnotatedMethod -> member.annotated.declaringClass.toKmClass()?.let { kmClass ->
val methodSignature = member.annotated.toSignature()

// since 2.11: support Kotlin's way of handling "isXxx" backed properties where
// logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
// (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
// https://github.com/FasterXML/jackson-databind/issues/2527
// for details)
override fun findRenameByField(
config: MapperConfig<*>,
field: AnnotatedField,
implName: PropertyName
): PropertyName? {
val origSimple = implName.simpleName
if (field.declaringClass.isKotlinClass() && origSimple.startsWith("is")) {
val mangledName: String? = BeanUtil.stdManglePropertyName(origSimple, 2)
if ((mangledName != null) && !mangledName.equals(origSimple)) {
return PropertyName.construct(mangledName)
}
kmClass.properties.find { it.getterSignature == methodSignature }?.name
}
return null
is AnnotatedField -> member.annotated.declaringClass.toKmClass()?.let { kmClass ->
val fieldSignature = member.annotated.toSignature()

kmClass.properties.find { it.fieldSignature == fieldSignature }?.name
}
is AnnotatedParameter -> findKotlinParameterName(member)
else -> null
}

@Suppress("UNCHECKED_CAST")
Expand Down

0 comments on commit b1d0761

Please sign in to comment.