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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -24,6 +24,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.aot.hint.ReflectionHints;
|
||||
@@ -66,6 +67,28 @@ public class ReflectiveRuntimeHintsRegistrar {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the given {@code type} is a valid candidate.
|
||||
* @param type the type to inspect
|
||||
* @return {@code true} if the type uses {@link Reflective} in a way that
|
||||
* is supported by this registrar
|
||||
* @since 6.2
|
||||
*/
|
||||
public boolean isCandidate(Class<?> type) {
|
||||
if (isReflective(type)) {
|
||||
return true;
|
||||
}
|
||||
AtomicBoolean candidate = new AtomicBoolean(false);
|
||||
doWithReflectiveConstructors(type, constructor -> candidate.set(true));
|
||||
if (!candidate.get()) {
|
||||
ReflectionUtils.doWithFields(type, field -> candidate.set(true), this::isReflective);
|
||||
}
|
||||
if (!candidate.get()) {
|
||||
ReflectionUtils.doWithMethods(type, method -> candidate.set(true), this::isReflective);
|
||||
}
|
||||
return candidate.get();
|
||||
}
|
||||
|
||||
private void processType(Set<Entry> entries, Class<?> typeToProcess) {
|
||||
if (isReflective(typeToProcess)) {
|
||||
entries.add(createEntry(typeToProcess));
|
||||
|
||||
@@ -36,26 +36,28 @@ import org.springframework.aot.hint.MemberCategory;
|
||||
* <p>This annotation can be used as a meta-annotation to customize how hints
|
||||
* are registered against each target class.
|
||||
*
|
||||
* <p>The annotated element can be any bean:
|
||||
* <p>You can use this annotation on any bean that is contributed to the context:
|
||||
* <pre><code class="java">
|
||||
* @Configuration
|
||||
* @RegisterReflection(classes = CustomerEntry.class, memberCategories = PUBLIC_FIELDS)
|
||||
* public class MyConfig {
|
||||
* class MyConfig {
|
||||
* // ...
|
||||
* }</code></pre>
|
||||
*
|
||||
* <p>If scanning of {@link Reflective} is enabled, any type in the configured
|
||||
* packages can use this annotation as well.
|
||||
*
|
||||
* <p>To register reflection hints for the type itself, only member categories
|
||||
* should be specified:<pre><code class="java">
|
||||
* @Component
|
||||
* @RegisterReflection(memberCategories = INVOKE_PUBLIC_METHODS)
|
||||
* public class MyComponent {
|
||||
* class MyComponent {
|
||||
* // ...
|
||||
* }</code></pre>
|
||||
*
|
||||
* <p>Reflection hints can be registered from a method. In this case, at least
|
||||
* one target class should be specified:<pre><code class="java">
|
||||
* @Component
|
||||
* public class MyComponent {
|
||||
* class MyProcessor {
|
||||
*
|
||||
* @RegisterReflection(classes = CustomerEntry.class, memberCategories = PUBLIC_FIELDS)
|
||||
* CustomerEntry process() { ... }
|
||||
@@ -65,6 +67,9 @@ import org.springframework.aot.hint.MemberCategory;
|
||||
* <p>If the class is not available, {@link #classNames()} allows to specify the
|
||||
* fully qualified name, rather than the {@link Class} reference.
|
||||
*
|
||||
* <p>The annotated element can also be any test class that uses the <em>Spring
|
||||
* TestContext Framework</em> to load an {@code ApplicationContext}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 6.2
|
||||
*/
|
||||
|
||||
@@ -32,26 +32,29 @@ import org.springframework.core.annotation.AliasFor;
|
||||
* and record components. Hints are also registered for types transitively used
|
||||
* on properties and record components.
|
||||
*
|
||||
* <p>The annotated element can be a configuration class — for example:
|
||||
* <p>You can use this annotation on any bean that is contributed to the context:
|
||||
* <pre><code class="java">
|
||||
* @Configuration
|
||||
* @RegisterReflectionForBinding({Foo.class, Bar.class})
|
||||
* public class MyConfig {
|
||||
* class MyConfig {
|
||||
* // ...
|
||||
* }</code></pre>
|
||||
*
|
||||
* <p>If scanning of {@link Reflective} is enabled, any type in the configured
|
||||
* packages can use this annotation as well.
|
||||
*
|
||||
* <p>When the annotated element is a type, the type itself is registered if no
|
||||
* candidates are provided:<pre><code class="java">
|
||||
* @Component
|
||||
* @RegisterReflectionForBinding
|
||||
* public class MyBean {
|
||||
* class MyBean {
|
||||
* // ...
|
||||
* }</code></pre>
|
||||
*
|
||||
* The annotation can also be specified on a method. In that case, at least one
|
||||
* target class must be specified:<pre><code class="java">
|
||||
* @Component
|
||||
* public class MyService {
|
||||
* class MyService {
|
||||
*
|
||||
* @RegisterReflectionForBinding(Baz.class)
|
||||
* public Baz process() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user