Skip to content

Commit 4b75d43

Browse files
committed
* Improve reflection in the CoroutinesUtils
1 parent dd532e2 commit 4b75d43

File tree

4 files changed

+12
-33
lines changed

4 files changed

+12
-33
lines changed

spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ private Object invokeGatewayMethod(MethodInvocation invocation, boolean runningO
582582
Object continuation = null;
583583
if (gateway.isSuspendingFunction) {
584584
for (Object argument : invocation.getArguments()) {
585-
if (CoroutinesUtils.KOTLIN_CONTINUATION_CLASS.isAssignableFrom(argument.getClass())) {
585+
if (argument != null && CoroutinesUtils.isContinuation(argument)) {
586586
continuation = argument;
587587
break;
588588
}
@@ -1029,8 +1029,8 @@ protected void doStop() {
10291029
this.gatewayMap.values().forEach(MethodInvocationGateway::stop);
10301030
}
10311031

1032-
@SuppressWarnings("unchecked")
10331032
@Nullable
1033+
@SuppressWarnings("unchecked")
10341034
private <T> T convert(Object source, Class<T> expectedReturnType, @Nullable Object continuation) {
10351035
if (continuation != null) {
10361036
return CoroutinesUtils.monoAwaitSingleOrNull((Mono<T>) source, continuation);

spring-integration-core/src/main/java/org/springframework/integration/handler/support/IntegrationMessageHandlerMethodFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.BeanFactoryAware;
2525
import org.springframework.beans.factory.BeanInitializationException;
2626
import org.springframework.beans.factory.InitializingBean;
27+
import org.springframework.core.KotlinDetector;
2728
import org.springframework.integration.support.NullAwarePayloadArgumentResolver;
2829
import org.springframework.messaging.converter.MessageConverter;
2930
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
@@ -98,7 +99,9 @@ private List<HandlerMethodArgumentResolver> buildArgumentResolvers(boolean listC
9899
resolvers.add(new CollectionArgumentResolver(true));
99100
}
100101
resolvers.add(new MapArgumentResolver());
101-
resolvers.add(new ContinuationHandlerMethodArgumentResolver());
102+
if (KotlinDetector.isKotlinPresent()) {
103+
resolvers.add(new ContinuationHandlerMethodArgumentResolver());
104+
}
102105

103106
for (HandlerMethodArgumentResolver resolver : resolvers) {
104107
if (resolver instanceof BeanFactoryAware) {

spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -488,7 +488,7 @@ private Object processInternal(ParametersWrapper parameters) {
488488

489489
private synchronized void initialize() {
490490
if (isProvidedMessageHandlerFactoryBean()) {
491-
LOGGER.info("Overriding default instance of MessageHandlerMethodFactory with provided one.");
491+
LOGGER.trace("Overriding default instance of MessageHandlerMethodFactory with provided one.");
492492
this.messageHandlerMethodFactory =
493493
getBeanFactory()
494494
.getBean(

spring-integration-core/src/main/java/org/springframework/integration/util/CoroutinesUtils.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.core.KotlinDetector;
2020
import org.springframework.lang.Nullable;
2121
import org.springframework.util.Assert;
22-
import org.springframework.util.ClassUtils;
2322

2423
import reactor.core.publisher.Mono;
2524

@@ -34,43 +33,20 @@
3433
*/
3534
public final class CoroutinesUtils {
3635

37-
/**
38-
* The {@link kotlin.coroutines.Continuation} class object.
39-
*/
40-
@Nullable
41-
public static final Class<?> KOTLIN_CONTINUATION_CLASS;
42-
43-
static {
44-
if (KotlinDetector.isKotlinPresent()) {
45-
Class<?> kotlinClass = null;
46-
try {
47-
kotlinClass = ClassUtils.forName("kotlin.coroutines.Continuation", ClassUtils.getDefaultClassLoader());
48-
}
49-
catch (ClassNotFoundException ex) {
50-
//Ignore: assume no Kotlin in classpath
51-
}
52-
finally {
53-
KOTLIN_CONTINUATION_CLASS = kotlinClass;
54-
}
55-
}
56-
else {
57-
KOTLIN_CONTINUATION_CLASS = null;
58-
}
59-
}
60-
6136
public static boolean isContinuation(Object candidate) {
6237
return isContinuationType(candidate.getClass());
6338
}
6439

6540
public static boolean isContinuationType(Class<?> candidate) {
66-
return KOTLIN_CONTINUATION_CLASS != null && KOTLIN_CONTINUATION_CLASS.isAssignableFrom(candidate);
41+
return KotlinDetector.isKotlinPresent() && kotlin.coroutines.Continuation.class.isAssignableFrom(candidate);
6742
}
6843

6944
@Nullable
7045
@SuppressWarnings("unchecked")
7146
public static <T> T monoAwaitSingleOrNull(Mono<? extends T> source, Object continuation) {
72-
Assert.notNull(KOTLIN_CONTINUATION_CLASS, "Kotlin Coroutines library is not present in classpath");
73-
Assert.isAssignable(KOTLIN_CONTINUATION_CLASS, continuation.getClass());
47+
Assert.state(isContinuation(continuation), () ->
48+
"The 'continuation' must be an instance of 'kotlin.coroutines.Continuation', but it is: "
49+
+ continuation.getClass());
7450
return (T) kotlinx.coroutines.reactor.MonoKt.awaitSingleOrNull(
7551
source, (kotlin.coroutines.Continuation<T>) continuation);
7652
}

0 commit comments

Comments
 (0)