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:
Stephane Nicoll
2022-05-20 14:48:06 +02:00
parent 9181ac70f5
commit 2517c72f7d
5 changed files with 181 additions and 11 deletions

View File

@@ -0,0 +1,62 @@
/*
* 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.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.SynthesizedAnnotation;
/**
* Utility methods for runtime hints support code.
*
* @author Stephane Nicoll
* @since 6.0
*/
public abstract class RuntimeHintsUtils {
/**
* A {@link TypeHint} customizer suitable for an annotation. Make sure
* that its attributes are visible.
*/
public static final Consumer<Builder> ANNOTATION_HINT = hint ->
hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
/**
* Register the necessary hints so that the specified annotation is visible
* at runtime.
* @param hints the {@link RuntimeHints} instance ot use
* @param annotation the annotation
* @see SynthesizedAnnotation
*/
public static void registerAnnotation(RuntimeHints hints, MergedAnnotation<?> annotation) {
registerAnnotation(hints, annotation.getType(),
annotation.synthesize() instanceof SynthesizedAnnotation);
}
private static void registerAnnotation(RuntimeHints hints, Class<?> annotationType, boolean withProxy) {
hints.reflection().registerType(annotationType, ANNOTATION_HINT);
if (withProxy) {
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
}
}
}

View File

@@ -0,0 +1,9 @@
/**
* Convenience classes for using runtime hints.
*/
@NonNullApi
@NonNullFields
package org.springframework.aot.hint.support;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -16,12 +16,9 @@
package org.springframework.core.annotation;
import java.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
/**
* {@link RuntimeHintsRegistrar} for core annotations.
@@ -31,13 +28,10 @@ import org.springframework.aot.hint.TypeHint;
*/
class CoreAnnotationsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {
private static final Consumer<TypeHint.Builder> HINT = builder -> builder.withMembers(
MemberCategory.INVOKE_DECLARED_METHODS);
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(AliasFor.class, HINT);
hints.reflection().registerType(Order.class, HINT);
hints.reflection().registerType(AliasFor.class, RuntimeHintsUtils.ANNOTATION_HINT)
.registerType(Order.class, RuntimeHintsUtils.ANNOTATION_HINT);
}
}

View File

@@ -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;
}
}

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_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));
}
}