Add support of declarative use of reflection

This commit adds a `@Reflective` annotation that can be used to declare
that the annotated element requires reflection at runtime. By default,
the annotated element is exposed but this can be customized by
specifying a dedicated `ReflectiveProcessor`.

Closes gh-28469
This commit is contained in:
Stephane Nicoll
2022-05-18 11:34:32 +02:00
parent 2517c72f7d
commit 4cca190aad
8 changed files with 684 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/*
* 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.annotation;
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.springframework.core.annotation.AliasFor;
/**
* Indicate that the annotated element requires reflection.
*
* <p>When present, either directly or as a meta-annotation, this annotation
* triggers the configured {@linkplain ReflectiveProcessor processors} against
* the annotated element. By default, a reflection hint is added on the
* annotated element so that it can be discovered and invoked if necessary.
*
* @author Stephane Nicoll
* @since 6.0
* @see SimpleReflectiveProcessor
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.CONSTRUCTOR,
ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Reflective {
/**
* Alias for {@link #processors()}.
*/
Class<? extends ReflectiveProcessor>[] value() default SimpleReflectiveProcessor.class;
/**
* {@link ReflectiveProcessor} implementations to invoke against the
* annotated element.
*/
@AliasFor("value")
Class<? extends ReflectiveProcessor>[] processors() default SimpleReflectiveProcessor.class;
}

View File

@@ -0,0 +1,39 @@
/*
* 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.annotation;
import java.lang.reflect.AnnotatedElement;
import org.springframework.aot.hint.ReflectionHints;
/**
* Process an {@link AnnotatedElement} and register the necessary reflection
* hints for it.
*
* @author Stephane Nicoll
* @since 6.0
*/
public interface ReflectiveProcessor {
/**
* Register {@link ReflectionHints} against the specified {@link AnnotatedElement}.
* @param hints the reflection hints instance to use
* @param element the element to process
*/
void registerReflectionHints(ReflectionHints hints, AnnotatedElement element);
}

View File

@@ -0,0 +1,93 @@
/*
* 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.annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import org.springframework.aot.hint.ExecutableHint.Builder;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.ReflectionHints;
/**
* A simple {@link ReflectiveProcessor} implementation that registers only a
* reflection hint for the annotated type. Can be sub-classed to customize
* processing for a given {@link AnnotatedElement} type.
*
* @author Stephane Nicoll
* @since 6.0
*/
public class SimpleReflectiveProcessor implements ReflectiveProcessor {
private static final Consumer<Builder> INVOKE_EXECUTABLE = hint -> hint.setModes(ExecutableMode.INVOKE);
@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
if (element instanceof Class<?> type) {
registerTypeHint(hints, type);
}
else if (element instanceof Constructor<?> constructor) {
registerConstructorHint(hints, constructor);
}
else if (element instanceof Field field) {
registerFieldHint(hints, field);
}
else if (element instanceof Method method) {
registerMethodHint(hints, method);
}
}
/**
* Register {@link ReflectionHints} against the specified {@link Class}.
* @param hints the reflection hints instance to use
* @param type the class to process
*/
protected void registerTypeHint(ReflectionHints hints, Class<?> type) {
hints.registerType(type, hint -> {});
}
/**
* Register {@link ReflectionHints} against the specified {@link Constructor}.
* @param hints the reflection hints instance to use
* @param constructor the constructor to process
*/
protected void registerConstructorHint(ReflectionHints hints, Constructor<?> constructor) {
hints.registerConstructor(constructor, INVOKE_EXECUTABLE);
}
/**
* Register {@link ReflectionHints} against the specified {@link Field}.
* @param hints the reflection hints instance to use
* @param field the field to process
*/
protected void registerFieldHint(ReflectionHints hints, Field field) {
hints.registerField(field);
}
/**
* Register {@link ReflectionHints} against the specified {@link Method}.
* @param hints the reflection hints instance to use
* @param method the method to process
*/
protected void registerMethodHint(ReflectionHints hints, Method method) {
hints.registerMethod(method, INVOKE_EXECUTABLE);
}
}

View File

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

View File

@@ -0,0 +1,115 @@
/*
* 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.annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeReference;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SimpleReflectiveProcessor}.
*
* @author Stephane Nicoll
*/
class SimpleReflectiveProcessorTests {
private final SimpleReflectiveProcessor processor = new SimpleReflectiveProcessor();
private final ReflectionHints hints = new ReflectionHints();
@Test
void registerReflectiveHintsForClass() {
processor.registerReflectionHints(hints, SampleBean.class);
assertThat(hints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleBean.class));
assertThat(typeHint.getMemberCategories()).isEmpty();
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).isEmpty();
});
}
@Test
void registerReflectiveHintsForConstructor() {
Constructor<?> constructor = SampleBean.class.getDeclaredConstructors()[0];
processor.registerReflectionHints(hints, constructor);
assertThat(hints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleBean.class));
assertThat(typeHint.getMemberCategories()).isEmpty();
assertThat(typeHint.constructors()).singleElement().satisfies(constructorHint -> {
assertThat(constructorHint.getName()).isEqualTo("<init>");
assertThat(constructorHint.getModes()).containsExactly(ExecutableMode.INVOKE);
assertThat(constructorHint.getParameterTypes()).containsExactly(TypeReference.of(String.class));
});
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).isEmpty();
});
}
@Test
void registerReflectiveHintsForField() throws NoSuchFieldException {
Field field = SampleBean.class.getDeclaredField("name");
processor.registerReflectionHints(hints, field);
assertThat(hints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleBean.class));
assertThat(typeHint.getMemberCategories()).isEmpty();
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).singleElement().satisfies(fieldHint ->
assertThat(fieldHint.getName()).isEqualTo("name"));
assertThat(typeHint.methods()).isEmpty();
});
}
@Test
void registerReflectiveHintsForMethod() throws NoSuchMethodException {
Method method = SampleBean.class.getDeclaredMethod("setName", String.class);
processor.registerReflectionHints(hints, method);
assertThat(hints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleBean.class));
assertThat(typeHint.getMemberCategories()).isEmpty();
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).singleElement().satisfies(methodHint -> {
assertThat(methodHint.getName()).isEqualTo("setName");
assertThat(methodHint.getModes()).containsExactly(ExecutableMode.INVOKE);
assertThat(methodHint.getParameterTypes()).containsExactly(TypeReference.of(String.class));
});
});
}
static class SampleBean {
private String name;
SampleBean(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}
}