Skip to content

GH-3344: Treat kotlin.Unit return as null in MMIH #3346

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

Merged
merged 3 commits into from
Jul 20, 2020
Merged
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 @@ -576,6 +576,7 @@ private void configureLocalMessageHandlerFactory() {
localHandlerMethodFactory.afterPropertiesSet();
}

@Nullable
private Object invokeHandlerMethod(HandlerMethod handlerMethod, ParametersWrapper parameters) {
try {
return handlerMethod.invoke(parameters);
Expand Down Expand Up @@ -1093,13 +1094,20 @@ void setInvocableHandlerMethod(InvocableHandlerMethod newInvocableHandlerMethod)
this.invocableHandlerMethod = newInvocableHandlerMethod;
}

@Nullable
public Object invoke(ParametersWrapper parameters) {
Message<?> message = parameters.getMessage();
if (this.canProcessMessageList) {
message = new MutableMessage<>(parameters.getMessages(), parameters.getHeaders());
}
try {
return this.invocableHandlerMethod.invoke(message);
Object result = this.invocableHandlerMethod.invoke(message);
if (result != null
&& org.springframework.integration.util.ClassUtils.isKotlinUnit(result.getClass())) {

result = null;
}
return result;
}
catch (RuntimeException ex) { // NOSONAR no way to handle conditional catch according Sonar rules
throw ex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public abstract class ClassUtils {
*/
public static final Class<?> KOTLIN_FUNCTION_1_CLASS;

/**
* The {@code kotlin.Unit} class object.
*/
public static final Class<?> KOTLIN_UNIT_CLASS;

static {
PRIMITIVE_WRAPPER_TYPE_MAP.put(Boolean.class, boolean.class);
PRIMITIVE_WRAPPER_TYPE_MAP.put(Byte.class, byte.class);
Expand Down Expand Up @@ -161,6 +166,18 @@ public abstract class ClassUtils {
finally {
KOTLIN_FUNCTION_1_CLASS = kotlinClass;
}

kotlinClass = null;
try {
kotlinClass = org.springframework.util.ClassUtils.forName("kotlin.Unit",
org.springframework.util.ClassUtils.getDefaultClassLoader());
}
catch (ClassNotFoundException e) {
//Ignore: assume no Kotlin in classpath
}
finally {
KOTLIN_UNIT_CLASS = kotlinClass;
}
}

public static Class<?> findClosestMatch(Class<?> type, Set<Class<?>> candidates, boolean failOnTie) {
Expand Down Expand Up @@ -247,4 +264,14 @@ public static boolean isKotlinFaction1(Class<?> aClass) {
return KOTLIN_FUNCTION_1_CLASS != null && KOTLIN_FUNCTION_1_CLASS.isAssignableFrom(aClass);
}

/**
* Check if class is {@code kotlin.Unit}.
* @param aClass the {@link Class} to check.
* @return true if class is a {@code kotlin.Unit} implementation.
* @since 5.3.2
*/
public static boolean isKotlinUnit(Class<?> aClass) {
return KOTLIN_UNIT_CLASS != null && KOTLIN_UNIT_CLASS.isAssignableFrom(aClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
package org.springframework.integration.dsl

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isGreaterThanOrEqualTo
import assertk.assertions.isInstanceOf
import assertk.assertions.isNotNull
import assertk.assertions.isTrue
import assertk.assertions.size
import assertk.assertions.*
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -51,6 +48,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
import reactor.core.publisher.Flux
import reactor.test.StepVerifier
import java.util.*
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Function

/**
Expand Down Expand Up @@ -210,6 +208,23 @@ class KotlinDslTests {
assertThat(payload).isInstanceOf(List::class.java).size().isGreaterThanOrEqualTo(1)
}

@Test
fun `no reply from handle`() {
val payloadReference = AtomicReference<String>()
val integrationFlow =
integrationFlow("handlerInputChanenl") {
handle<String> { payload, _ -> payloadReference.set(payload) }
}

val registration = this.integrationFlowContext.registration(integrationFlow).register()

registration.inputChannel.send(GenericMessage("test"))

assertThat(payloadReference.get()).isEqualTo("test")

registration.destroy()
}

@Configuration
@EnableIntegration
class Config {
Expand Down