Skip to content

Avoid Kotlin reflection in MethodParameter for non-suspending functions - #37366

Closed
Asthenia0412 wants to merge 1 commit into
spring-projects:mainfrom
Asthenia0412:fix/methodparameter-kotlin-reflection-guard
Closed

Asthenia0412 wants to merge 1 commit into
spring-projects:mainfrom
Asthenia0412:fix/methodparameter-kotlin-reflection-guard

Conversation

@Asthenia0412

Copy link
Copy Markdown

Overview

MethodParameter.getGenericParameterType() and computeParameterType() resolve the return type of any Kotlin method through KotlinDelegate, which calls ReflectJvmMapping.getKotlinFunction(method) solely to check KFunction.isSuspend(). That lookup materializes all members of the declaring KClass and is repeated for every method whose return type is resolved, including the many non-suspending ones.

Changes

Guard KotlinDelegate.getGenericReturnType(...) and getReturnType(...) with KotlinDetector.isSuspendingFunction(method). A suspending function always compiles to a JVM method whose last parameter is kotlin.coroutines.Continuation, which isSuspendingFunction checks without Kotlin reflection. Suspending functions still go through the KFunction path; regular (non-suspending) Kotlin methods now fall back to Java reflection immediately.

Related Issue

MethodParameter.getGenericParameterType() and computeParameterType()
resolved the return type of any Kotlin method through KotlinDelegate,
which calls ReflectJvmMapping.getKotlinFunction(method) solely to check
KFunction.isSuspend(). That lookup materializes all members of the
declaring KClass and is repeated for every method whose return type is
resolved, including the many non-suspending ones.

A suspending function always compiles to a JVM method whose last
parameter is kotlin.coroutines.Continuation, which
KotlinDetector.isSuspendingFunction(Method) already checks without
Kotlin reflection. Guard KotlinDelegate.getGenericReturnType(...) and
getReturnType(...) with it so that regular Kotlin methods fall back to
Java reflection immediately, while suspending functions still go through
the KFunction path.

Closes spring-projectsgh-37300
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 1, 2026
@bclozel bclozel closed this Oct 1, 2026
@sbrannen sbrannen added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants