Rationalize hints required for annotations

This commit updates RuntimeHintsUtils to focus on registering a JDK
proxy only as annotations of annotated elements that have at least
an introspection hints are visible out-of-the-box.

This commit also removes unnecessary hints and adapt `@Reflective` to
detect if a hint is required using the introduced
MergedAnnotation#isSynthesizable.

See gh-28967
This commit is contained in:
Stephane Nicoll
2022-08-16 14:15:02 +02:00
parent 32346b8382
commit 4f0c879778
17 changed files with 101 additions and 398 deletions

View File

@@ -32,6 +32,9 @@ import org.springframework.core.annotation.AliasFor;
* the annotated element. By default, a reflection hint is added on the
* annotated element so that it can be discovered and invoked if necessary.
*
* <p>A reflection hint is also added if necessary on the annotation that
* <em>directly</em> uses this annotation.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.0

View File

@@ -1,41 +0,0 @@
/*
* 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.stream.Stream;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.Order;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} for core annotations.
*
* @author Phillip Webb
* @since 6.0
*/
class CoreAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of(AliasFor.class, Order.class).forEach(annotationType ->
RuntimeHintsUtils.registerAnnotation(hints, annotationType));
}
}

View File

@@ -16,19 +16,9 @@
package org.springframework.aot.hint.support;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
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.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.SynthesizedAnnotation;
/**
@@ -40,72 +30,48 @@ import org.springframework.core.annotation.SynthesizedAnnotation;
*/
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_DECLARED_METHODS);
/**
* Register the necessary hints so that the specified annotation is visible
* at runtime.
* <p>If an annotation attribute aliases an attribute of another annotation,
* the other annotation is registered as well and a JDK proxy hint is defined
* so that the synthesized annotation can be resolved.
* @param hints the {@link RuntimeHints} instance to use
* @param annotationType the annotation type
* @see SynthesizedAnnotation
* @deprecated as annotation attributes are visible without additional hints
*/
@Deprecated
public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) {
registerSynthesizedAnnotation(hints, annotationType);
}
/**
* Register the necessary hints so that the specified annotation can be
* synthesized at runtime if necessary. Such hints are usually required
* if any of the following apply:
* <ul>
* <li>Use {@link AliasFor} for local aliases</li>
* <li>Has a meta-annotation that uses {@link AliasFor} for attribute overrides</li>
* <li>Has nested annotations or arrays of annotations that are synthesizable</li>
* </ul>
* Consider using {@link #registerAnnotationIfNecessary(RuntimeHints, MergedAnnotation)}
* that determines if the hints are required.
* @param hints the {@link RuntimeHints} instance to use
* @param annotationType the annotation type
* @see SynthesizedAnnotation
*/
public static void registerAnnotation(RuntimeHints hints, Class<?> annotationType) {
registerAnnotation(hints, annotationType, false);
public static void registerSynthesizedAnnotation(RuntimeHints hints, Class<?> annotationType) {
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
}
/**
* 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.
* Determine if the specified annotation can be synthesized at runtime, and
* register the necessary hints accordingly.
* @param hints the {@link RuntimeHints} instance to use
* @param annotationType the composable annotation type
* @see #registerAnnotation(RuntimeHints, Class)
* @param annotation the annotation
* @see #registerSynthesizedAnnotation(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() || withProxy) {
hints.proxies().registerJdkProxy(annotationType, SynthesizedAnnotation.class);
}
}
private static void collectAliasedAnnotations(Set<Class<?>> seen, Set<Class<?>> types, Class<?> annotationType) {
if (seen.contains(annotationType) || AliasFor.class.equals(annotationType) ||
Reflective.class.equals(annotationType)) {
return;
}
seen.add(annotationType);
for (Method method : annotationType.getDeclaredMethods()) {
AliasFor aliasFor = method.getAnnotation(AliasFor.class);
if (aliasFor != null) {
Class<?> annotationAttribute = aliasFor.annotation();
Class<?> targetAnnotation = (annotationAttribute != Annotation.class
? annotationAttribute : annotationType);
if (types.add(targetAnnotation)) {
if (!targetAnnotation.equals(annotationType)) {
collectAliasedAnnotations(seen, types, targetAnnotation);
}
}
}
public static void registerAnnotationIfNecessary(RuntimeHints hints, MergedAnnotation<?> annotation) {
if (annotation.isSynthesizable()) {
registerSynthesizedAnnotation(hints, annotation.getType());
}
}

View File

@@ -1,3 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.aot.hint.support.CoreAnnotationsRuntimeHints,\
org.springframework.aot.hint.support.SpringFactoriesLoaderRuntimeHints

View File

@@ -1,61 +0,0 @@
/*
* 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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CoreAnnotationsRuntimeHints}.
*
* @author Phillip Webb
*/
class CoreAnnotationsRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
@BeforeEach
void setup() {
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
}
@Test
void aliasForHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(AliasFor.class)
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
}
@Test
void orderAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(Order.class)
.withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
}
}

View File

@@ -23,11 +23,11 @@ import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.JdkProxyHint;
import org.springframework.aot.hint.MemberCategory;
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.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.SynthesizedAnnotation;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,75 +43,45 @@ class RuntimeHintsUtilsTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
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 registerComposableAnnotationType() {
RuntimeHintsUtils.registerComposableAnnotation(this.hints, SampleInvoker.class);
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(SampleInvoker.class));
void registerSynthesizedAnnotation() {
RuntimeHintsUtils.registerSynthesizedAnnotation(this.hints, SampleInvoker.class);
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(SampleInvoker.class));
}
@Test
void registerAnnotationTypeWithLocalUseOfAliasForRegistersProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, LocalMapping.class);
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(LocalMapping.class));
void registerAnnotationIfNecessaryWithNonSynthesizedAnnotation() throws NoSuchFieldException {
MergedAnnotation<SampleInvoker> annotation = MergedAnnotations
.from(TestBean.class.getField("sampleInvoker")).get(SampleInvoker.class);
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation);
assertThat(this.hints.proxies().jdkProxies()).isEmpty();
}
@Test
void registerAnnotationIfNecessaryWithLocalAliases() throws NoSuchFieldException {
MergedAnnotation<LocalMapping> annotation = MergedAnnotations
.from(TestBean.class.getField("localMapping")).get(LocalMapping.class);
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation);
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(LocalMapping.class));
}
@Test
void registerAnnotationTypeProxyRegistersJdkProxies() {
RuntimeHintsUtils.registerAnnotation(this.hints, RetryInvoker.class);
assertThat(this.hints.reflection().typeHints())
.anySatisfy(annotationHint(RetryInvoker.class))
.anySatisfy(annotationHint(SampleInvoker.class))
.hasSize(2);
assertThat(this.hints.proxies().jdkProxies())
.anySatisfy(annotationProxy(RetryInvoker.class))
.anySatisfy(annotationProxy(SampleInvoker.class))
.hasSize(2);
}
@Test // gh-28953
void registerAnnotationForAliasForShouldNotRegisterSynthesizedAnnotationProxy() {
RuntimeHintsUtils.registerAnnotation(this.hints, AliasFor.class);
assertThat(this.hints.reflection().typeHints()).singleElement()
.satisfies(annotationHint(AliasFor.class));
assertThat(this.hints.proxies().jdkProxies()).isEmpty();
void registerAnnotationIfNecessaryWithMetaAttributeOverride() throws NoSuchFieldException {
MergedAnnotation<SampleInvoker> annotation = MergedAnnotations
.from(TestBean.class.getField("retryInvoker")).get(SampleInvoker.class);
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation);
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(SampleInvoker.class));
}
@Test
void registerAnnotationTypeWhereUsedAsAMetaAnnotationRegistersHierarchy() {
RuntimeHintsUtils.registerAnnotation(this.hints, RetryWithEnabledFlagInvoker.class);
assertThat(this.hints.reflection().typeHints())
.anySatisfy(annotationHint(RetryWithEnabledFlagInvoker.class))
.anySatisfy(annotationHint(RetryInvoker.class))
.anySatisfy(annotationHint(SampleInvoker.class))
.hasSize(3);
assertThat(this.hints.proxies().jdkProxies())
.anySatisfy(annotationProxy(RetryWithEnabledFlagInvoker.class))
.anySatisfy(annotationProxy(RetryInvoker.class))
.anySatisfy(annotationProxy(SampleInvoker.class))
.hasSize(3);
}
private Consumer<TypeHint> annotationHint(Class<?> type) {
return typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(type));
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).isEmpty();
assertThat(typeHint.getMemberCategories()).containsOnly(MemberCategory.INVOKE_DECLARED_METHODS);
};
void registerAnnotationIfNecessaryWithSynthesizedAttribute() throws NoSuchFieldException {
MergedAnnotation<RetryContainer> annotation = MergedAnnotations
.from(TestBean.class.getField("retryContainer")).get(RetryContainer.class);
RuntimeHintsUtils.registerAnnotationIfNecessary(this.hints, annotation);
assertThat(this.hints.proxies().jdkProxies()).singleElement()
.satisfies(annotationProxy(RetryContainer.class));
}
private Consumer<JdkProxyHint> annotationProxy(Class<?> type) {
@@ -120,6 +90,22 @@ class RuntimeHintsUtilsTests {
}
static class TestBean {
@SampleInvoker
public String sampleInvoker;
@LocalMapping
public String localMapping;
@RetryInvoker
public String retryInvoker;
@RetryContainer(retry = @RetryInvoker(3))
public String retryContainer;
}
@Retention(RetentionPolicy.RUNTIME)
@interface LocalMapping {
@@ -149,13 +135,9 @@ class RuntimeHintsUtilsTests {
}
@Retention(RetentionPolicy.RUNTIME)
@RetryInvoker
@interface RetryWithEnabledFlagInvoker {
@interface RetryContainer {
@AliasFor(annotation = RetryInvoker.class)
int value() default 5;
boolean enabled() default true;
RetryInvoker retry();
}