Skip to content

Commit 15b69a3

Browse files
committed
Polish RuntimeHintsPredicates
This commit adds a method variant checking that all `MemberCategory` are supported by the `RuntimeHints` for a given type hint. See gh-28555
1 parent dc3ec5b commit 15b69a3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ public Predicate<RuntimeHints> withMemberCategory(MemberCategory memberCategory)
180180
return this.and(hints -> getTypeHint(hints).getMemberCategories().contains(memberCategory));
181181
}
182182

183+
/**
184+
* Refine the current predicate to only match if the given {@link MemberCategory categories} are present.
185+
* @param memberCategories the member categories
186+
* @return the refined {@link RuntimeHints} predicate
187+
*/
188+
public Predicate<RuntimeHints> withMemberCategories(MemberCategory... memberCategories) {
189+
Assert.notEmpty(memberCategories, "'memberCategories' should not be empty");
190+
return this.and(hints -> getTypeHint(hints).getMemberCategories().containsAll(Arrays.asList(memberCategories)));
191+
}
192+
183193
/**
184194
* Refine the current predicate to match if any of the given {@link MemberCategory categories} is present.
185195
* @param memberCategories the member categories

spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ void typeWithMemberCategoryDoesNotMatchOtherCategory() {
9898
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
9999
}
100100

101+
@Test
102+
void typeWithMemberCategoriesMatchesCategories() {
103+
runtimeHints.reflection().registerType(SampleClass.class, builder ->
104+
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
105+
assertPredicateMatches(reflection.onType(SampleClass.class)
106+
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
107+
}
108+
109+
@Test
110+
void typeWithMemberCategoriesDoesNotMatchMissingCategory() {
111+
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
112+
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
113+
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
114+
}
115+
101116
@Test
102117
void typeWithAnyMemberCategoryFailsWithNullCategories() {
103118
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));

0 commit comments

Comments
 (0)