Recursively box/unbox nested inline value classes

See gh-34592
Signed-off-by: Dmitry Sulman <dmitry.sulman@gmail.com>
This commit is contained in:
Dmitry Sulman
2025-03-13 14:57:57 +02:00
committed by Sébastien Deleuze
parent c6a9aa59a3
commit 0c2ba4e38e
4 changed files with 91 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import kotlin.Unit;
import kotlin.jvm.JvmClassMappingKt;
@@ -322,11 +323,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
KType type = parameter.getType();
if (!(type.isMarkedNullable() && arg == null) && type.getClassifier() instanceof KClass<?> kClass
&& KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) {
KFunction<?> constructor = KClasses.getPrimaryConstructor(kClass);
if (!KCallablesJvm.isAccessible(constructor)) {
KCallablesJvm.setAccessible(constructor, true);
}
arg = constructor.call(arg);
arg = box(kClass, arg);
}
argMap.put(parameter, arg);
}
@@ -341,6 +338,19 @@ public class InvocableHandlerMethod extends HandlerMethod {
return (result == Unit.INSTANCE ? null : result);
}
private static Object box(KClass<?> kClass, @Nullable Object arg) {
KFunction<?> constructor = Objects.requireNonNull(KClasses.getPrimaryConstructor(kClass));
KType type = constructor.getParameters().get(0).getType();
if (!(type.isMarkedNullable() && arg == null) && type.getClassifier() instanceof KClass<?> parameterClass
&& KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(parameterClass))) {
arg = box(parameterClass, arg);
}
if (!KCallablesJvm.isAccessible(constructor)) {
KCallablesJvm.setAccessible(constructor, true);
}
return constructor.call(arg);
}
private static void handleResult(Object result, SynchronousSink<Object> sink) {
if (KotlinDetector.isInlineClass(result.getClass())) {
try {
@@ -361,7 +371,11 @@ public class InvocableHandlerMethod extends HandlerMethod {
}
private static Object unbox(Object result) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
return result.getClass().getDeclaredMethod("unbox-impl").invoke(result);
Object unboxed = result.getClass().getDeclaredMethod("unbox-impl").invoke(result);
if (KotlinDetector.isInlineClass(unboxed.getClass())) {
return unbox(unboxed);
}
return unboxed;
}
}