Polishing

This commit is contained in:
Sam Brannen
2022-10-08 13:35:48 +02:00
parent ced37d53b4
commit c98ed17728
4 changed files with 25 additions and 35 deletions

View File

@@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
@@ -33,8 +32,10 @@ import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
/**
* Process {@link Reflective} annotated elements.
* Process {@link Reflective @Reflective} annotated elements.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
@@ -53,12 +54,12 @@ public class ReflectiveRuntimeHintsRegistrar {
*/
public void registerRuntimeHints(RuntimeHints runtimeHints, Class<?>... types) {
Set<Entry> entries = new HashSet<>();
Arrays.stream(types).forEach(type -> {
for (Class<?> type : types) {
processType(entries, type);
for (Class<?> implementedInterface : ClassUtils.getAllInterfacesForClass(type)) {
processType(entries, implementedInterface);
}
});
}
entries.forEach(entry -> {
AnnotatedElement element = entry.element();
entry.processor().registerReflectionHints(runtimeHints.reflection(), element);
@@ -86,20 +87,21 @@ public class ReflectiveRuntimeHintsRegistrar {
}
private boolean isReflective(AnnotatedElement element) {
return MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).isPresent(Reflective.class);
return MergedAnnotations.from(element, TYPE_HIERARCHY).isPresent(Reflective.class);
}
@SuppressWarnings("unchecked")
private Entry createEntry(AnnotatedElement element) {
List<ReflectiveProcessor> processors =
MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(Reflective.class).flatMap(annotation -> Stream.of(annotation.getClassArray("value")))
.map(type -> (Class<? extends ReflectiveProcessor>) type)
.distinct()
.map(processorClass -> this.processors.computeIfAbsent(processorClass, this::instantiateClass))
.toList();
List<ReflectiveProcessor> processors = MergedAnnotations.from(element, TYPE_HIERARCHY)
.stream(Reflective.class)
.map(annotation -> annotation.getClassArray("value"))
.flatMap(Arrays::stream)
.map(type -> (Class<? extends ReflectiveProcessor>) type)
.distinct()
.map(processorClass -> this.processors.computeIfAbsent(processorClass, this::instantiateClass))
.toList();
ReflectiveProcessor processorToUse = (processors.size() == 1 ? processors.get(0)
: new DelegateReflectiveProcessor(processors));
: new DelegatingReflectiveProcessor(processors));
return new Entry(element, processorToUse);
}
@@ -114,11 +116,11 @@ public class ReflectiveRuntimeHintsRegistrar {
}
}
static class DelegateReflectiveProcessor implements ReflectiveProcessor {
private static class DelegatingReflectiveProcessor implements ReflectiveProcessor {
private final Iterable<ReflectiveProcessor> processors;
public DelegateReflectiveProcessor(Iterable<ReflectiveProcessor> processors) {
DelegatingReflectiveProcessor(Iterable<ReflectiveProcessor> processors) {
this.processors = processors;
}

View File

@@ -132,6 +132,7 @@ class ReflectiveRuntimeHintsRegistrarTests {
this.registrar.registerRuntimeHints(this.runtimeHints, beanClass);
}
@Reflective
@SuppressWarnings("unused")
static class SampleTypeAnnotatedBean {
@@ -139,7 +140,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
private String notManaged;
public void notManaged() {
}
}
@@ -148,13 +148,10 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Reflective
SampleConstructorAnnotatedBean(String name) {
}
SampleConstructorAnnotatedBean(Integer nameAsNumber) {
}
}
@SuppressWarnings("unused")
@@ -176,7 +173,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
@SuppressWarnings("unused")
@@ -188,7 +184,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
@SuppressWarnings("unused")
@@ -200,7 +195,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
void notManaged() {
}
}
static class SampleMethodAnnotatedBeanWithInterface implements SampleInterface {
@@ -211,7 +205,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public void notManaged() {
}
}
static class SampleMethodAnnotatedBeanWithInheritance extends SampleInheritedClass {
@@ -222,7 +215,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public void notManaged() {
}
}
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@@ -251,7 +243,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Documented
@Reflective(TestTypeHintReflectiveProcessor.class)
@interface ReflectiveWithCustomProcessor {
}
interface SampleInterface {
@@ -273,7 +264,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public String managed() {
return "test";
}
}
@Reflective
@@ -283,7 +273,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
public String managed() {
return "test";
}
}
private static class TestMethodHintReflectiveProcessor extends SimpleReflectiveProcessor {
@@ -293,7 +282,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
super.registerMethodHint(hints, method);
hints.registerType(method.getReturnType(), MemberCategory.INVOKE_DECLARED_METHODS);
}
}
private static class TestTypeHintReflectiveProcessor extends SimpleReflectiveProcessor {
@@ -303,7 +291,6 @@ class ReflectiveRuntimeHintsRegistrarTests {
super.registerTypeHint(hints, type);
hints.registerType(type, MemberCategory.INVOKE_DECLARED_METHODS);
}
}
}