This commit is contained in:
Stephane Nicoll 2022-06-14 14:11:21 +02:00
parent d6d4b98780
commit 77ad4a1428
3 changed files with 48 additions and 34 deletions

View File

@ -34,14 +34,16 @@ import org.springframework.aot.hint.ExecutableHint;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
/**
* Register the necessary reflection hints so that the specified type can be bound/serialized at runtime.
* Fields, constructors and property methods are registered, except for a set of types like those in
* {@code java.} package where just the type is registered. Types are discovered transitively and
* generic types are registered as well.
* 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
@ -53,15 +55,39 @@ public class BindingReflectionHintsRegistrar {
private static final Consumer<ExecutableHint.Builder> INVOKE = builder -> builder
.withMode(ExecutableMode.INVOKE);
/**
* 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) {
for (Type type : types) {
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
collectReferencedTypes(new HashSet<>(), referencedTypes, type);
referencedTypes.forEach(referencedType -> hints.registerType(referencedType, builder -> {
for (Class<?> referencedType : referencedTypes) {
hints.registerType(referencedType, builder -> {
if (shouldRegisterMembers(referencedType)) {
builder.withMembers(MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
registerMembers(hints, referencedType, builder);
}
});
}
}
}
/**
* 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.");
}
private void registerMembers(ReflectionHints hints, Class<?> type, Builder builder) {
builder.withMembers(MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
try {
BeanInfo beanInfo = Introspector.getBeanInfo(referencedType);
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method writeMethod = propertyDescriptor.getWriteMethod();
@ -80,22 +106,10 @@ public class BindingReflectionHintsRegistrar {
}
catch (IntrospectionException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring referenced type [" + referencedType.getName() + "]: " + ex.getMessage());
logger.debug("Ignoring referenced type [" + type.getName() + "]: " + ex.getMessage());
}
}
}
}));
}
}
/**
* 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.");
}
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
if (seen.contains(type)) {

View File

@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class BindingReflectionHintsRegistrarTests {
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
private final RuntimeHints hints = new RuntimeHints();
@Test

View File

@ -34,14 +34,13 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
* with {@link ResponseBody} and parameters annotated with {@link RequestBody}
* which are serialized as well.
*
*
* @author Stephane Nicoll
* @author Sebastien Deleuze
* @since 6.0
*/
class RequestMappingReflectiveProcessor implements ReflectiveProcessor {
private BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {