Skip to content

Commit 916a871

Browse files
committed
Add dedicated hint support for composable annotations
This commit adds a dedicated method for annotations that are used as meta-annotation when the composed annotation does not require to be visible at runtime. Closes gh-28887
1 parent 1aa4e7c commit 916a871

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

spring-core/src/main/java/org/springframework/aot/hint/support/RuntimeHintsUtils.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,32 @@ public abstract class RuntimeHintsUtils {
5757
* @see SynthesizedAnnotation
5858
*/
5959
public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) {
60+
registerAnnotation(hints, annotationType, false);
61+
}
62+
63+
/**
64+
* Register the necessary hints so that the specified <em>composable</em>
65+
* annotation is visible at runtime. Use this method rather than the regular
66+
* {@link #registerAnnotation(RuntimeHints, Class)} when the specified
67+
* annotation is meta-annotated, but the meta-annotated annotations do not
68+
* need to be visible.
69+
* @param hints the {@link RuntimeHints} instance to use
70+
* @param annotationType the composable annotation type
71+
* @see #registerAnnotation(RuntimeHints, Class)
72+
*/
73+
public static void registerComposableAnnotation(RuntimeHints hints, Class<?> annotationType) {
74+
registerAnnotation(hints, annotationType, true);
75+
}
76+
77+
private static void registerAnnotation(RuntimeHints hints, Class<?> annotationType, boolean withProxy) {
6078
hints.reflection().registerType(annotationType, ANNOTATION_HINT);
6179
Set<Class<?>> allAnnotations = new LinkedHashSet<>();
6280
collectAliasedAnnotations(new HashSet<>(), allAnnotations, annotationType);
6381
allAnnotations.forEach(annotation -> {
6482
hints.reflection().registerType(annotation, ANNOTATION_HINT);
6583
hints.proxies().registerJdkProxy(annotation, SynthesizedAnnotation.class);
6684
});
67-
if (!allAnnotations.isEmpty()) {
85+
if (!allAnnotations.isEmpty() || withProxy) {
6886
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
6987
}
7088
}

spring-core/src/test/java/org/springframework/aot/hint/support/RuntimeHintsUtilsTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ void registerAnnotationType() {
5050
assertThat(this.hints.proxies().jdkProxies()).isEmpty();
5151
}
5252

53+
@Test
54+
void registerComposableAnnotationType() {
55+
RuntimeHintsUtils.registerComposableAnnotation(this.hints, SampleInvoker.class);
56+
assertThat(this.hints.reflection().typeHints()).singleElement()
57+
.satisfies(annotationHint(SampleInvoker.class));
58+
assertThat(this.hints.proxies().jdkProxies()).singleElement()
59+
.satisfies(annotationProxy(SampleInvoker.class));
60+
}
61+
5362
@Test
5463
void registerAnnotationTypeWithLocalUseOfAliasForRegistersProxy() {
5564
RuntimeHintsUtils.registerAnnotation(this.hints, LocalMapping.class);

0 commit comments

Comments
 (0)