Refine BindingReflectionHintsRegistrar Kotlin support

Closes gh-29593
This commit is contained in:
Sébastien Deleuze
2022-12-11 11:34:30 +01:00
parent dc5a773b2b
commit b27735d4a6
2 changed files with 16 additions and 1 deletions

View File

@@ -111,6 +111,8 @@ public class BindingReflectionHintsRegistrar {
if (KotlinDetector.isKotlinType(clazz)) {
KotlinDelegate.registerComponentHints(hints, clazz);
registerKotlinSerializationHints(hints, clazz);
// For Kotlin reflection
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS);
}
});
}
@@ -192,7 +194,7 @@ public class BindingReflectionHintsRegistrar {
if (kClass.isData()) {
for (Method method : type.getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("component") || methodName.equals("copy")) {
if (methodName.startsWith("component") || methodName.equals("copy") || methodName.equals("copy$default")) {
hints.registerMethod(method, ExecutableMode.INVOKE);
}
}

View File

@@ -20,6 +20,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.ThrowingConsumer
import org.junit.jupiter.api.Test
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates
import java.lang.reflect.Method
/**
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
@@ -67,6 +68,16 @@ class KotlinBindingReflectionHintsRegistrarTests {
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "component1")).accepts(hints)
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "copy")).accepts(hints)
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "getName")).accepts(hints)
val copyDefault: Method = SampleDataClass::class.java.getMethod("copy\$default", SampleDataClass::class.java,
String::class.java , Int::class.java, Object::class.java)
assertThat(RuntimeHintsPredicates.reflection().onMethod(copyDefault)).accepts(hints)
}
@Test
fun `Register reflection hints on declared methods for Kotlin class`() {
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleClass::class.java)
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClass::class.java)
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)).accepts(hints)
}
}
@@ -74,3 +85,5 @@ class KotlinBindingReflectionHintsRegistrarTests {
class SampleSerializableClass(val name: String)
data class SampleDataClass(val name: String)
class SampleClass(val name: String)