Skip to content

Commit eb91d21

Browse files
committed
Polishing
1 parent 1f0a35b commit eb91d21

File tree

13 files changed

+69
-36
lines changed

13 files changed

+69
-36
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
3838

3939
private final ReflectiveRuntimeHintsRegistrar reflectiveRegistrar = new ReflectiveRuntimeHintsRegistrar();
4040

41+
4142
@Override
4243
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
4344
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
@@ -53,4 +54,5 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
5354
private void typeHint(Builder typeHint) {
5455
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS).onReachableType(SchedulerFactoryBean.class);
5556
}
57+
5658
}

spring-context/src/main/java/org/springframework/context/annotation/ImportRuntimeHints.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* should be processed.
3030
*
3131
* <p>Unlike declaring {@link RuntimeHintsRegistrar} using
32-
* {@code spring/aot.factories}, this annotation allows for more flexible
32+
* {@code META-INF/spring/aot.factories}, this annotation allows for more flexible
3333
* registration where it is only processed if the annotated component or bean
3434
* method is actually registered in the bean factory. To illustrate this
3535
* behavior, consider the following example:
@@ -47,19 +47,24 @@
4747
*
4848
* }</pre>
4949
*
50-
* If the configuration class above is processed, {@code MyHints} will be
51-
* contributed only if {@code MyCondition} matches. If it does not, and
52-
* therefore {@code MyService} is not defined as a bean, the hints will
50+
* <p>If the configuration class above is processed, {@code MyHints} will be
51+
* contributed only if {@code MyCondition} matches. If the condition does not
52+
* match, {@code MyService} will not be defined as a bean and the hints will
5353
* not be processed either.
5454
*
55-
* <p>If several components refer to the same {@link RuntimeHintsRegistrar}
56-
* implementation, it is invoked only once for a given bean factory
57-
* processing.
55+
* <p>{@code @ImportRuntimeHints} can also be applied to any test class that uses
56+
* the <em>Spring TestContext Framework</em> to load an {@code ApplicationContext}.
57+
*
58+
* <p>If several components or test classes refer to the same {@link RuntimeHintsRegistrar}
59+
* implementation, the registrar will only be invoked once for the given bean factory
60+
* processing or test suite.
5861
*
5962
* @author Brian Clozel
6063
* @author Stephane Nicoll
6164
* @since 6.0
6265
* @see org.springframework.aot.hint.RuntimeHints
66+
* @see org.springframework.aot.hint.annotation.Reflective
67+
* @see org.springframework.aot.hint.annotation.RegisterReflectionForBinding
6368
*/
6469
@Target({ElementType.TYPE, ElementType.METHOD})
6570
@Retention(RetentionPolicy.RUNTIME)

spring-core/src/main/java/org/springframework/aot/hint/annotation/Reflective.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
*
3030
* <p>When present, either directly or as a meta-annotation, this annotation
3131
* triggers the configured {@linkplain ReflectiveProcessor processors} against
32-
* the annotated element. By default, a reflection hint is added on the
32+
* the annotated element. By default, a reflection hint is registered for the
3333
* annotated element so that it can be discovered and invoked if necessary.
3434
*
3535
* @author Stephane Nicoll
3636
* @author Sam Brannen
3737
* @since 6.0
3838
* @see SimpleReflectiveProcessor
3939
* @see ReflectiveRuntimeHintsRegistrar
40+
* @see RegisterReflectionForBinding @RegisterReflectionForBinding
4041
*/
4142
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.CONSTRUCTOR,
4243
ElementType.FIELD, ElementType.METHOD })

spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
* Process an {@link AnnotatedElement} and register the necessary reflection
2525
* hints for it.
2626
*
27+
* <p>{@code ReflectiveProcessor} implementations are registered via
28+
* {@link Reflective#processors() @Reflective(processors = ...)}.
29+
*
2730
* @author Stephane Nicoll
2831
* @since 6.0
32+
* @see Reflective @Reflective
2933
*/
3034
public interface ReflectiveProcessor {
3135

spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ private Entry createEntry(AnnotatedElement element) {
9696
.stream(Reflective.class)
9797
.map(annotation -> annotation.getClassArray("value"))
9898
.flatMap(Arrays::stream)
99-
.map(type -> (Class<? extends ReflectiveProcessor>) type)
10099
.distinct()
100+
.map(type -> (Class<? extends ReflectiveProcessor>) type)
101101
.map(processorClass -> this.processors.computeIfAbsent(processorClass, this::instantiateClass))
102102
.toList();
103-
ReflectiveProcessor processorToUse = (processors.size() == 1 ? processors.get(0)
104-
: new DelegatingReflectiveProcessor(processors));
103+
ReflectiveProcessor processorToUse = (processors.size() == 1 ? processors.get(0) :
104+
new DelegatingReflectiveProcessor(processors));
105105
return new Entry(element, processorToUse);
106106
}
107107

spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBinding.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,24 @@
2525
import org.springframework.core.annotation.AliasFor;
2626

2727
/**
28-
* Indicate that the classes specified in the annotation attributes require some
28+
* Indicates that the classes specified in the annotation attributes require some
2929
* reflection hints for binding or reflection-based serialization purposes. For each
3030
* class specified, hints on constructors, fields, properties, record components,
3131
* including types transitively used on properties and record components are registered.
3232
* At least one class must be specified in the {@code value} or {@code classes} annotation
3333
* attributes.
3434
*
35-
* <p>The annotated element can be a configuration class, for example:
35+
* <p>The annotated element can be a configuration class &mdash; for example:
3636
*
3737
* <pre class="code">
3838
* &#064;Configuration
3939
* &#064;RegisterReflectionForBinding({ Foo.class, Bar.class })
4040
* public class MyConfig {
41-
*
4241
* // ...
4342
* }</pre>
4443
*
45-
* <p>The annotated element can also be any Spring bean class, constructor, field, or method.
46-
* For example:
44+
* <p>The annotated element can be any Spring bean class, constructor, field,
45+
* or method &mdash; for example:
4746
*
4847
* <pre class="code">
4948
* &#064;Service
@@ -56,9 +55,13 @@
5655
*
5756
* }</pre>
5857
*
58+
* <p>The annotated element can also be any test class that uses the <em>Spring
59+
* TestContext Framework</em> to load an {@code ApplicationContext}.
60+
*
5961
* @author Sebastien Deleuze
6062
* @since 6.0
6163
* @see org.springframework.aot.hint.BindingReflectionHintsRegistrar
64+
* @see Reflective @Reflective
6265
*/
6366
@Target({ ElementType.TYPE, ElementType.METHOD })
6467
@Retention(RetentionPolicy.RUNTIME)
@@ -67,10 +70,7 @@
6770
public @interface RegisterReflectionForBinding {
6871

6972
/**
70-
* Classes for which reflection hints should be registered.
71-
* <p>At least one class must be specified either via {@link #value} or
72-
* {@link #classes}.
73-
* @see #classes()
73+
* Alias for {@link #classes()}.
7474
*/
7575
@AliasFor("classes")
7676
Class<?>[] value() default {};

spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public class RegisterReflectionForBindingProcessor implements ReflectiveProcesso
3636

3737
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
3838

39+
3940
@Override
4041
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
41-
RegisterReflectionForBinding registerReflection = AnnotationUtils.getAnnotation(element, RegisterReflectionForBinding.class);
42+
RegisterReflectionForBinding registerReflection =
43+
AnnotationUtils.getAnnotation(element, RegisterReflectionForBinding.class);
4244
if (registerReflection != null) {
4345
Class<?>[] classes = registerReflection.classes();
4446
Assert.state(classes.length != 0, () -> "A least one class should be specified in " +

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/MessageMappingReflectiveProcessor.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434

3535
/**
3636
* {@link ReflectiveProcessor} implementation for {@link MessageMapping}
37-
* annotated types. On top of registering reflection hints for invoking
37+
* annotated types. In addition to registering reflection hints for invoking
3838
* the annotated method, this implementation handles:
39+
*
3940
* <ul>
40-
* <li>Return types.</li>
41-
* <li>Parameters identified as potential payload.</li>
42-
* <li>{@link Message} parameters.</li>
41+
* <li>Return types</li>
42+
* <li>Parameters identified as potential payloads</li>
43+
* <li>{@link Message} parameters</li>
4344
* </ul>
4445
*
4546
* @author Sebastien Deleuze
@@ -49,6 +50,7 @@ class MessageMappingReflectiveProcessor implements ReflectiveProcessor {
4950

5051
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
5152

53+
5254
@Override
5355
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
5456
if (element instanceof Class<?> type) {
@@ -99,6 +101,8 @@ protected void registerReturnValueHints(ReflectionHints hints, Method method) {
99101
@Nullable
100102
protected Type getMessageType(MethodParameter parameter) {
101103
MethodParameter nestedParameter = parameter.nested();
102-
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ? null : nestedParameter.getNestedParameterType());
104+
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ?
105+
null : nestedParameter.getNestedParameterType());
103106
}
107+
104108
}

spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,20 @@ public void processAheadOfTime(Stream<Class<?>> testClasses) throws TestContextA
137137
mergedConfigMappings.add(mergedConfig, testClass);
138138
collectRuntimeHintsRegistrarClasses(testClass, coreRuntimeHintsRegistrarClasses);
139139
reflectiveRuntimeHintsRegistrar.registerRuntimeHints(this.runtimeHints, testClass);
140-
this.testRuntimeHintsRegistrars.forEach(registrar ->
141-
registrar.registerHints(this.runtimeHints, testClass, classLoader));
140+
this.testRuntimeHintsRegistrars.forEach(registrar -> {
141+
if (logger.isTraceEnabled()) {
142+
logger.trace("Processing RuntimeHints contribution from class [%s]"
143+
.formatted(registrar.getClass().getCanonicalName()));
144+
}
145+
registrar.registerHints(this.runtimeHints, testClass, classLoader);
146+
});
142147
});
143148

144149
coreRuntimeHintsRegistrarClasses.stream()
145150
.map(BeanUtils::instantiateClass)
146151
.forEach(registrar -> {
147152
if (logger.isTraceEnabled()) {
148-
logger.trace("Processing RuntimeHints contribution from test class [%s]"
153+
logger.trace("Processing RuntimeHints contribution from class [%s]"
149154
.formatted(registrar.getClass().getCanonicalName()));
150155
}
151156
registrar.registerHints(this.runtimeHints, classLoader);

spring-test/src/main/java/org/springframework/test/context/aot/TestRuntimeHintsRegistrar.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
* specific to particular test classes, favor implementing {@code RuntimeHintsRegistrar}
3535
* over this API.
3636
*
37+
* <p>As an alternative to implementing and registering a {@code TestRuntimeHintsRegistrar},
38+
* you may choose to annotate a test class with
39+
* {@link org.springframework.aot.hint.annotation.Reflective @Reflective},
40+
* {@link org.springframework.aot.hint.annotation.RegisterReflectionForBinding @RegisterReflectionForBinding},
41+
* or {@link org.springframework.context.annotation.ImportRuntimeHints @ImportRuntimeHints}.
42+
*
3743
* @author Sam Brannen
3844
* @since 6.0
3945
* @see org.springframework.aot.hint.RuntimeHintsRegistrar

spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerMappingReflectiveProcessor.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333

3434
/**
3535
* {@link ReflectiveProcessor} implementation for {@link Controller} and
36-
* controller-specific annotated methods. On top of registering reflection
36+
* controller-specific annotated methods. In addition to registering reflection
3737
* hints for invoking the annotated method, this implementation handles:
38+
*
3839
* <ul>
39-
* <li>Return types annotated with {@link ResponseBody}.</li>
40-
* <li>Parameters annotated with {@link RequestBody}.</li>
41-
* <li>{@link HttpEntity} return type and parameters.</li>
40+
* <li>Return types annotated with {@link ResponseBody}</li>
41+
* <li>Parameters annotated with {@link RequestBody}</li>
42+
* <li>{@link HttpEntity} return types and parameters</li>
4243
* </ul>
4344
*
4445
* @author Stephane Nicoll
@@ -49,6 +50,7 @@ class ControllerMappingReflectiveProcessor implements ReflectiveProcessor {
4950

5051
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
5152

53+
5254
@Override
5355
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
5456
if (element instanceof Class<?> type) {
@@ -98,8 +100,8 @@ else if (HttpEntity.class.isAssignableFrom(returnTypeParameter.getParameterType(
98100
@Nullable
99101
private Type getHttpEntityType(MethodParameter parameter) {
100102
MethodParameter nestedParameter = parameter.nested();
101-
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType()
102-
? null : nestedParameter.getNestedParameterType());
103+
return (nestedParameter.getNestedParameterType() == nestedParameter.getParameterType() ?
104+
null : nestedParameter.getNestedParameterType());
103105
}
104106

105107
}

spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandlerReflectiveProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ protected void registerReturnTypeHints(ReflectionHints hints, MethodParameter re
3737
}
3838
super.registerReturnTypeHints(hints, returnTypeParameter);
3939
}
40+
4041
}

spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchangeReflectiveProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* {@link ReflectiveProcessor} implementation for {@link HttpExchange @HttpExchange}
32-
* and annotated methods. On top of registering reflection hints for invoking
32+
* annotated methods. In addition to registering reflection hints for invoking
3333
* the annotated method, this implementation handles reflection-based
3434
* binding for return types and parameters annotated with {@link RequestBody}.
3535
*
@@ -40,6 +40,7 @@ class HttpExchangeReflectiveProcessor implements ReflectiveProcessor {
4040

4141
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
4242

43+
4344
@Override
4445
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
4546
if (element instanceof Method method) {

0 commit comments

Comments
 (0)