Skip to content

Commit

Permalink
Merge pull request #858 from k163377/refactor-find-creator
Browse files Browse the repository at this point in the history
Refactor findDefaultCreator
  • Loading branch information
k163377 authored Nov 23, 2024
2 parents a15c278 + f603e8f commit ed013a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.19.0 (not yet released)

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

Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Co-maintainers:

2.19.0 (not yet released)

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

Expand Down
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 ed013a7

Please sign in to comment.