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.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import kotlin.jvm.JvmClassMappingKt;
|
||||||
|
import kotlin.reflect.KClass;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
|
@ -101,6 +103,7 @@ public class BindingReflectionHintsRegistrar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (KotlinDetector.isKotlinType(clazz)) {
|
if (KotlinDetector.isKotlinType(clazz)) {
|
||||||
|
KotlinDelegate.registerComponentHints(hints, clazz);
|
||||||
registerKotlinSerializationHints(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.Assertions.assertThat
|
||||||
import org.assertj.core.api.ThrowingConsumer
|
import org.assertj.core.api.ThrowingConsumer
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
|
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
|
||||||
|
|
@ -32,7 +33,7 @@ class KotlinBindingReflectionHintsRegistrarTests {
|
||||||
private val hints = RuntimeHints()
|
private val hints = RuntimeHints()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `Register type for Kotlinx serialization`() {
|
fun `Register reflection hints for Kotlinx serialization`() {
|
||||||
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleSerializableClass::class.java)
|
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleSerializableClass::class.java)
|
||||||
assertThat(hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
assertThat(hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||||
ThrowingConsumer { typeHint: TypeHint ->
|
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
|
@kotlinx.serialization.Serializable
|
||||||
class SampleSerializableClass(val name: String)
|
class SampleSerializableClass(val name: String)
|
||||||
|
|
||||||
|
data class SampleDataClass(val name: String)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue