Browse Source

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
pull/28616/head
Brian Clozel 4 years ago
parent
commit
15b69a3ede
  1. 10
      spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java
  2. 15
      spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java

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

@ -180,6 +180,16 @@ public class ReflectionHintsPredicates { @@ -180,6 +180,16 @@ public class ReflectionHintsPredicates {
return this.and(hints -> getTypeHint(hints).getMemberCategories().contains(memberCategory));
}
/**
* Refine the current predicate to only match if the given {@link MemberCategory categories} are present.
* @param memberCategories the member categories
* @return the refined {@link RuntimeHints} predicate
*/
public Predicate<RuntimeHints> withMemberCategories(MemberCategory... memberCategories) {
Assert.notEmpty(memberCategories, "'memberCategories' should not be empty");
return this.and(hints -> getTypeHint(hints).getMemberCategories().containsAll(Arrays.asList(memberCategories)));
}
/**
* Refine the current predicate to match if any of the given {@link MemberCategory categories} is present.
* @param memberCategories the member categories

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

@ -98,6 +98,21 @@ class ReflectionHintsPredicatesTests { @@ -98,6 +98,21 @@ class ReflectionHintsPredicatesTests {
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
}
@Test
void typeWithMemberCategoriesMatchesCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithMemberCategoriesDoesNotMatchMissingCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithAnyMemberCategoryFailsWithNullCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));

Loading…
Cancel
Save