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