Skip to content

Make Kotlin functions accessible in CoroutinesUtils #23840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import kotlinx.coroutines.reactor.asFlux
import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.Mono
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.reflect.KFunction
import kotlin.reflect.full.callSuspend
import kotlin.reflect.jvm.kotlinFunction

/**
* Convert a [Deferred] instance to a [Mono] one.
Expand Down Expand Up @@ -58,8 +57,7 @@ internal fun <T: Any> monoToDeferred(source: Mono<T>) =
* @since 5.2
*/
@Suppress("UNCHECKED_CAST")
internal fun invokeSuspendingFunction(method: Method, bean: Any, vararg args: Any?): Any? {
val function = method.kotlinFunction!!
internal fun invokeSuspendingFunction(function: KFunction<*>, bean: Any, vararg args: Any?): Any? {
return if (function.isSuspend) {
val mono = mono(Dispatchers.Unconfined) {
function.callSuspend(bean, *args.sliceArray(0..(args.size-2)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

@file:JvmName("KotlinReflectionUtils")
package org.springframework.util

import java.lang.reflect.Method
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.kotlinFunction

/**
* Convert a [Method] to a [KFunction].
*
* @author Michael Gmeiner
* @since 5.2
*/
internal fun methodToFunction(method: Method) = method.kotlinFunction
?: throw IllegalStateException("Could not convert Java method to Kotlin function")

/**
* Make the given [KFunction] accessible if necessary.
* The [isAccessible] setter is only called when actually necessary, to avoid unnecessary conflicts with a JVM
* SecurityManager (if active).
*
* @author Michael Gmeiner
* @since 5.2
*/
internal fun makeFunctionAccessible(function: KFunction<*>) {
if (!function.isAccessible) {
function.isAccessible = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.stream.Stream;

import kotlin.reflect.KFunction;
import reactor.core.publisher.Mono;

import org.springframework.core.CoroutinesUtils;
Expand All @@ -37,6 +38,7 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.HandlerMethod;
import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException;
import org.springframework.util.KotlinReflectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

Expand Down Expand Up @@ -129,11 +131,13 @@ public Mono<Object> invoke(Message<?> message, Object... providedArgs) {
Object value;
try {
Method method = getBridgedMethod();
ReflectionUtils.makeAccessible(method);
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(method.getDeclaringClass())) {
value = CoroutinesUtils.invokeSuspendingFunction(method, getBean(), args);
KFunction<?> function = KotlinReflectionUtils.methodToFunction(method);
KotlinReflectionUtils.makeFunctionAccessible(function);
value = CoroutinesUtils.invokeSuspendingFunction(function, getBean(), args);
}
else {
ReflectionUtils.makeAccessible(method);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for this has moved to the constructor of HandlerMethod so this needs to be removed from here.

value = method.invoke(getBean(), args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.stream.Stream;

import kotlin.reflect.KFunction;
import reactor.core.publisher.Mono;

import org.springframework.core.CoroutinesUtils;
Expand All @@ -36,6 +37,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.KotlinReflectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.method.HandlerMethod;
Expand Down Expand Up @@ -137,12 +139,14 @@ public Mono<HandlerResult> invoke(
return getMethodArgumentValues(exchange, bindingContext, providedArgs).flatMap(args -> {
Object value;
try {
ReflectionUtils.makeAccessible(getBridgedMethod());
Method method = getBridgedMethod();
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(method.getDeclaringClass())) {
value = CoroutinesUtils.invokeSuspendingFunction(method, getBean(), args);
KFunction<?> function = KotlinReflectionUtils.methodToFunction(method);
KotlinReflectionUtils.makeFunctionAccessible(function);
value = CoroutinesUtils.invokeSuspendingFunction(function, getBean(), args);
}
else {
ReflectionUtils.makeAccessible(method);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, the code for this has moved to the constructor of HandlerMethod so this needs to be removed from here.

value = method.invoke(getBean(), args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class KotlinInvocableHandlerMethodTests {
.verifyComplete()
}

class CoroutinesController {
private class CoroutinesController {

suspend fun singleArg(q: String?): String {
delay(10)
Expand All @@ -146,7 +146,5 @@ class KotlinInvocableHandlerMethodTests {
delay(10)
response.headers.add("foo", "bar")
}


}
}
}