Skip to content

Commit ed013a7

Browse files
authored
Merge pull request #858 from k163377/refactor-find-creator
Refactor findDefaultCreator
2 parents a15c278 + f603e8f commit ed013a7

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

release-notes/CREDITS-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Contributors:
1818
# 2.19.0 (not yet released)
1919

2020
WrongWrong (@k163377)
21+
* #858: Refactor findDefaultCreator
2122
* #839: Remove useKotlinPropertyNameForGetter and unify with kotlinPropertyNameAsImplicitName
2223
* #835: Remove old SingletonSupport class and unified with KotlinFeature.SingletonSupport
2324

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Co-maintainers:
1818

1919
2.19.0 (not yet released)
2020

21+
#858: Minor performance improvement of findDefaultCreator in edge cases.
2122
#839: Remove useKotlinPropertyNameForGetter and unify with kotlinPropertyNameAsImplicitName.
2223
#835: Remove old SingletonSupport class and unified with KotlinFeature.SingletonSupport.
2324

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,11 @@ internal class KotlinNamesAnnotationIntrospector(
9191
): PotentialCreator? {
9292
val kClass = valueClass.creatableKotlinClass() ?: return null
9393

94-
val propertyNames = kClass.memberProperties.map { it.name }.toSet()
95-
96-
val defaultCreator = kClass.let { _ ->
97-
// By default, the primary constructor or the only publicly available constructor may be used
98-
val ctor = kClass.primaryConstructor ?: kClass.constructors.takeIf { it.size == 1 }?.single()
99-
ctor?.takeIf { it.isPossibleCreator(propertyNames) }
100-
}
94+
val defaultCreator = kClass.primarilyConstructor()
95+
?.takeIf { ctor ->
96+
val propertyNames = kClass.memberProperties.map { it.name }.toSet()
97+
ctor.isPossibleCreator(propertyNames)
98+
}
10199
?: return null
102100

103101
return declaredConstructors.find {
@@ -115,6 +113,9 @@ private fun AnnotatedClass.creatableKotlinClass(): KClass<*>? = annotated
115113
.takeIf { it.isKotlinClass() && !it.isEnum }
116114
?.kotlin
117115

116+
// By default, the primary constructor or the only publicly available constructor may be used
117+
private fun KClass<*>.primarilyConstructor() = primaryConstructor ?: constructors.singleOrNull()
118+
118119
private fun KFunction<*>.isPossibleCreator(propertyNames: Set<String>): Boolean = 0 < parameters.size
119120
&& !isPossibleSingleString(propertyNames)
120121
&& parameters.none { it.name == null }

0 commit comments

Comments
 (0)