fixup! Support Kotlin Context Parameters

Signed-off-by: SIMULATAN <jakob@simulatan.me>
This commit is contained in:
SIMULATAN 2025-09-17 17:32:03 +02:00
parent 99fa9dd5e5
commit e69b67bffd
No known key found for this signature in database
4 changed files with 73 additions and 0 deletions

View File

@ -127,6 +127,12 @@ jar {
}
}
kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xcontext-parameters")
}
}
test {
// Make sure the classes dir is used on the test classpath (required by ResourceTests).
// When test fixtures are involved, the JAR is used by default.

View File

@ -275,6 +275,26 @@ class CoroutinesUtilsTests {
}
}
@Test
fun invokeSuspendingFunctionWithContextParameter() {
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("suspendingFunctionWithContextParameter",
CustomException::class.java, Continuation::class.java)
val mono = CoroutinesUtils.invokeSuspendingFunction(method, this, CustomException("foo")) as Mono
runBlocking {
Assertions.assertThat(mono.awaitSingleOrNull()).isEqualTo("foo")
}
}
@Test
fun invokeSuspendingFunctionWithContextParameterAndParameter() {
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("suspendingFunctionWithContextParameterAndParameter",
CustomException::class.java, Int::class.java, Continuation::class.java)
val mono = CoroutinesUtils.invokeSuspendingFunction(method, this, CustomException("foo"), 20) as Mono
runBlocking {
Assertions.assertThat(mono.awaitSingleOrNull()).isEqualTo("foo-20")
}
}
@Test
fun invokeSuspendingFunctionWithGenericParameter() {
val method = GenericController::class.java.declaredMethods.first { it.name.startsWith("handle") }
@ -377,6 +397,18 @@ class CoroutinesUtilsTests {
return "${this.message}-$limit"
}
context(value: CustomException)
suspend fun suspendingFunctionWithContextParameter(): String {
delay(1)
return "${value.message}"
}
context(value: CustomException)
suspend fun suspendingFunctionWithContextParameterAndParameter(limit: Int): String {
delay(1)
return "${value.message}-$limit"
}
interface Named {
val name: String
}

View File

@ -102,3 +102,9 @@ dependencies {
testRuntimeOnly("org.glassfish:jakarta.el")
testRuntimeOnly("org.hibernate.validator:hibernate-validator")
}
kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xcontext-parameters")
}
}

View File

@ -238,6 +238,22 @@ class InvocableHandlerMethodKotlinTests {
Assertions.assertThat(value).isEqualTo("foo-20")
}
@Test
fun contextParameter() {
composite.addResolver(StubArgumentResolver(CustomException::class.java, CustomException("foo")))
val value = getInvocable(ReflectionUtils.findMethod(ContextParameterHandler::class.java, "handle", CustomException::class.java)!!).invokeForRequest(request, null)
Assertions.assertThat(value).isEqualTo("foo")
}
@Test
fun contextParameterWithParameter() {
composite.addResolver(StubArgumentResolver(CustomException::class.java, CustomException("foo")))
composite.addResolver(StubArgumentResolver(Int::class.java, 20))
val value = getInvocable(ReflectionUtils.findMethod(ContextParameterHandler::class.java, "handleWithParameter", CustomException::class.java, Int::class.java)!!)
.invokeForRequest(request, null)
Assertions.assertThat(value).isEqualTo("foo-20")
}
@Test
fun genericParameter() {
val horse = Animal("horse")
@ -359,6 +375,19 @@ class InvocableHandlerMethodKotlinTests {
}
}
private class ContextParameterHandler {
context(exception: CustomException)
fun handle(): String {
return "${exception.message}"
}
context(exception: CustomException)
fun handleWithParameter(limit: Int): String {
return "${exception.message}-$limit"
}
}
private abstract class GenericHandler<T : Named> {
fun handle(named: T) = named.name