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
This commit is contained in:
Stephane Nicoll 2022-07-29 15:38:09 +02:00
parent 1aa4e7c68d
commit 916a871fbc
2 changed files with 28 additions and 1 deletions

View File

@ -57,6 +57,24 @@ public abstract class RuntimeHintsUtils {
* @see SynthesizedAnnotation * @see SynthesizedAnnotation
*/ */
public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) { public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) {
registerAnnotation(hints, annotationType, false);
}
/**
* Register the necessary hints so that the specified <em>composable</em>
* annotation is visible at runtime. Use this method rather than the regular
* {@link #registerAnnotation(RuntimeHints, Class)} when the specified
* annotation is meta-annotated, but the meta-annotated annotations do not
* need to be visible.
* @param hints the {@link RuntimeHints} instance to use
* @param annotationType the composable annotation type
* @see #registerAnnotation(RuntimeHints, Class)
*/
public static void registerComposableAnnotation(RuntimeHints hints, Class<?> annotationType) {
registerAnnotation(hints, annotationType, true);
}
private static void registerAnnotation(RuntimeHints hints, Class<?> annotationType, boolean withProxy) {
hints.reflection().registerType(annotationType, ANNOTATION_HINT); hints.reflection().registerType(annotationType, ANNOTATION_HINT);
Set<Class<?>> allAnnotations = new LinkedHashSet<>(); Set<Class<?>> allAnnotations = new LinkedHashSet<>();
collectAliasedAnnotations(new HashSet<>(), allAnnotations, annotationType); collectAliasedAnnotations(new HashSet<>(), allAnnotations, annotationType);
@ -64,7 +82,7 @@ public abstract class RuntimeHintsUtils {
hints.reflection().registerType(annotation, ANNOTATION_HINT); hints.reflection().registerType(annotation, ANNOTATION_HINT);
hints.proxies().registerJdkProxy(annotation, SynthesizedAnnotation.class); hints.proxies().registerJdkProxy(annotation, SynthesizedAnnotation.class);
}); });
if (!allAnnotations.isEmpty()) { if (!allAnnotations.isEmpty() || withProxy) {
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class); hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
} }
} }

View File

@ -50,6 +50,15 @@ class RuntimeHintsUtilsTests {
assertThat(this.hints.proxies().jdkProxies()).isEmpty(); assertThat(this.hints.proxies().jdkProxies()).isEmpty();
} }
@Test
void registerComposableAnnotationType() {
RuntimeHintsUtils.registerComposableAnnotation(this.hints, SampleInvoker.class);
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(SampleInvoker.class));
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(SampleInvoker.class));
}
@Test @Test
void registerAnnotationTypeWithLocalUseOfAliasForRegistersProxy() { void registerAnnotationTypeWithLocalUseOfAliasForRegistersProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, LocalMapping.class); RuntimeHintsUtils.registerAnnotation(this.hints, LocalMapping.class);