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

@ -25,7 +25,7 @@ ext {
linkScmDevConnection = "scm:git:ssh://git@github.com:spring-projects/spring-framework.git" linkScmDevConnection = "scm:git:ssh://git@github.com:spring-projects/spring-framework.git"
moduleProjects = subprojects.findAll { moduleProjects = subprojects.findAll {
!it.name.equals("spring-build-src") && !it.name.equals("spring-framework-bom") !it.name.equals("spring-build-src") && !it.name.equals("spring-framework-bom") && !it.name.equals("spring-core-coroutines")
} }
aspectjVersion = "1.9.2" aspectjVersion = "1.9.2"

View File

@ -5,6 +5,7 @@ include "spring-context"
include "spring-context-support" include "spring-context-support"
include "spring-context-indexer" include "spring-context-indexer"
include "spring-core" include "spring-core"
include "spring-core-coroutines"
include "spring-expression" include "spring-expression"
include "spring-instrument" include "spring-instrument"
include "spring-jcl" include "spring-jcl"

View File

@ -0,0 +1,9 @@
description = "Spring Core Coroutines support"
dependencies {
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
compile("io.projectreactor:reactor-core")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutinesVersion}")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${coroutinesVersion}")
}

View File

@ -14,10 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.reactive.result.method @file:JvmName("CoroutinesUtils")
package org.springframework.core
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactor.mono import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.Mono
import reactor.core.publisher.onErrorMap import reactor.core.publisher.onErrorMap
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method import java.lang.reflect.Method
@ -25,7 +31,23 @@ import kotlin.reflect.full.callSuspend
import kotlin.reflect.jvm.kotlinFunction import kotlin.reflect.jvm.kotlinFunction
/** /**
* Invoke an handler method converting suspending method to {@link Mono} if necessary. * Convert a [Deferred] instance to a [Mono] one.
*
* @author Sebastien Deleuze
* @since 5.2
*/
internal fun <T: Any> deferredToMono(source: Deferred<T>) = GlobalScope.mono { source.await() }
/**
* Convert a [Mono] instance to a [Deferred] one.
*
* @author Sebastien Deleuze
* @since 5.2
*/
internal fun <T: Any> monoToDeferred(source: Mono<T>) = GlobalScope.async { source.awaitFirstOrNull() }
/**
* Invoke an handler method converting suspending method to [Mono] if necessary.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 5.2 * @since 5.2

View File

@ -17,6 +17,9 @@ configurations {
jarjar jarjar
cglib cglib
objenesis objenesis
coroutines {
transitive = false
}
} }
task cglibRepackJar(type: Jar) { repackJar -> task cglibRepackJar(type: Jar) { repackJar ->
@ -65,10 +68,12 @@ dependencies {
cglib("cglib:cglib:${cglibVersion}@jar") cglib("cglib:cglib:${cglibVersion}@jar")
objenesis("org.objenesis:objenesis:${objenesisVersion}@jar") objenesis("org.objenesis:objenesis:${objenesisVersion}@jar")
jarjar("org.pantsbuild:jarjar:1.7.2") jarjar("org.pantsbuild:jarjar:1.7.2")
coroutines(project(":spring-core-coroutines"))
compile(files(cglibRepackJar)) compile(files(cglibRepackJar))
compile(files(objenesisRepackJar)) compile(files(objenesisRepackJar))
compile(project(":spring-jcl")) compile(project(":spring-jcl"))
compileOnly(project(":spring-core-coroutines"))
optional("net.sf.jopt-simple:jopt-simple:5.0.4") optional("net.sf.jopt-simple:jopt-simple:5.0.4")
optional("org.aspectj:aspectjweaver:${aspectjVersion}") optional("org.aspectj:aspectjweaver:${aspectjVersion}")
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
@ -78,8 +83,6 @@ dependencies {
optional("io.reactivex:rxjava-reactive-streams:${rxjavaAdapterVersion}") optional("io.reactivex:rxjava-reactive-streams:${rxjavaAdapterVersion}")
optional("io.reactivex.rxjava2:rxjava:${rxjava2Version}") optional("io.reactivex.rxjava2:rxjava:${rxjava2Version}")
optional("io.netty:netty-buffer") optional("io.netty:netty-buffer")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutinesVersion}")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${coroutinesVersion}")
testCompile("io.projectreactor:reactor-test") testCompile("io.projectreactor:reactor-test")
testCompile("org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}") testCompile("org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}")
testCompile("org.xmlunit:xmlunit-matchers:2.6.2") testCompile("org.xmlunit:xmlunit-matchers:2.6.2")
@ -87,6 +90,7 @@ dependencies {
testCompile("com.fasterxml.woodstox:woodstox-core:5.2.0") { testCompile("com.fasterxml.woodstox:woodstox-core:5.2.0") {
exclude group: "stax", module: "stax-api" exclude group: "stax", module: "stax-api"
} }
testCompile(project(":spring-core-coroutines"))
} }
jar { jar {
@ -107,4 +111,6 @@ jar {
from(zipTree(objenesisRepackJar.archivePath)) { from(zipTree(objenesisRepackJar.archivePath)) {
include "org/springframework/objenesis/**" include "org/springframework/objenesis/**"
} }
from { configurations.coroutines.collect { it.isDirectory() ? it : zipTree(it) } }
} }

View File

@ -25,6 +25,8 @@ import java.util.function.Function;
import io.reactivex.BackpressureStrategy; import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import kotlinx.coroutines.CompletableDeferredKt;
import kotlinx.coroutines.Deferred;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -93,7 +95,7 @@ public class ReactiveAdapterRegistry {
// Coroutines // Coroutines
if (ClassUtils.isPresent("kotlinx.coroutines.Deferred", classLoader)) { if (ClassUtils.isPresent("kotlinx.coroutines.Deferred", classLoader)) {
CoroutinesRegistrarKt.registerAdapter(this); new CoroutinesRegistrar().registerAdapters(this);
} }
} }
@ -324,4 +326,16 @@ public class ReactiveAdapterRegistry {
} }
} }
private static class CoroutinesRegistrar {
@SuppressWarnings("KotlinInternalInJava")
void registerAdapters(ReactiveAdapterRegistry registry) {
registry.registerReactiveType(
ReactiveTypeDescriptor.singleOptionalValue(Deferred.class, () -> CompletableDeferredKt.CompletableDeferred(null)),
source -> CoroutinesUtils.deferredToMono((Deferred<?>) source),
source -> CoroutinesUtils.monoToDeferred(Mono.from(source)));
}
}
} }

View File

@ -1,38 +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.core
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.toMono
/**
* Register Reactive adapters for Coroutines types.
*
* @author Sebastien Deleuze
* @since 5.2
*/
internal fun registerAdapter(registry: ReactiveAdapterRegistry) {
registry.registerReactiveType(
ReactiveTypeDescriptor.singleOptionalValue(Deferred::class.java) { GlobalScope.async {} },
{ source -> GlobalScope.mono { (source as Deferred<*>).await() }},
{ source -> GlobalScope.async { source.toMono().awaitFirstOrNull() } }
)
}

