Skip to content

Commit 7646895

Browse files
committed
Support Kotlin synthetic classes in MethodParameter and SpEL
Closes gh-23812
1 parent f2b3953 commit 7646895

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

spring-core/src/main/java/org/springframework/core/MethodParameter.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -908,9 +908,14 @@ else if (ctor != null) {
908908
* functions via Kotlin reflection.
909909
*/
910910
static private Type getGenericReturnType(Method method) {
911-
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
912-
if (function != null && function.isSuspend()) {
913-
return ReflectJvmMapping.getJavaType(function.getReturnType());
911+
try {
912+
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
913+
if (function != null && function.isSuspend()) {
914+
return ReflectJvmMapping.getJavaType(function.getReturnType());
915+
}
916+
}
917+
catch (UnsupportedOperationException ex) {
918+
// probably a synthetic class - let's use java reflection instead
914919
}
915920
return method.getGenericReturnType();
916921
}
@@ -920,10 +925,15 @@ static private Type getGenericReturnType(Method method) {
920925
* functions via Kotlin reflection.
921926
*/
922927
static private Class<?> getReturnType(Method method) {
923-
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
924-
if (function != null && function.isSuspend()) {
925-
Type paramType = ReflectJvmMapping.getJavaType(function.getReturnType());
926-
return ResolvableType.forType(paramType).resolve(method.getReturnType());
928+
try {
929+
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
930+
if (function != null && function.isSuspend()) {
931+
Type paramType = ReflectJvmMapping.getJavaType(function.getReturnType());
932+
return ResolvableType.forType(paramType).resolve(method.getReturnType());
933+
}
934+
}
935+
catch (UnsupportedOperationException ex) {
936+
// probably a synthetic class - let's use java reflection instead
927937
}
928938
return method.getReturnType();
929939
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
description = "Spring Expression Language (SpEL)"
22

3+
apply plugin: "kotlin"
4+
35
dependencies {
46
compile(project(":spring-core"))
7+
testCompile("org.jetbrains.kotlin:kotlin-reflect")
8+
testCompile("org.jetbrains.kotlin:kotlin-stdlib")
59
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.springframework.expression.spel
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
import org.springframework.expression.ExpressionParser
6+
import org.springframework.expression.spel.standard.SpelExpressionParser
7+
8+
class KotlinSpelReproTests {
9+
10+
private val parser: ExpressionParser = SpelExpressionParser()
11+
12+
private val context = TestScenarioCreator.getTestEvaluationContext()
13+
14+
15+
@Test
16+
fun `gh-23812 SpEL cannot invoke Kotlin synthetic classes`() {
17+
val expr = parser.parseExpression("new org.springframework.expression.spel.KotlinSpelReproTests\$Config().kotlinSupplier().invoke()")
18+
assertThat(expr.getValue(context)).isEqualTo("test")
19+
}
20+
21+
class Config {
22+
23+
fun kotlinSupplier(): () -> String {
24+
return { "test" }
25+
}
26+
27+
}
28+
}

0 commit comments

Comments
 (0)