Introduce spring-core-coroutines module

This commit introduces the spring-core-coroutines module
in order to avoid referencing Kotlin code from Java one,
which is currently not supported by Eclipse.

During the build, spring-core-coroutines is merged into
spring-core, so this change is expected to have no impact
for end users.

This module contains functions accessible from Java via
the CoroutinesUtils class to adapt Coroutines and Deferred
instances to and from Mono.

See gh-19975
This commit is contained in:
Sebastien Deleuze
2019-03-25 21:41:03 +01:00
parent 837be3eaa0
commit 88a2729fba
9 changed files with 64 additions and 46 deletions

View File

@@ -26,6 +26,7 @@ import java.util.stream.Stream;
import reactor.core.publisher.Mono;
import org.springframework.core.CoroutinesUtils;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodParameter;
@@ -131,6 +132,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
* @param providedArgs optional list of argument values to match by type
* @return a Mono with a {@link HandlerResult}.
*/
@SuppressWarnings("KotlinInternalInJava")
public Mono<HandlerResult> invoke(
ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {
@@ -140,7 +142,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
ReflectionUtils.makeAccessible(getBridgedMethod());
Method method = getBridgedMethod();
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(method.getDeclaringClass())) {
value = InvocableHandlerMethodKt.invokeHandlerMethod(method, getBean(), args);
value = CoroutinesUtils.invokeHandlerMethod(method, getBean(), args);
}
else {
value = method.invoke(getBean(), args);

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.onErrorMap
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.reflect.full.callSuspend
import kotlin.reflect.jvm.kotlinFunction
/**
* Invoke an handler method converting suspending method to {@link Mono} if necessary.
*
* @author Sebastien Deleuze
* @since 5.2
*/
internal fun invokeHandlerMethod(method: Method, bean: Any, vararg args: Any?): Any? {
val function = method.kotlinFunction!!
return if (function.isSuspend) {
GlobalScope.mono { function.callSuspend(bean, *args.sliceArray(0..(args.size-2)))
.let { if (it == Unit) null else it} }
.onErrorMap(InvocationTargetException::class) { it.targetException }
}
else {
function.call(bean, *args)
}
}