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

Convert args to Kotlin value class in CoroutinesUtils #31846

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Objects;

Expand All @@ -40,6 +41,7 @@
import kotlinx.coroutines.reactor.MonoKt;
import kotlinx.coroutines.reactor.ReactorFlowKt;
import org.reactivestreams.Publisher;
import org.springframework.util.ReflectionUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -55,6 +57,9 @@
*/
public abstract class CoroutinesUtils {

private static final ReflectionUtils.MethodFilter boxImplFilter =
(method -> method.isSynthetic() && Modifier.isStatic(method.getModifiers()) && method.getName().equals("box-impl"));

/**
* Convert a {@link Deferred} instance to a {@link Mono}.
*/
Expand Down Expand Up @@ -115,7 +120,14 @@ public static Publisher<?> invokeSuspendingFunction(CoroutineContext context, Me
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++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ class CoroutinesUtilsTests {
}
}

@Test
fun invokeSuspendingFunctionWithValueClassParameter() {
val method = CoroutinesUtilsTests::class.java.declaredMethods.first { it.name.startsWith("suspendingFunctionWithValueClass") }
val mono = CoroutinesUtils.invokeSuspendingFunction(method, this, "foo", null) as Mono
runBlocking {
Assertions.assertThat(mono.awaitSingle()).isEqualTo("foo")
}
}

suspend fun suspendingFunction(value: String): String {
delay(1)
return value
Expand Down Expand Up @@ -172,4 +181,12 @@ class CoroutinesUtilsTests {
return null
}

suspend fun suspendingFunctionWithValueClass(value: ValueClass): String {
delay(1)
return value.value
}

@JvmInline
value class ValueClass(val value: String)

}