Move BindingReflectionHintsRegistrar to spring-core
See gh-28979
This commit is contained in:
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.aot;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.RecordComponent;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.ReflectionHints;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Register the necessary reflection hints so that the specified type can be
|
||||
* bound at runtime. Fields, constructors and property methods are registered,
|
||||
* except for a set of types like those in the {@code java.} package where just
|
||||
* the type is registered. Types are discovered transitively and generic types
|
||||
* are registered as well.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 6.0
|
||||
*/
|
||||
public class BindingReflectionHintsRegistrar {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(BindingReflectionHintsRegistrar.class);
|
||||
|
||||
private static final String KOTLIN_COMPANION_SUFFIX = "$Companion";
|
||||
|
||||
/**
|
||||
* Register the necessary reflection hints to bind the specified types.
|
||||
* @param hints the hints instance to use
|
||||
* @param types the types to bind
|
||||
*/
|
||||
public void registerReflectionHints(ReflectionHints hints, Type... types) {
|
||||
Set<Type> seen = new LinkedHashSet<>();
|
||||
for (Type type : types) {
|
||||
registerReflectionHints(hints, seen, type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether the members of the type should be registered transitively.
|
||||
* @param type the type to evaluate
|
||||
* @return {@code true} if the members of the type should be registered transitively
|
||||
*/
|
||||
protected boolean shouldRegisterMembers(Class<?> type) {
|
||||
return !type.getCanonicalName().startsWith("java.") && !type.isArray();
|
||||
}
|
||||
|
||||
private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type type) {
|
||||
if (type instanceof Class<?> clazz) {
|
||||
if (clazz.isPrimitive() || clazz == Object.class) {
|
||||
return;
|
||||
}
|
||||
hints.registerType(clazz, typeHint -> {
|
||||
if (seen.contains(type)) {
|
||||
return;
|
||||
}
|
||||
seen.add(type);
|
||||
if (shouldRegisterMembers(clazz)) {
|
||||
if (clazz.isRecord()) {
|
||||
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
for (RecordComponent recordComponent : clazz.getRecordComponents()) {
|
||||
registerRecordHints(hints, seen, recordComponent.getAccessor());
|
||||
}
|
||||
}
|
||||
else {
|
||||
typeHint.withMembers(
|
||||
MemberCategory.DECLARED_FIELDS,
|
||||
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
try {
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
|
||||
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
||||
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
|
||||
registerPropertyHints(hints, seen, propertyDescriptor.getWriteMethod(), 0);
|
||||
registerPropertyHints(hints, seen, propertyDescriptor.getReadMethod(), -1);
|
||||
}
|
||||
}
|
||||
catch (IntrospectionException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ignoring referenced type [" + clazz.getName() + "]: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (KotlinDetector.isKotlinType(clazz)) {
|
||||
registerKotlinSerializationHints(hints, clazz);
|
||||
}
|
||||
});
|
||||
}
|
||||
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
|
||||
collectReferencedTypes(seen, referencedTypes, type);
|
||||
referencedTypes.forEach(referencedType -> registerReflectionHints(hints, seen, referencedType));
|
||||
}
|
||||
|
||||
private void registerRecordHints(ReflectionHints hints, Set<Type> seen, Method method) {
|
||||
hints.registerMethod(method, ExecutableMode.INVOKE);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, -1);
|
||||
Type methodParameterType = methodParameter.getGenericParameterType();
|
||||
if (!seen.contains(methodParameterType)) {
|
||||
registerReflectionHints(hints, seen, methodParameterType);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerPropertyHints(ReflectionHints hints, Set<Type> seen, @Nullable Method method, int parameterIndex) {
|
||||
if (method != null && method.getDeclaringClass() != Object.class
|
||||
&& method.getDeclaringClass() != Enum.class) {
|
||||
hints.registerMethod(method, ExecutableMode.INVOKE);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, parameterIndex);
|
||||
Type methodParameterType = methodParameter.getGenericParameterType();
|
||||
if (!seen.contains(methodParameterType)) {
|
||||
registerReflectionHints(hints, seen, methodParameterType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerKotlinSerializationHints(ReflectionHints hints, Class<?> clazz) {
|
||||
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
|
||||
if (ClassUtils.isPresent(companionClassName, null)) {
|
||||
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
|
||||
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
|
||||
if (serializerMethod != null) {
|
||||
hints.registerMethod(serializerMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
|
||||
if (seen.contains(type)) {
|
||||
return;
|
||||
}
|
||||
ResolvableType resolvableType = ResolvableType.forType(type);
|
||||
Class<?> clazz = resolvableType.resolve();
|
||||
if (clazz != null && !types.contains(clazz)) {
|
||||
types.add(clazz);
|
||||
for (ResolvableType genericResolvableType : resolvableType.getGenerics()) {
|
||||
collectReferencedTypes(seen, types, genericResolvableType.getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,310 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.aot;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link BindingReflectionHintsRegistrar}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class BindingReflectionHintsRegistrarTests {
|
||||
|
||||
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
|
||||
|
||||
private final RuntimeHints hints = new RuntimeHints();
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithEmptyClass() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleEmptyClass.class);
|
||||
assertThat(this.hints.reflection().typeHints()).singleElement()
|
||||
.satisfies(typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleEmptyClass.class));
|
||||
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
|
||||
MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithNoProperty() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithNoProperty.class);
|
||||
assertThat(this.hints.reflection().typeHints()).singleElement()
|
||||
.satisfies(typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithNoProperty.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithGetter() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithGetter.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithGetter.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("getName");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithSetter() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithSetter.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithSetter.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("setName");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithListProperty() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithListProperty.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(List.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithListProperty.class));
|
||||
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
|
||||
methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("setNames");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
},
|
||||
methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("getNames");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithCycles() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithCycles.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithCycles.class)),
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(List.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithResolvableType() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithResolvableType.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(ResolvableType[].class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Type.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Class.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(ResolvableType.class));
|
||||
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
|
||||
MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).hasSizeGreaterThan(1);
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassWithResolvableType.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(
|
||||
methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("getResolvableType");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithMultipleLevelsAndCollection() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassA.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassA.class)),
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassB.class)),
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleClassC.class)),
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)),
|
||||
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Set.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithEnum() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleEnum.class);
|
||||
assertThat(this.hints.reflection().typeHints()).singleElement()
|
||||
.satisfies(typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleEnum.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTypeForSerializationWithRecord() {
|
||||
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleRecord.class);
|
||||
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class));
|
||||
assertThat(typeHint.getMemberCategories()).isEmpty();
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
},
|
||||
typeHint -> {
|
||||
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleRecord.class));
|
||||
assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> {
|
||||
assertThat(methodHint.getName()).isEqualTo("name");
|
||||
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INVOKE);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
static class SampleEmptyClass {
|
||||
}
|
||||
|
||||
static class SampleClassWithNoProperty {
|
||||
|
||||
String name() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassWithGetter {
|
||||
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public SampleEmptyClass unmanaged() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassWithSetter {
|
||||
|
||||
public void setName(String name) {
|
||||
}
|
||||
|
||||
public SampleEmptyClass unmanaged() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassWithListProperty {
|
||||
|
||||
public List<String> getNames() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setNames(List<String> names) {
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassWithCycles {
|
||||
|
||||
public SampleClassWithCycles getSampleClassWithCycles() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<SampleClassWithCycles> getSampleClassWithCyclesList() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassWithResolvableType {
|
||||
|
||||
public ResolvableType getResolvableType() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassA {
|
||||
public Set<SampleClassB> getB() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SampleClassB {
|
||||
public SampleClassC getC() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class SampleClassC {
|
||||
public String getString() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
enum SampleEnum {
|
||||
value1, value2
|
||||
}
|
||||
|
||||
record SampleRecord(String name) {}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.aot
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.ThrowingConsumer
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.aot.hint.*
|
||||
|
||||
/**
|
||||
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class KotlinBindingReflectionHintsRegistrarTests {
|
||||
|
||||
private val bindingRegistrar = BindingReflectionHintsRegistrar()
|
||||
|
||||
private val hints = RuntimeHints()
|
||||
|
||||
@Test
|
||||
fun `Register type for Kotlinx serialization`() {
|
||||
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleSerializableClass::class.java)
|
||||
assertThat(hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
|
||||
ThrowingConsumer { typeHint: TypeHint ->
|
||||
assertThat(typeHint.type).isEqualTo(TypeReference.of(String::class.java))
|
||||
assertThat(typeHint.memberCategories).isEmpty()
|
||||
assertThat(typeHint.constructors()).isEmpty()
|
||||
assertThat(typeHint.fields()).isEmpty()
|
||||
assertThat(typeHint.methods()).isEmpty()
|
||||
},
|
||||
ThrowingConsumer { typeHint: TypeHint ->
|
||||
assertThat(typeHint.type).isEqualTo(TypeReference.of(SampleSerializableClass::class.java))
|
||||
assertThat(typeHint.methods()).singleElement()
|
||||
.satisfies(ThrowingConsumer { methodHint: ExecutableHint ->
|
||||
assertThat(methodHint.name).isEqualTo("getName")
|
||||
assertThat(methodHint.mode).isEqualTo(ExecutableMode.INVOKE)
|
||||
})
|
||||
},
|
||||
ThrowingConsumer { typeHint: TypeHint ->
|
||||
assertThat(typeHint.type).isEqualTo(TypeReference.of(SampleSerializableClass::class.qualifiedName + "\$Companion"))
|
||||
assertThat(typeHint.methods()).singleElement()
|
||||
.satisfies(ThrowingConsumer { methodHint: ExecutableHint ->
|
||||
assertThat(methodHint.name).isEqualTo("serializer")
|
||||
assertThat(methodHint.mode).isEqualTo(ExecutableMode.INVOKE)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
class SampleSerializableClass(val name: String)
|
||||
Reference in New Issue
Block a user