Support suspending handler methods in Spring MVC

This commit adds support for Kotlin Coroutines suspending functions to
Spring MVC, by converting those to a Mono that can then be handled by
the asynchronous request processing feature.

It also optimizes Coroutines detection with the introduction of an
optimized KotlinDetector.isSuspendingFunction() method that does not
require kotlin-reflect.

Closes gh-23611
This commit is contained in:
Sébastien Deleuze
2020-10-09 12:22:15 +02:00
parent fad7243733
commit 94a42a3086
13 changed files with 110 additions and 29 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.core;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
@@ -74,4 +75,20 @@ public abstract class KotlinDetector {
return (kotlinMetadata != null && clazz.getDeclaredAnnotation(kotlinMetadata) != null);
}
/**
* Return {@code true} if the method is a suspending function.
*
* @author Sebastien Deleuze
* @since 5.3
*/
public static boolean isSuspendingFunction(Method method) {
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
Class<?>[] types = method.getParameterTypes();
if ((types.length > 0) && "kotlin.coroutines.Continuation".equals(types[types.length - 1].getName())) {
return true;
}
}
return false;
}
}