Box Kotlin value class parameters in web endpoint
In order to avoid "java.lang.IllegalArgumentException: object is not an instance of declaring class" errors. Closes gh-31698
This commit is contained in:
@@ -18,11 +18,14 @@ package org.springframework.web.method.support;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.JvmClassMappingKt;
|
||||
import kotlin.reflect.KClass;
|
||||
import kotlin.reflect.KFunction;
|
||||
import kotlin.reflect.KParameter;
|
||||
import kotlin.reflect.jvm.KCallablesJvm;
|
||||
@@ -35,8 +38,10 @@ import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.validation.method.MethodValidator;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
@@ -60,6 +65,9 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
||||
|
||||
private static final Class<?>[] EMPTY_GROUPS = new Class<?>[0];
|
||||
|
||||
private static final ReflectionUtils.MethodFilter boxImplFilter =
|
||||
(method -> method.isSynthetic() && Modifier.isStatic(method.getModifiers()) && method.getName().equals("box-impl"));
|
||||
|
||||
|
||||
private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
|
||||
|
||||
@@ -310,7 +318,15 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
||||
case INSTANCE -> argMap.put(parameter, target);
|
||||
case VALUE -> {
|
||||
if (!parameter.isOptional() || args[index] != null) {
|
||||
argMap.put(parameter, args[index]);
|
||||
if (parameter.getType().getClassifier() instanceof KClass<?> kClass && kClass.isValue()) {
|
||||
Class<?> javaClass = JvmClassMappingKt.getJavaClass(kClass);
|
||||
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(javaClass, boxImplFilter);
|
||||
Assert.state(methods.length == 1, "Unable to find a single box-impl synthetic static method in " + javaClass.getName());
|
||||
argMap.put(parameter, ReflectionUtils.invokeMethod(methods[0], null, args[index]));
|
||||
}
|
||||
else {
|
||||
argMap.put(parameter, args[index]);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -84,6 +84,20 @@ class InvocableHandlerMethodKotlinTests {
|
||||
Assertions.assertThat(value).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun valueClass() {
|
||||
composite.addResolver(StubArgumentResolver(Long::class.java, 1L))
|
||||
val value = getInvocable(Long::class.java).invokeForRequest(request, null)
|
||||
Assertions.assertThat(value).isEqualTo(1L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun valueClassDefaultValue() {
|
||||
composite.addResolver(StubArgumentResolver(Double::class.java))
|
||||
val value = getInvocable(Double::class.java).invokeForRequest(request, null)
|
||||
Assertions.assertThat(value).isEqualTo(3.1)
|
||||
}
|
||||
|
||||
private fun getInvocable(vararg argTypes: Class<*>): InvocableHandlerMethod {
|
||||
val method = ResolvableMethod.on(Handler::class.java).argTypes(*argTypes).resolveMethod()
|
||||
val handlerMethod = InvocableHandlerMethod(Handler(), method)
|
||||
@@ -116,6 +130,18 @@ class InvocableHandlerMethodKotlinTests {
|
||||
fun nullable(arg: String?): String? {
|
||||
return null
|
||||
}
|
||||
|
||||
fun valueClass(limit: LongValueClass) =
|
||||
limit.value
|
||||
|
||||
fun valueClass(limit: DoubleValueClass = DoubleValueClass(3.1)) =
|
||||
limit.value
|
||||
}
|
||||
|
||||
@JvmInline
|
||||
value class LongValueClass(val value: Long)
|
||||
|
||||
@JvmInline
|
||||
value class DoubleValueClass(val value: Double)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user