Add shortcut method to register multiple types hints

Closes gh-28339
This commit is contained in:
Stephane Nicoll 2022-04-14 10:22:07 +02:00
parent 780d07217b
commit 5be6b3d2a7
2 changed files with 28 additions and 0 deletions

View File

@ -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<TypeReference> types, Consumer<TypeHint.Builder> 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

View File

@ -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");