@@ -24,35 +24,57 @@ import kotlin.reflect.full.hasAnnotation
24
24
import kotlin.reflect.full.memberProperties
25
25
import kotlin.reflect.full.primaryConstructor
26
26
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
27
+ import kotlin.reflect.jvm.javaGetter
27
28
import kotlin.reflect.jvm.javaType
28
29
import kotlin.reflect.jvm.kotlinFunction
29
30
30
- internal class KotlinNamesAnnotationIntrospector (val module : KotlinModule , val cache : ReflectionCache , val ignoredClassesForImplyingJsonCreator : Set <KClass <* >>) : NopAnnotationIntrospector() {
31
+ internal class KotlinNamesAnnotationIntrospector (
32
+ val module : KotlinModule ,
33
+ val cache : ReflectionCache ,
34
+ val ignoredClassesForImplyingJsonCreator : Set <KClass <* >>,
35
+ val useKotlinPropertyNameForGetter : Boolean
36
+ ) : NopAnnotationIntrospector() {
37
+ private fun getterNameFromJava (member : AnnotatedMethod ): String? {
38
+ val name = member.name
39
+
40
+ // The reason for truncating after `-` is to truncate the random suffix
41
+ // given after the value class accessor name.
42
+ return when {
43
+ name.startsWith(" get" ) -> name.takeIf { it.contains(" -" ) }?.let { _ ->
44
+ name.substringAfter(" get" )
45
+ .replaceFirstChar { it.lowercase(Locale .getDefault()) }
46
+ .substringBefore(' -' )
47
+ }
48
+ // since 2.15: support Kotlin's way of handling "isXxx" backed properties where
49
+ // logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
50
+ // (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
51
+ // https://github.com/FasterXML/jackson-databind/issues/2527 and
52
+ // https://github.com/FasterXML/jackson-module-kotlin/issues/340
53
+ // for details)
54
+ name.startsWith(" is" ) -> if (name.contains(" -" )) name.substringAfter(" -" ) else name
55
+ else -> null
56
+ }
57
+ }
58
+
59
+ private fun getterNameFromKotlin (member : AnnotatedMethod ): String? {
60
+ val getter = member.member
61
+
62
+ return member.member.declaringClass.takeIf { it.isKotlinClass() }?.let { clazz ->
63
+ clazz.kotlin.memberProperties.find { it.javaGetter == getter }
64
+ ?.let { it.name }
65
+ }
66
+ }
67
+
31
68
// since 2.4
32
69
override fun findImplicitPropertyName (member : AnnotatedMember ): String? {
33
70
if (! member.declaringClass.isKotlinClass()) return null
34
71
35
- val name = member.name
36
-
37
72
return when (member) {
38
73
is AnnotatedMethod -> if (member.parameterCount == 0 ) {
39
- // The reason for truncating after `-` is to truncate the random suffix
40
- // given after the value class accessor name.
41
- when {
42
- name.startsWith(" get" ) -> name.takeIf { it.contains(" -" ) }?.let { _ ->
43
- name.substringAfter(" get" )
44
- .replaceFirstChar { it.lowercase(Locale .getDefault()) }
45
- .substringBefore(' -' )
46
- }
47
- // since 2.15: support Kotlin's way of handling "isXxx" backed properties where
48
- // logical property name needs to remain "isXxx" and not become "xxx" as with Java Beans
49
- // (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html and
50
- // https://github.com/FasterXML/jackson-databind/issues/2527 and
51
- // https://github.com/FasterXML/jackson-module-kotlin/issues/340
52
- // for details)
53
- name.startsWith(" is" ) -> if (name.contains(" -" )) name.substringAfter(" -" ) else name
54
- else -> null
55
- }
74
+ if (useKotlinPropertyNameForGetter) {
75
+ // Fall back to default if it is a getter-like function
76
+ getterNameFromKotlin(member) ? : getterNameFromJava(member)
77
+ } else getterNameFromJava(member)
56
78
} else null
57
79
is AnnotatedParameter -> findKotlinParameterName(member)
58
80
else -> null
0 commit comments