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

@@ -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));

View File

@@ -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">
* &#064;Configuration
* &#064;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">
* &#064;Component
* &#064;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">
* &#064;Component
* public class MyComponent {
* class MyProcessor {
*
* &#064;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
*/

View File

@@ -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 &mdash; for example:
* <p>You can use this annotation on any bean that is contributed to the context:
* <pre><code class="java">
* &#064;Configuration
* &#064;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">
* &#064;Component
* &#064;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">
* &#064;Component
* public class MyService {
* class MyService {
*
* &#064;RegisterReflectionForBinding(Baz.class)
* public Baz process() {

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();