Skip to content

Commit

Permalink
Modified the name of isGetter to be changed by findImplicitPropertyName
Browse files Browse the repository at this point in the history
Because problems like FasterXML#340 occur when using findRenameByField
  • Loading branch information
k163377 committed Mar 4, 2023
1 parent 8e7aeb9 commit 80d289f
Showing 1 changed file with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ 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
}

// 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)
}
override fun findImplicitPropertyName(member: AnnotatedMember): String? {
if (!member.declaringClass.isKotlinClass()) return null

val name = member.name

return when (member) {
is AnnotatedMethod -> if (member.parameterCount == 0) {
// The reason for truncating after `-` is to truncate the random suffix
// given after the value class accessor name.
when {
name.startsWith("get") -> name.takeIf { it.contains("-") }?.let { _ ->
name.substringAfter("get")
.replaceFirstChar { it.lowercase(Locale.getDefault()) }
.substringBefore('-')
}
// since 2.15: 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 and
// https://github.com/FasterXML/jackson-module-kotlin/issues/340
// for details)
name.startsWith("is") -> if (name.contains("-")) name.substringAfter("-") else name
else -> null
}
} else null
is AnnotatedParameter -> findKotlinParameterName(member)
else -> null
}
return null
}

@Suppress("UNCHECKED_CAST")
Expand Down

0 comments on commit 80d289f

Please sign in to comment.