Skip to content
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

Recursively boxing/unboxing nested inline value classes #34592

Closed
wants to merge 1 commit into from
Closed
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 @@ -318,11 +318,7 @@ private static class KotlinDelegate {
KType type = parameter.getType();
if (!(type.isMarkedNullable() && arg == null) && type.getClassifier() instanceof KClass<?> kClass
&& KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) {
KFunction<?> constructor = Objects.requireNonNull(KClasses.getPrimaryConstructor(kClass));
if (!KCallablesJvm.isAccessible(constructor)) {
KCallablesJvm.setAccessible(constructor, true);
}
arg = constructor.call(arg);
arg = box(kClass, arg);
}
argMap.put(parameter, arg);
}
Expand All @@ -337,6 +333,19 @@ private static class KotlinDelegate {
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 {
Expand All @@ -357,7 +366,11 @@ private static void handleResult(Object result, SynchronousSink<Object> sink) {
}

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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -109,12 +109,25 @@ class InvocableHandlerMethodKotlinTests {
Assertions.assertThat(value).isEqualTo(1L)
}

@Test
fun nestedValueClass() {
composite.addResolver(StubArgumentResolver(Long::class.java, 1L))
val value = getInvocable(ValueClassHandler::nestedValueClass.javaMethod!!).invokeForRequest(request, null)
Assertions.assertThat(value).isEqualTo(1L)
}

@Test
fun valueClassReturnValue() {
val value = getInvocable(ValueClassHandler::valueClassReturnValue.javaMethod!!).invokeForRequest(request, null)
Assertions.assertThat(value).isEqualTo("foo")
}

@Test
fun nestedValueClassReturnValue() {
val value = getInvocable(ValueClassHandler::nestedValueClassReturnValue.javaMethod!!).invokeForRequest(request, null)
Assertions.assertThat(value).isEqualTo("foo")
}

@Test
fun resultOfUnitReturnValue() {
val value = getInvocable(ValueClassHandler::resultOfUnitReturnValue.javaMethod!!).invokeForRequest(request, null)
Expand Down Expand Up @@ -273,10 +286,14 @@ class InvocableHandlerMethodKotlinTests {

fun valueClassReturnValue() = StringValueClass("foo")

fun nestedValueClassReturnValue() = NestedStringValueClass(StringValueClass("foo"))

fun resultOfUnitReturnValue() = Result.success(Unit)

fun longValueClass(limit: LongValueClass) = limit.value

fun nestedValueClass(limit: ULongValueClass) = limit.value

fun doubleValueClass(limit: DoubleValueClass = DoubleValueClass(3.1)) = limit.value

fun valueClassWithInit(valueClass: ValueClassWithInit) = valueClass
Expand Down Expand Up @@ -358,9 +375,15 @@ class InvocableHandlerMethodKotlinTests {
@JvmInline
value class StringValueClass(val value: String)

@JvmInline
value class NestedStringValueClass(val value: StringValueClass)

@JvmInline
value class LongValueClass(val value: Long)

@JvmInline
value class ULongValueClass(val value: ULong)

@JvmInline
value class DoubleValueClass(val value: Double)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,7 @@ private static class KotlinDelegate {
KType type = parameter.getType();
if (!(type.isMarkedNullable() && arg == null) && type.getClassifier() instanceof KClass<?> kClass
&& KotlinDetector.isInlineClass(JvmClassMappingKt.getJavaClass(kClass))) {
KFunction<?> constructor = Objects.requireNonNull(KClasses.getPrimaryConstructor(kClass));
if (!KCallablesJvm.isAccessible(constructor)) {
KCallablesJvm.setAccessible(constructor, true);
}
arg = constructor.call(arg);
arg = box(kClass, arg);
}
argMap.put(parameter, arg);
}
Expand All @@ -376,6 +372,19 @@ private static class KotlinDelegate {
}
}

private static Object box(KClass<?> kClass, 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 {
Expand All @@ -396,7 +405,11 @@ private static void handleResult(Object result, SynchronousSink<Object> sink) {
}

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;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -205,13 +205,28 @@ class InvocableHandlerMethodKotlinTests {
assertHandlerResultValue(result, "1")
}

@Test
fun nestedValueClass() {
this.resolvers.add(stubResolver(1L, Long::class.java))
val method = ValueClassController::nestedValueClass.javaMethod!!
val result = invoke(ValueClassController(), method,1L)
assertHandlerResultValue(result, "1")
}

@Test
fun valueClassReturnValue() {
val method = ValueClassController::valueClassReturnValue.javaMethod!!
val result = invoke(ValueClassController(), method)
assertHandlerResultValue(result, "foo")
}

@Test
fun nestedValueClassReturnValue() {
val method = ValueClassController::nestedValueClassReturnValue.javaMethod!!
val result = invoke(ValueClassController(), method)
assertHandlerResultValue(result, "foo")
}

@Test
fun resultOfUnitReturnValue() {
val method = ValueClassController::resultOfUnitReturnValue.javaMethod!!
Expand Down Expand Up @@ -448,8 +463,12 @@ class InvocableHandlerMethodKotlinTests {

fun valueClass(limit: LongValueClass) = "${limit.value}"

fun nestedValueClass(limit: ULongValueClass) = "${limit.value}"

fun valueClassReturnValue() = StringValueClass("foo")

fun nestedValueClassReturnValue() = NestedStringValueClass(StringValueClass("foo"))

fun resultOfUnitReturnValue() = Result.success(Unit)

fun valueClassWithDefault(limit: DoubleValueClass = DoubleValueClass(3.1)) = "${limit.value}"
Expand Down Expand Up @@ -533,9 +552,15 @@ class InvocableHandlerMethodKotlinTests {
@JvmInline
value class StringValueClass(val value: String)

@JvmInline
value class NestedStringValueClass(val value: StringValueClass)

@JvmInline
value class LongValueClass(val value: Long)

@JvmInline
value class ULongValueClass(val value: ULong)

@JvmInline
value class DoubleValueClass(val value: Double)

Expand Down
Loading