Add Kotlin data class components support to BindingReflectionHintsRegistrar
Closes gh-29316
This commit is contained in:
parent
581fa1419f
commit
69cb68f7d6
|
|
@ -22,6 +22,8 @@ import java.lang.reflect.Type;
|
|||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import kotlin.jvm.JvmClassMappingKt;
|
||||
import kotlin.reflect.KClass;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
|
@ -101,6 +103,7 @@ public class BindingReflectionHintsRegistrar {
|
|||
}
|
||||
}
|
||||
if (KotlinDetector.isKotlinType(clazz)) {
|
||||
KotlinDelegate.registerComponentHints(hints, clazz);
|
||||
registerKotlinSerializationHints(hints, clazz);
|
||||
}
|
||||
});
|
||||
|
|
@ -148,4 +151,22 @@ public class BindingReflectionHintsRegistrar {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inner class to avoid a hard dependency on Kotlin at runtime.
|
||||
*/
|
||||
private static class KotlinDelegate {
|
||||
|
||||
public static void registerComponentHints(ReflectionHints hints, Class<?> type) {
|
||||
KClass<?> kClass = JvmClassMappingKt.getKotlinClass(type);
|
||||
if (kClass.isData()) {
|
||||
for (Method method : type.getMethods()) {
|
||||
String methodName = method.getName();
|
||||
if (methodName.startsWith("component") || methodName.equals("copy")) {
|
||||
hints.registerMethod(method, ExecutableMode.INVOKE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.aot.hint
|
|||
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
|
||||
|
||||
/**
|
||||
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
|
||||
|
|
@ -32,7 +33,7 @@ class KotlinBindingReflectionHintsRegistrarTests {
|
|||
private val hints = RuntimeHints()
|
||||
|
||||
@Test
|
||||
fun `Register type for Kotlinx serialization`() {
|
||||
fun `Register reflection hints for Kotlinx serialization`() {
|
||||
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleSerializableClass::class.java)
|
||||
assertThat(hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
ThrowingConsumer { typeHint: TypeHint ->
|
||||
|
|
@ -59,7 +60,17 @@ class KotlinBindingReflectionHintsRegistrarTests {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Register reflection hints for Kotlin data class`() {
|
||||
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleDataClass::class.java)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
class SampleSerializableClass(val name: String)
|
||||
|
||||
data class SampleDataClass(val name: String)
|
||||
|
|
|
|||
Loading…
Reference in New Issue