View File

@ -13,6 +13,7 @@ dependencies {
compile(project(":spring-core")) compile(project(":spring-core"))
compile(project(":spring-web")) compile(project(":spring-web"))
compile("io.projectreactor:reactor-core") compile("io.projectreactor:reactor-core")
compileOnly(project(":spring-core-coroutines"))
optional(project(":spring-context")) optional(project(":spring-context"))
optional(project(":spring-context-support")) // for FreeMarker support optional(project(":spring-context-support")) // for FreeMarker support
optional("javax.servlet:javax.servlet-api:4.0.1") optional("javax.servlet:javax.servlet-api:4.0.1")
@ -55,6 +56,7 @@ dependencies {
testCompile("org.eclipse.jetty:jetty-reactive-httpclient:1.0.2") testCompile("org.eclipse.jetty:jetty-reactive-httpclient:1.0.2")
testCompile("com.squareup.okhttp3:mockwebserver:3.14.0") testCompile("com.squareup.okhttp3:mockwebserver:3.14.0")
testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}") testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
testCompile(project(":spring-core-coroutines"))
testRuntime("org.jetbrains.kotlin:kotlin-script-util:${kotlinVersion}") testRuntime("org.jetbrains.kotlin:kotlin-script-util:${kotlinVersion}")
testRuntime("org.jetbrains.kotlin:kotlin-compiler:${kotlinVersion}") testRuntime("org.jetbrains.kotlin:kotlin-compiler:${kotlinVersion}")
testRuntime("org.jruby:jruby:9.2.6.0") testRuntime("org.jruby:jruby:9.2.6.0")

View File

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