From c1d4b610ca62733094bbe6d720aac4980fe264fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 7 Mar 2024 11:08:28 +0100 Subject: [PATCH] Refine Kotlin inline class optimizations Closes gh-32334 --- .../springframework/core/CoroutinesUtils.java | 19 ++++++++----------- .../support/InvocableHandlerMethod.java | 19 ++++++++----------- .../result/method/InvocableHandlerMethod.java | 19 ++++++++----------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java b/spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java index 687101c5b075..d36d59376fa5 100644 --- a/spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java +++ b/spring-core/src/main/java/org/springframework/core/CoroutinesUtils.java @@ -28,9 +28,11 @@ import kotlin.reflect.KClassifier; import kotlin.reflect.KFunction; import kotlin.reflect.KParameter; +import kotlin.reflect.KType; import kotlin.reflect.full.KCallables; import kotlin.reflect.full.KClasses; import kotlin.reflect.jvm.KCallablesJvm; +import kotlin.reflect.jvm.KTypesJvm; import kotlin.reflect.jvm.ReflectJvmMapping; import kotlinx.coroutines.BuildersKt; import kotlinx.coroutines.CoroutineStart; @@ -117,19 +119,14 @@ public static Publisher invokeSuspendingFunction(CoroutineContext context, Me case VALUE, EXTENSION_RECEIVER -> { Object arg = args[index]; if (!(parameter.isOptional() && arg == null)) { - if (parameter.getType().getClassifier() instanceof KClass kClass) { - Class javaClass = JvmClassMappingKt.getJavaClass(kClass); - if (KotlinDetector.isInlineClass(javaClass) - && !(parameter.getType().isMarkedNullable() && arg == null)) { - argMap.put(parameter, KClasses.getPrimaryConstructor(kClass).call(arg)); + KType type = parameter.getType(); + if (!(type.isMarkedNullable() && arg == null)) { + KClass kClass = KTypesJvm.getJvmErasure(type); + if (KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) { + arg = KClasses.getPrimaryConstructor(kClass).call(arg); } - else { - argMap.put(parameter, arg); - } - } - else { - argMap.put(parameter, arg); } + argMap.put(parameter, arg); } index++; } diff --git a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java index 5bdc816d23f7..918ce2226d7c 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java @@ -26,8 +26,10 @@ import kotlin.reflect.KClass; import kotlin.reflect.KFunction; import kotlin.reflect.KParameter; +import kotlin.reflect.KType; import kotlin.reflect.full.KClasses; import kotlin.reflect.jvm.KCallablesJvm; +import kotlin.reflect.jvm.KTypesJvm; import kotlin.reflect.jvm.ReflectJvmMapping; import org.springframework.context.MessageSource; @@ -315,19 +317,14 @@ public static Object invokeFunction(Method method, Object target, Object[] args) case VALUE, EXTENSION_RECEIVER -> { Object arg = args[index]; if (!(parameter.isOptional() && arg == null)) { - if (parameter.getType().getClassifier() instanceof KClass kClass) { - Class javaClass = JvmClassMappingKt.getJavaClass(kClass); - if (KotlinDetector.isInlineClass(javaClass) - && !(parameter.getType().isMarkedNullable() && arg == null)) { - argMap.put(parameter, KClasses.getPrimaryConstructor(kClass).call(arg)); + KType type = parameter.getType(); + if (!(type.isMarkedNullable() && arg == null)) { + KClass kClass = KTypesJvm.getJvmErasure(type); + if (KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) { + arg = KClasses.getPrimaryConstructor(kClass).call(arg); } - else { - argMap.put(parameter, arg); - } - } - else { - argMap.put(parameter, arg); } + argMap.put(parameter, arg); } index++; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java index bab33e84d9fc..de362ea27a91 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java @@ -31,8 +31,10 @@ import kotlin.reflect.KClass; import kotlin.reflect.KFunction; import kotlin.reflect.KParameter; +import kotlin.reflect.KType; import kotlin.reflect.full.KClasses; import kotlin.reflect.jvm.KCallablesJvm; +import kotlin.reflect.jvm.KTypesJvm; import kotlin.reflect.jvm.ReflectJvmMapping; import reactor.core.publisher.Mono; @@ -326,19 +328,14 @@ public static Object invokeFunction(Method method, Object target, Object[] args, case VALUE, EXTENSION_RECEIVER -> { Object arg = args[index]; if (!(parameter.isOptional() && arg == null)) { - if (parameter.getType().getClassifier() instanceof KClass kClass) { - Class javaClass = JvmClassMappingKt.getJavaClass(kClass); - if (KotlinDetector.isInlineClass(javaClass) - && !(parameter.getType().isMarkedNullable() && arg == null)) { - argMap.put(parameter, KClasses.getPrimaryConstructor(kClass).call(arg)); + KType type = parameter.getType(); + if (!(type.isMarkedNullable() && arg == null)) { + KClass kClass = KTypesJvm.getJvmErasure(type); + if (KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) { + arg = KClasses.getPrimaryConstructor(kClass).call(arg); } - else { - argMap.put(parameter, arg); - } - } - else { - argMap.put(parameter, arg); } + argMap.put(parameter, arg); } index++; }