Skip to content

Commit

Permalink
Treat kotlin.Unit as void
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Nov 22, 2023
1 parent 5a3ad6b commit 56b3da2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -27,6 +27,7 @@
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 5.0
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -93,4 +94,19 @@ public static boolean isSuspendingFunction(Method method) {
return false;
}

/**
* Return {@code true} if the class is {@code kotlin.Unit}.
* @since 6.1.1
*/
public static boolean isKotlinUnit(Class<?> clazz) {
return "kotlin.Unit".equals(clazz.getName());
}

/**
* Return {@code true} if the object is a {@code kotlin.Unit}.
* @since 6.1.1
*/
public static boolean isKotlinUnit(@Nullable Object object) {
return (object != null && isKotlinUnit(object.getClass()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 3.1
*/
public class InvocableHandlerMethod extends HandlerMethod {
Expand Down Expand Up @@ -315,7 +316,11 @@ public static Object invokeFunction(Method method, Object target, Object[] args)
}
}
}
return function.callBy(argMap);
Object returnValue = function.callBy(argMap);
if (KotlinDetector.isKotlinUnit(returnValue)) {
returnValue = null;
}
return returnValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse
* Kotlin unit tests for {@link InvocableHandlerMethod}.
*
* @author Sebastien Deleuze
* @author Yanming Zhou
*/
class InvocableHandlerMethodKotlinTests {

Expand Down Expand Up @@ -71,6 +72,11 @@ class InvocableHandlerMethodKotlinTests {
Assertions.assertThat(value).isEqualTo("true")
}

@Test
fun shouldTreatUnitAsVoid() {
Assertions.assertThat(getInvocable().invokeForRequest(request, null)).isNull()
}

private fun getInvocable(vararg argTypes: Class<*>): InvocableHandlerMethod {
val method = ResolvableMethod.on(Handler::class.java).argTypes(*argTypes).resolveMethod()
val handlerMethod = InvocableHandlerMethod(Handler(), method)
Expand All @@ -95,6 +101,8 @@ class InvocableHandlerMethodKotlinTests {

fun nullableBooleanDefaultValue(status: Boolean? = true) =
status.toString()

fun returnUnit() {}
}

}

0 comments on commit 56b3da2

Please sign in to comment.