Skip to content

Commit 9064931

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 63cc65f commit 9064931

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,11 +27,14 @@
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 reactor.core.publisher.Mono;
3135

3236
import org.springframework.core.CoroutinesUtils;
3337
import org.springframework.core.KotlinDetector;
34-
import org.springframework.data.util.KotlinReflectionUtils;
3538
import org.springframework.graphql.execution.ContextPropagationHelper;
3639
import org.springframework.lang.Nullable;
3740
import org.springframework.util.Assert;
@@ -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
@Nullable
5257
private final Executor executor;
@@ -137,6 +142,7 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
137142
private static Object invokeSuspendingFunction(Object bean, Method method, Object[] argValues) {
138143
Object result = CoroutinesUtils.invokeSuspendingFunction(method, bean, argValues);
139144

145+
Assert.state(KOTLIN_REFLECT_PRESENT, "Missing org.jetbrains.kotlin:kotlin-reflect dependency");
140146
// Support use of DataLoader from suspending function
141147
Class<?> returnType = KotlinReflectionUtils.getReturnType(method);
142148
if (CompletableFuture.class.isAssignableFrom(returnType)) {
@@ -210,4 +216,15 @@ protected Mono<Object[]> toArgsMono(Object[] args) {
210216
});
211217
}
212218

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

0 commit comments

Comments
 (0)