From 15b69a3edec7a7f1dc166e3f07fa4b1cb54b0df9 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 13 Jun 2022 18:27:45 +0200 Subject: [PATCH] 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 --- .../aot/hint/ReflectionHintsPredicates.java | 10 ++++++++++ .../aot/hint/ReflectionHintsPredicatesTests.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java index 1f88ea15029..107d1900c31 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java @@ -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 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 diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java index 415685a1c19..6953a325928 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java @@ -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));