Add a common utility to register hints for an annotation
This commit adds a utility that takes care of registering the necessary hints to make an annotation visible at runtime. The core framework may create a JDK proxy if necessary, which requires specific handling. Closes gh-28497
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aot.hint.support;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Tests for {@link RuntimeHintsUtils}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class RuntimeHintsUtilsTests {
|
||||
|
||||
private final RuntimeHints hints = new RuntimeHints();
|
||||
|
||||
@Test
|
||||
void registerAnnotation() {
|
||||
RuntimeHintsUtils.registerAnnotation(this.hints, MergedAnnotations
|
||||
.from(SampleInvokerClass.class).get(SampleInvoker.class));
|
||||
assertThat(this.hints.reflection().getTypeHint(SampleInvoker.class)).satisfies(typeHint -> {
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
assertThat(typeHint.getMemberCategories()).containsOnly(MemberCategory.INVOKE_PUBLIC_METHODS);
|
||||
});
|
||||
assertThat(this.hints.proxies().jdkProxies()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerAnnotationProxyRegistersJdkProxy() {
|
||||
RuntimeHintsUtils.registerAnnotation(this.hints, MergedAnnotations
|
||||
.from(RetryInvokerClass.class).get(RetryInvoker.class));
|
||||
assertThat(this.hints.reflection().getTypeHint(RetryInvoker.class)).satisfies(typeHint -> {
|
||||
assertThat(typeHint.constructors()).isEmpty();
|
||||
assertThat(typeHint.fields()).isEmpty();
|
||||
assertThat(typeHint.methods()).isEmpty();
|
||||
assertThat(typeHint.getMemberCategories()).containsOnly(MemberCategory.INVOKE_PUBLIC_METHODS);
|
||||
});
|
||||
assertThat(this.hints.proxies().jdkProxies()).anySatisfy(jdkProxyHint ->
|
||||
assertThat(jdkProxyHint.getProxiedInterfaces()).containsExactly(
|
||||
TypeReference.of(RetryInvoker.class), TypeReference.of(SynthesizedAnnotation.class)));
|
||||
}
|
||||
|
||||
|
||||
@SampleInvoker
|
||||
static class SampleInvokerClass {
|
||||
|
||||
}
|
||||
|
||||
@RetryInvoker
|
||||
static class RetryInvokerClass {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@interface SampleInvoker {
|
||||
|
||||
int retries() default 0;
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@SampleInvoker
|
||||
@interface RetryInvoker {
|
||||
|
||||
@AliasFor(attribute = "retries", annotation = SampleInvoker.class)
|
||||
int value() default 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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_DECLARED_METHODS));
|
||||
.containsExactly(MemberCategory.INVOKE_PUBLIC_METHODS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void orderAnnotationHasHints() {
|
||||
assertThat(this.hints.reflection().getTypeHint(TypeReference.of(Order.class)))
|
||||
.satisfies(hint -> assertThat(hint.getMemberCategories())
|
||||
.containsExactly(MemberCategory.INVOKE_DECLARED_METHODS));
|
||||
.containsExactly(MemberCategory.INVOKE_PUBLIC_METHODS));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user