diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java index 1f7be98f33..a2d51bd27c 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHints.java @@ -95,6 +95,19 @@ public class ReflectionHints { return registerType(TypeReference.of(type), typeHint); } + /** + * Register or customize reflection hints for the types defined by the + * specified list of {@link TypeReference type references}. The specified + * {@code typeHint} consumer is invoked for each type. + * @param types the types to customize + * @param typeHint a builder to further customize hints for each type + * @return {@code this}, to facilitate method chaining + */ + public ReflectionHints registerTypes(Iterable types, Consumer typeHint) { + types.forEach(type -> registerType(type, typeHint)); + return this; + } + /** * Register the need for reflection on the specified {@link Field}. * @param field the field that requires reflection diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java index 8d96f72190..7288e21fde 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsTests.java @@ -19,6 +19,7 @@ package org.springframework.aot.hint; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.function.Consumer; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -87,6 +88,20 @@ class ReflectionHintsTests { typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); } + @Test + void registerTypesApplyTheSameHints() { + this.reflectionHints.registerTypes(Stream.of(Integer.class, String.class, Double.class) + .map(TypeReference::of).toList(), hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); + assertThat(this.reflectionHints.typeHints()) + .anySatisfy( + typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)) + .anySatisfy( + typeWithMemberCategories(String.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)) + .anySatisfy( + typeWithMemberCategories(Double.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)) + .hasSize(3); + } + @Test void registerField() { Field field = ReflectionUtils.findField(TestType.class, "field");