From 9cba9042b9bb8291583f46c5b960a94ff30df522 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 23 Nov 2024 23:19:29 +0900 Subject: [PATCH 1/2] Refactor findDefaultCreator --- .../kotlin/KotlinNamesAnnotationIntrospector.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt index 6489ed032..204ae7d48 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt @@ -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 { @@ -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): Boolean = 0 < parameters.size && !isPossibleSingleString(propertyNames) && parameters.none { it.name == null } From f603e8f34074358c94312f17bce6e4857cef526c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sat, 23 Nov 2024 23:27:16 +0900 Subject: [PATCH 2/2] Update release notes wrt #858 --- release-notes/CREDITS-2.x | 1 + release-notes/VERSION-2.x | 1 + 2 files changed, 2 insertions(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 18ad194b2..7769c55b5 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -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 diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 4c285a41c..bf44013f4 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -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.