Skip to content
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 @@ -16,7 +16,6 @@
package org.springframework.data.repository.core.support;

import kotlin.Unit;
import kotlin.reflect.KFunction;
import kotlinx.coroutines.flow.Flow;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -46,6 +45,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Greg Taube
* @since 2.4
* @see #forFragmentMethod(Method, Object, Method)
* @see #forRepositoryQuery(Method, RepositoryQuery)
Expand Down Expand Up @@ -319,15 +319,8 @@ public static CoroutineAdapterInformation create(Method declaredMethod, Method b
baseClassMethod.getParameterCount());
}

KFunction<?> declaredFunction = KotlinDetector.isKotlinType(declaredMethod.getDeclaringClass())
? KotlinReflectionUtils.findKotlinFunction(declaredMethod)
: null;
KFunction<?> baseClassFunction = KotlinDetector.isKotlinType(baseClassMethod.getDeclaringClass())
? KotlinReflectionUtils.findKotlinFunction(baseClassMethod)
: null;

boolean suspendedDeclaredMethod = declaredFunction != null && declaredFunction.isSuspend();
boolean suspendedBaseClassMethod = baseClassFunction != null && baseClassFunction.isSuspend();
boolean suspendedDeclaredMethod = KotlinReflectionUtils.isSuspend(declaredMethod);
boolean suspendedBaseClassMethod = KotlinReflectionUtils.isSuspend(baseClassMethod);
boolean reactiveBaseClassMethod = !suspendedBaseClassMethod
&& ReactiveWrapperConverters.supports(baseClassMethod.getReturnType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Johannes Englmeier
* @author Greg Taube
* @since 2.3
* @see org.springframework.core.KotlinDetector#isKotlinReflectPresent()
*/
Expand Down Expand Up @@ -109,8 +110,11 @@ public static boolean isDataClass(Class<?> type) {
*/
public static boolean isSuspend(Method method) {

KFunction<?> invokedFunction = KotlinDetector.isKotlinType(method.getDeclaringClass()) ? findKotlinFunction(method)
: null;
if (!KotlinDetector.isSuspendingFunction(method) || !KotlinDetector.isKotlinType(method.getDeclaringClass())) {
return false;
}

KFunction<?> invokedFunction = findKotlinFunction(method);

return invokedFunction != null && invokedFunction.isSuspend();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2026-present 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.data.util

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.data.repository.sample.User
import kotlin.coroutines.Continuation

/**
* Unit tests for [KotlinReflectionUtils.isSuspend].
*
* @author Greg Taube
*/
class KotlinReflectionUtilsSuspendUnitTests {

@Test // GH-3544
fun `detects suspend functions`() {

val method = SuspendingInterface::class.java.getDeclaredMethod("suspending", Continuation::class.java)

assertThat(KotlinReflectionUtils.isSuspend(method)).isTrue()
}

@Test // GH-3544
fun `considers Continuation parameter of a regular function as non-suspending`() {

val method = SuspendingInterface::class.java.getDeclaredMethod("continuationParameter", Continuation::class.java)

assertThat(KotlinReflectionUtils.isSuspend(method)).isFalse()
}

@Test // GH-3544
fun `considers regular functions as non-suspending`() {

assertThat(KotlinReflectionUtils.isSuspend(SuspendingInterface::class.java.getDeclaredMethod("regular"))).isFalse()
assertThat(KotlinReflectionUtils.isSuspend(SuspendingInterface::class.java.getDeclaredMethod("withParameter", User::class.java))).isFalse()
}

@Test // GH-3544
fun `considers Java methods as non-suspending`() {

assertThat(KotlinReflectionUtils.isSuspend(Object::class.java.getDeclaredMethod("toString"))).isFalse()
}

interface SuspendingInterface {

suspend fun suspending(): User

fun continuationParameter(continuation: Continuation<User>): User

fun regular(): User

fun withParameter(user: User): User
}
}