Skip to content

Commit ff27189

Browse files
committed
Detect unsupported suspending handler methods in Spring MVC
closes gh-23585
1 parent f54395b commit ff27189

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
import javax.servlet.ServletException;
3535
import javax.servlet.http.HttpServletRequest;
3636

37+
import kotlin.reflect.KFunction;
38+
import kotlin.reflect.jvm.ReflectJvmMapping;
39+
3740
import org.springframework.aop.support.AopUtils;
3841
import org.springframework.beans.factory.BeanFactoryUtils;
3942
import org.springframework.beans.factory.InitializingBean;
43+
import org.springframework.core.KotlinDetector;
4044
import org.springframework.core.MethodIntrospector;
4145
import org.springframework.lang.Nullable;
4246
import org.springframework.util.Assert;
@@ -585,6 +589,10 @@ public void releaseReadLock() {
585589
}
586590

587591
public void register(T mapping, Object handler, Method method) {
592+
// Assert that the handler method is not a suspending one.
593+
if (KotlinDetector.isKotlinType(method.getDeclaringClass()) && KotlinDelegate.isSuspend(method)) {
594+
throw new IllegalStateException("Unsupported suspending handler method detected: " + method);
595+
}
588596
this.readWriteLock.writeLock().lock();
589597
try {
590598
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
@@ -793,4 +801,15 @@ public void handle() {
793801
}
794802
}
795803

804+
/**
805+
* Inner class to avoid a hard dependency on Kotlin at runtime.
806+
*/
807+
private static class KotlinDelegate {
808+
809+
static private boolean isSuspend(Method method) {
810+
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
811+
return function != null && function.isSuspend();
812+
}
813+
}
814+
796815
}

0 commit comments

Comments
 (0)