Skip to content

Commit 3704418

Browse files
committed
Avoid dependency on Spring Data for Kotlin apps
Prior to this commit, Kotlin applications would require "spring-data-commons" on the classpath to get the invocation method support. This commit removes the dependency on Spring Data's `KotlinReflectionUtils` and use a local implementation instead. Fixes gh-1397
1 parent fce1dbf commit 3704418

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/InvocableHandlerMethodSupport.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727

2828
import graphql.GraphQLContext;
2929
import io.micrometer.context.ContextSnapshot;
30+
import kotlin.jvm.JvmClassMappingKt;
31+
import kotlin.reflect.KFunction;
32+
import kotlin.reflect.jvm.KTypesJvm;
33+
import kotlin.reflect.jvm.ReflectJvmMapping;
3034
import org.jspecify.annotations.Nullable;
3135
import reactor.core.publisher.Mono;
3236

3337
import org.springframework.core.CoroutinesUtils;
3438
import org.springframework.core.KotlinDetector;
35-
import org.springframework.data.util.KotlinReflectionUtils;
3639
import org.springframework.graphql.execution.ContextPropagationHelper;
3740
import org.springframework.util.Assert;
3841

@@ -47,6 +50,8 @@ public abstract class InvocableHandlerMethodSupport extends HandlerMethod {
4750

4851
private static final Object NO_VALUE = new Object();
4952

53+
private static final boolean KOTLIN_REFLECT_PRESENT = KotlinDetector.isKotlinReflectPresent();
54+
5055

5156
private final @Nullable Executor executor;
5257

@@ -135,6 +140,7 @@ protected InvocableHandlerMethodSupport(
135140
private static Object invokeSuspendingFunction(Object bean, Method method, @Nullable Object[] argValues) {
136141
Object result = CoroutinesUtils.invokeSuspendingFunction(method, bean, argValues);
137142

143+
Assert.state(KOTLIN_REFLECT_PRESENT, "Missing org.jetbrains.kotlin:kotlin-reflect dependency");
138144
// Support use of DataLoader from suspending function
139145
Class<?> returnType = KotlinReflectionUtils.getReturnType(method);
140146
if (CompletableFuture.class.isAssignableFrom(returnType)) {
@@ -209,4 +215,15 @@ private Throwable processInvocationTargetException(@Nullable Object[] argValues,
209215
});
210216
}
211217

218+
static class KotlinReflectionUtils {
219+
220+
static Class<?> getReturnType(Method method) {
221+
KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(method);
222+
if (kotlinFunction == null) {
223+
throw new IllegalArgumentException(String.format("Cannot resolve %s to a KFunction", method));
224+
}
225+
return JvmClassMappingKt.getJavaClass(KTypesJvm.getJvmErasure(kotlinFunction.getReturnType()));
226+
}
227+
}
228+
212229
}

0 commit comments

Comments
 (0)