Skip to content

Commit

Permalink
Refactor findDefaultCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed Nov 23, 2024
1 parent a15c278 commit 9cba904
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,11 @@ internal class KotlinNamesAnnotationIntrospector(
): PotentialCreator? {
val kClass = valueClass.creatableKotlinClass() ?: return null

val propertyNames = kClass.memberProperties.map { it.name }.toSet()

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

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

// By default, the primary constructor or the only publicly available constructor may be used
private fun KClass<*>.primarilyConstructor() = primaryConstructor ?: constructors.singleOrNull()

private fun KFunction<*>.isPossibleCreator(propertyNames: Set<String>): Boolean = 0 < parameters.size
&& !isPossibleSingleString(propertyNames)
&& parameters.none { it.name == null }
Expand Down

0 comments on commit 9cba904

Please sign in to comment.