From 5be6b3d2a7ed4f1ad980ae02cb8c3cf0a6a616b4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 14 Apr 2022 10:22:07 +0200 Subject: [PATCH] Add shortcut method to register multiple types hints Closes gh-28339 --- .../springframework/aot/hint/ReflectionHints.java | 13 +++++++++++++ .../aot/hint/ReflectionHintsTests.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+) 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");