Skip to content

Commit

Permalink
Add dedicated hint support for composable annotations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jul 29, 2022
1 parent 1aa4e7c commit 916a871
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,32 @@ public abstract class RuntimeHintsUtils {
* @see SynthesizedAnnotation
*/
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);
Set<Class<?>> allAnnotations = new LinkedHashSet<>();
collectAliasedAnnotations(new HashSet<>(), allAnnotations, annotationType);
allAnnotations.forEach(annotation -> {
hints.reflection().registerType(annotation, ANNOTATION_HINT);
hints.proxies().registerJdkProxy(annotation, SynthesizedAnnotation.class);
});
if (!allAnnotations.isEmpty()) {
if (!allAnnotations.isEmpty() || withProxy) {
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ void registerAnnotationType() {
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
void registerAnnotationTypeWithLocalUseOfAliasForRegistersProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, LocalMapping.class);
Expand Down

0 comments on commit 916a871

Please sign in to comment.