diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 18ad194b..7769c55b 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 4c285a41..bf44013f 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. 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 6489ed03..204ae7d4 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 }