Introduce ReflectiveScan

This commit allows `@Reflective` to be used on arbitrary types, not
only Spring beans. This makes the feature much more powerful as
components can be tagged directly.

Scanning happens during AOT processing (typically at build-time) when
`@ReflectiveScan` is used. Types do not need to have a particular
annotation, and types that can't be loaded are ignored.

This commit also exposes the infrastructure that does the scanning so
that custom code can do the scanning in an AOT contribution if they
don't want to rely on the annotation.

Closes gh-33132
This commit is contained in:
Stéphane Nicoll
2024-05-03 16:12:39 +02:00
parent f1658079a5
commit 93587da394
25 changed files with 887 additions and 39 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.aot.hint.annotation;
import java.io.Closeable;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -24,6 +25,8 @@ import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.aot.hint.FieldHint;
import org.springframework.aot.hint.MemberCategory;
@@ -50,6 +53,20 @@ class ReflectiveRuntimeHintsRegistrarTests {
private final RuntimeHints runtimeHints = new RuntimeHints();
@ParameterizedTest
@ValueSource(classes = { SampleTypeAnnotatedBean.class, SampleFieldAnnotatedBean.class,
SampleConstructorAnnotatedBean.class, SampleMethodAnnotatedBean.class, SampleInterface.class,
SampleMethodMetaAnnotatedBeanWithAlias.class, SampleMethodAnnotatedBeanWithInterface.class })
void isCandidateWithMatchingAnnotatedElement(Class<?> candidate) {
assertThat(this.registrar.isCandidate(candidate)).isTrue();
}
@ParameterizedTest
@ValueSource(classes = { String.class, Closeable.class })
void isCandidateWithNonMatchingAnnotatedElement(Class<?> candidate) {
assertThat(this.registrar.isCandidate(candidate)).isFalse();
}
@Test
void shouldIgnoreNonAnnotatedType() {
RuntimeHints mock = mock();