Register annotation based on its type

This commit improves registerAnnotation to use the annotation type
rather than a `MergedAnnotation` attribute.

See gh-28497
This commit is contained in:
Stephane Nicoll
2022-05-25 10:25:53 +02:00
parent a60e9d6695
commit 059b66bf26
6 changed files with 56 additions and 41 deletions

View File

@@ -32,7 +32,6 @@ import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.SynthesizedAnnotation;
import static org.assertj.core.api.Assertions.assertThat;
@@ -47,36 +46,34 @@ class RuntimeHintsUtilsTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void registerAnnotation() {
RuntimeHintsUtils.registerAnnotation(this.hints, MergedAnnotations
.from(SampleInvokerClass.class).get(SampleInvoker.class));
void registerAnnotationType() {
RuntimeHintsUtils.registerAnnotation(this.hints, SampleInvoker.class);
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(SampleInvoker.class));
assertThat(this.hints.proxies().jdkProxies()).isEmpty();
}
@Test
void registerAnnotationProxyRegistersJdkProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, MergedAnnotations
.from(RetryInvokerClass.class).get(RetryInvoker.class));
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(RetryInvoker.class));
void registerAnnotationTypeProxyRegistersJdkProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, RetryInvoker.class);
assertThat(this.hints.reflection().typeHints())
.anySatisfy(annotationHint(RetryInvoker.class))
.anySatisfy(annotationHint(SampleInvoker.class));
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(RetryInvoker.class));
}
@Test
void registerAnnotationWhereUsedAsAMetaAnnotationRegistersHierarchy() {
RuntimeHintsUtils.registerAnnotation(this.hints, MergedAnnotations
.from(RetryWithEnabledFlagInvokerClass.class).get(SampleInvoker.class));
void registerAnnotationTypeWhereUsedAsAMetaAnnotationRegistersHierarchy() {
RuntimeHintsUtils.registerAnnotation(this.hints, RetryWithEnabledFlagInvoker.class);
ReflectionHints reflection = this.hints.reflection();
assertThat(reflection.typeHints())
.anySatisfy(annotationHint(SampleInvoker.class))
.anySatisfy(annotationHint(RetryInvoker.class))
.anySatisfy(annotationHint(RetryWithEnabledFlagInvoker.class))
.anySatisfy(annotationHint(RetryInvoker.class))
.anySatisfy(annotationHint(SampleInvoker.class))
.hasSize(3);
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(SampleInvoker.class));
.satisfies(annotationProxy(RetryWithEnabledFlagInvoker.class));
}
private Consumer<TypeHint> annotationHint(Class<?> type) {
@@ -85,7 +82,7 @@ class RuntimeHintsUtilsTests {
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).isEmpty();
assertThat(typeHint.getMemberCategories()).containsOnly(MemberCategory.INVOKE_PUBLIC_METHODS);
assertThat(typeHint.getMemberCategories()).containsOnly(MemberCategory.INVOKE_DECLARED_METHODS);
};
}

View File

@@ -49,14 +49,14 @@ class CoreAnnotationsRuntimeHintsRegistrarTests {
void aliasForHasHints() {
assertThat(this.hints.reflection().getTypeHint(TypeReference.of(AliasFor.class)))
.satisfies(hint -> assertThat(hint.getMemberCategories())
.containsExactly(MemberCategory.INVOKE_PUBLIC_METHODS));
.containsExactly(MemberCategory.INVOKE_DECLARED_METHODS));
}
@Test
void orderAnnotationHasHints() {
assertThat(this.hints.reflection().getTypeHint(TypeReference.of(Order.class)))
.satisfies(hint -> assertThat(hint.getMemberCategories())
.containsExactly(MemberCategory.INVOKE_PUBLIC_METHODS));
.containsExactly(MemberCategory.INVOKE_DECLARED_METHODS));
}
}