Skip to content

Commit e69b67b

Browse files
committed
fixup! Support Kotlin Context Parameters
Signed-off-by: SIMULATAN <[email protected]>
1 parent 99fa9dd commit e69b67b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

spring-core/spring-core.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ jar {
127127
}
128128
}
129129

130+
kotlin {
131+
compilerOptions {
132+
freeCompilerArgs.addAll("-Xcontext-parameters")
133+
}
134+
}
135+
130136
test {
131137
// Make sure the classes dir is used on the test classpath (required by ResourceTests).
132138
// When test fixtures are involved, the JAR is used by default.

spring-core/src/test/kotlin/org/springframework/core/CoroutinesUtilsTests.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ class CoroutinesUtilsTests {
275275
}
276276
}
277277

278+
@Test
279+
fun invokeSuspendingFunctionWithContextParameter() {
280+
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("suspendingFunctionWithContextParameter",
281+
CustomException::class.java, Continuation::class.java)
282+
val mono = CoroutinesUtils.invokeSuspendingFunction(method, this, CustomException("foo")) as Mono
283+
runBlocking {
284+
Assertions.assertThat(mono.awaitSingleOrNull()).isEqualTo("foo")
285+
}
286+
}
287+
288+
@Test
289+
fun invokeSuspendingFunctionWithContextParameterAndParameter() {
290+
val method = CoroutinesUtilsTests::class.java.getDeclaredMethod("suspendingFunctionWithContextParameterAndParameter",
291+
CustomException::class.java, Int::class.java, Continuation::class.java)
292+
val mono = CoroutinesUtils.invokeSuspendingFunction(method, this, CustomException("foo"), 20) as Mono
293+
runBlocking {
294+
Assertions.assertThat(mono.awaitSingleOrNull()).isEqualTo("foo-20")
295+
}
296+
}
297+
278298
@Test
279299
fun invokeSuspendingFunctionWithGenericParameter() {
280300
val method = GenericController::class.java.declaredMethods.first { it.name.startsWith("handle") }
@@ -377,6 +397,18 @@ class CoroutinesUtilsTests {
377397
return "${this.message}-$limit"
378398
}
379399

400+
context(value: CustomException)
401+
suspend fun suspendingFunctionWithContextParameter(): String {
402+
delay(1)
403+
return "${value.message}"
404+
}
405+
406+
context(value: CustomException)
407+
suspend fun suspendingFunctionWithContextParameterAndParameter(limit: Int): String {
408+
delay(1)
409+
return "${value.message}-$limit"
410+
}
411+
380412
interface Named {
381413
val name: String
382414
}

spring-web/spring-web.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,9 @@ dependencies {
102102
testRuntimeOnly("org.glassfish:jakarta.el")
103103
testRuntimeOnly("org.hibernate.validator:hibernate-validator")
104104
}
105+
106+
kotlin {
107+
compilerOptions {
108+
freeCompilerArgs.addAll("-Xcontext-parameters")
109+
}
110+
}

spring-web/src/test/kotlin/org/springframework/web/method/support/InvocableHandlerMethodKotlinTests.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ class InvocableHandlerMethodKotlinTests {
238238
Assertions.assertThat(value).isEqualTo("foo-20")
239239
}
240240

241+
@Test
242+
fun contextParameter() {
243+
composite.addResolver(StubArgumentResolver(CustomException::class.java, CustomException("foo")))
244+
val value = getInvocable(ReflectionUtils.findMethod(ContextParameterHandler::class.java, "handle", CustomException::class.java)!!).invokeForRequest(request, null)
245+
Assertions.assertThat(value).isEqualTo("foo")
246+
}
247+
248+
@Test
249+
fun contextParameterWithParameter() {
250+
composite.addResolver(StubArgumentResolver(CustomException::class.java, CustomException("foo")))
251+
composite.addResolver(StubArgumentResolver(Int::class.java, 20))
252+
val value = getInvocable(ReflectionUtils.findMethod(ContextParameterHandler::class.java, "handleWithParameter", CustomException::class.java, Int::class.java)!!)
253+
.invokeForRequest(request, null)
254+
Assertions.assertThat(value).isEqualTo("foo-20")
255+
}
256+
241257
@Test
242258
fun genericParameter() {
243259
val horse = Animal("horse")
@@ -359,6 +375,19 @@ class InvocableHandlerMethodKotlinTests {
359375
}
360376
}
361377

378+
private class ContextParameterHandler {
379+
380+
context(exception: CustomException)
381+
fun handle(): String {
382+
return "${exception.message}"
383+
}
384+
385+
context(exception: CustomException)
386+
fun handleWithParameter(limit: Int): String {
387+
return "${exception.message}-$limit"
388+
}
389+
}
390+
362391
private abstract class GenericHandler<T : Named> {
363392

364393
fun handle(named: T) = named.name

0 commit comments

Comments
 (0)