Infer reflection hints for Jackson annotation class attributes

Closes gh-29646
Closes gh-29386
This commit is contained in:
Sébastien Deleuze
2023-01-23 13:21:34 +01:00
parent 1e47f31210
commit e6397c8a38
3 changed files with 56 additions and 11 deletions

View File

@@ -16,12 +16,15 @@
package org.springframework.aot.hint;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.RecordComponent;
import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;
import kotlin.jvm.JvmClassMappingKt;
import kotlin.reflect.KClass;
@@ -161,27 +164,32 @@ public class BindingReflectionHintsRegistrar {
private void registerJacksonHints(ReflectionHints hints, Class<?> clazz) {
ReflectionUtils.doWithFields(clazz, field ->
MergedAnnotations
.from(field, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(JACKSON_ANNOTATION)
.filter(MergedAnnotation::isMetaPresent)
.forEach(annotation -> {
forEachJacksonAnnotation(field, annotation -> {
Field sourceField = (Field) annotation.getSource();
if (sourceField != null) {
hints.registerField(sourceField);
}
}));
ReflectionUtils.doWithMethods(clazz, method ->
MergedAnnotations
.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(JACKSON_ANNOTATION)
.filter(MergedAnnotation::isMetaPresent)
.forEach(annotation -> {
forEachJacksonAnnotation(method, annotation -> {
Method sourceMethod = (Method) annotation.getSource();
if (sourceMethod != null) {
hints.registerMethod(sourceMethod, ExecutableMode.INVOKE);
}
}));
forEachJacksonAnnotation(clazz, annotation -> annotation.getRoot().asMap().values().forEach(value -> {
if (value instanceof Class<?> classValue) {
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}));
}
private void forEachJacksonAnnotation(AnnotatedElement element, Consumer<MergedAnnotation<Annotation>> action) {
MergedAnnotations
.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(JACKSON_ANNOTATION)
.filter(MergedAnnotation::isMetaPresent)
.forEach(action::accept);
}
/**