Skip to content

Commit 9cba904

Browse files
committed
Refactor findDefaultCreator
1 parent a15c278 commit 9cba904

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

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)