From f722dbe5a85d6135b3a3f19d9bcb137052c62ee2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 13 Jun 2022 17:35:50 +0200 Subject: [PATCH] Polishing --- .../hint/ReflectionHintsPredicatesTests.java | 855 +++++++++--------- 1 file changed, 429 insertions(+), 426 deletions(-) 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 9550455220..415685a1c1 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 @@ -23,11 +23,11 @@ import java.util.List; import java.util.function.Predicate; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Tests for {@link ReflectionHintsPredicates} @@ -42,7 +42,7 @@ class ReflectionHintsPredicatesTests { private final ReflectionHintsPredicates reflection = new ReflectionHintsPredicates(); - private RuntimeHints runtimeHints; + private final RuntimeHints runtimeHints = new RuntimeHints(); @BeforeAll @@ -51,428 +51,434 @@ class ReflectionHintsPredicatesTests { publicConstructor = SampleClass.class.getConstructor(); } - @BeforeEach - void setup() { - this.runtimeHints = new RuntimeHints(); - } - - // Reflection on type - - @Test - void shouldFailForNullType() { - assertThatThrownBy(() -> reflection.onType((TypeReference) null)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - void reflectionOnClassShouldMatchIntrospection() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> { - }); - assertPredicateMatches(reflection.onType(SampleClass.class)); - } - - @Test - void reflectionOnTypeReferenceShouldMatchIntrospection() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> { - }); - assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class))); - } - - @Test - void reflectionOnDifferentClassShouldNotMatchIntrospection() { - this.runtimeHints.reflection().registerType(Integer.class, builder -> { - }); - assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class))); - } - - @Test - void typeWithMemberCategoryFailsWithNullCategory() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertThatThrownBy(() -> reflection.onType(SampleClass.class).withMemberCategory(null)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - void typeWithMemberCategoryMatchesCategory() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateMatches(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - } - - @Test - void typeWithMemberCategoryDoesNotMatchOtherCategory() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS)); - } - - @Test - void typeWithAnyMemberCategoryFailsWithNullCategories() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertThatThrownBy(() -> reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[]{})).isInstanceOf(IllegalArgumentException.class); - } - - @Test - void typeWithAnyMemberCategoryMatchesCategory() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateMatches(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - } - - @Test - void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() { - this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)); - } - - // Reflection on constructor - - @Test - void constructorIntrospectionMatchesConstructorHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> { - })); - assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); - } - - @Test - void constructorIntrospectionMatchesIntrospectPublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); - } - - @Test - void constructorIntrospectionMatchesInvokePublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); - } - - @Test - void constructorIntrospectionMatchesIntrospectDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); - } - - @Test - void constructorIntrospectionMatchesInvokeDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); - } - - @Test - void constructorInvocationDoesNotMatchConstructorHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> { - })); - assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void constructorInvocationMatchesConstructorInvocationHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE))); - assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void constructorInvocationDoesNotMatchIntrospectPublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void constructorInvocationMatchesInvokePublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void constructorInvocationMatchesInvokeDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); - } - - @Test - void privateConstructorIntrospectionMatchesConstructorHint() { - List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> { - })); - assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); - } - - @Test - void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); - } - - @Test - void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); - } - - @Test - void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); - } - - @Test - void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); - } - - @Test - void privateConstructorInvocationDoesNotMatchConstructorHint() { - List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> { - })); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); - } - - @Test - void privateConstructorInvocationMatchesConstructorInvocationHint() { - List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE))); - assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke()); - } - - @Test - void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); - } - - @Test - void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); - } - - @Test - void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); - assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); - } - - @Test - void privateConstructorInvocationMatchesInvokeDeclaredConstructors() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); - assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke()); - } + @Nested + class ReflectionOnType { + + @Test + void shouldFailForNullType() { + assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType((TypeReference) null)); + } + + @Test + void reflectionOnClassShouldMatchIntrospection() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> { + }); + assertPredicateMatches(reflection.onType(SampleClass.class)); + } + + @Test + void reflectionOnTypeReferenceShouldMatchIntrospection() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> { + }); + assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class))); + } + + @Test + void reflectionOnDifferentClassShouldNotMatchIntrospection() { + runtimeHints.reflection().registerType(Integer.class, builder -> { + }); + assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class))); + } + + @Test + void typeWithMemberCategoryFailsWithNullCategory() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType(SampleClass.class).withMemberCategory(null)); + } + + @Test + void typeWithMemberCategoryMatchesCategory() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateMatches(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + } + + @Test + void typeWithMemberCategoryDoesNotMatchOtherCategory() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS)); + } + + @Test + void typeWithAnyMemberCategoryFailsWithNullCategories() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[0])); + } + + @Test + void typeWithAnyMemberCategoryMatchesCategory() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateMatches(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + } + + @Test + void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() { + runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)); + } + + } + + @Nested + class ReflectionOnConstructor { + + @Test + void constructorIntrospectionMatchesConstructorHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> { + })); + assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); + } + + @Test + void constructorIntrospectionMatchesIntrospectPublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); + } + + @Test + void constructorIntrospectionMatchesInvokePublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); + } + + @Test + void constructorIntrospectionMatchesIntrospectDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); + } + + @Test + void constructorIntrospectionMatchesInvokeDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); + } + + @Test + void constructorInvocationDoesNotMatchConstructorHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> { + })); + assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void constructorInvocationMatchesConstructorInvocationHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE))); + assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void constructorInvocationDoesNotMatchIntrospectPublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void constructorInvocationMatchesInvokePublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void constructorInvocationMatchesInvokeDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); + } + + @Test + void privateConstructorIntrospectionMatchesConstructorHint() { + List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> { + })); + assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); + } + + @Test + void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); + } + + @Test + void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); + } + + @Test + void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); + } + + @Test + void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); + } + + @Test + void privateConstructorInvocationDoesNotMatchConstructorHint() { + List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> { + })); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); + } + + @Test + void privateConstructorInvocationMatchesConstructorInvocationHint() { + List parameterTypes = Collections.singletonList(TypeReference.of(String.class)); + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE))); + assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke()); + } + + @Test + void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); + } + + @Test + void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); + } + + @Test + void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); + assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); + } + + @Test + void privateConstructorInvocationMatchesInvokeDeclaredConstructors() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); + assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke()); + } + + } + + @Nested + class ReflectionOnMethod { + + @Test + void methodIntrospectionMatchesMethodHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> { + })); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); + } + + @Test + void methodIntrospectionMatchesIntrospectPublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); + } + + @Test + void methodIntrospectionMatchesInvokePublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); + } + + @Test + void methodIntrospectionMatchesIntrospectDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); + } + + @Test + void methodIntrospectionMatchesInvokeDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); + } + + @Test + void methodInvocationDoesNotMatchMethodHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> { + })); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void methodInvocationMatchesMethodInvocationHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE))); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void methodInvocationDoesNotMatchIntrospectPublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void methodInvocationMatchesInvokePublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void methodInvocationDoesNotMatchIntrospectDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void methodInvocationMatchesInvokeDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); + } + + @Test + void privateMethodIntrospectionMatchesMethodHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> { + })); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); + } + + @Test + void privateMethodIntrospectionDoesNotMatchIntrospectPublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); + } + + @Test + void privateMethodIntrospectionDoesNotMatchInvokePublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); + } + + @Test + void privateMethodIntrospectionMatchesIntrospectDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); + } + + @Test + void privateMethodIntrospectionMatchesInvokeDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); + } + + @Test + void privateMethodInvocationDoesNotMatchMethodHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> { + })); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + @Test + void privateMethodInvocationMatchesMethodInvocationHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE))); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + @Test + void privateMethodInvocationDoesNotMatchIntrospectPublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + @Test + void privateMethodInvocationDoesNotMatchInvokePublicMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + @Test + void privateMethodInvocationDoesNotMatchIntrospectDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); + assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + @Test + void privateMethodInvocationMatchesInvokeDeclaredMethods() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); + assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); + } + + } + + @Nested + class ReflectionOnField { + + @Test + void shouldFailForMissingField() { + assertThatIllegalArgumentException().isThrownBy(() -> reflection.onField(SampleClass.class, "missingField")); + } + + @Test + void fieldReflectionMatchesFieldHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { + })); + assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); + } + + @Test + void fieldWriteReflectionDoesNotMatchFieldHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { + })); + assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite()); + } + + @Test + void fieldUnsafeReflectionDoesNotMatchFieldHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { + })); + assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess()); + } + + @Test + void fieldWriteReflectionMatchesFieldHintWithWrite() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> + typeHint.withField("publicField", fieldHint -> fieldHint.allowWrite(true))); + assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite()); + } + + @Test + void fieldUnsafeReflectionMatchesFieldHintWithUnsafe() { + runtimeHints.reflection().registerType(SampleClass.class, + typeHint -> typeHint.withField("publicField", fieldHint -> fieldHint.allowUnsafeAccess(true))); + assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess()); + } + + @Test + void fieldReflectionMatchesPublicFieldsHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); + assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); + } + + @Test + void fieldReflectionMatchesDeclaredFieldsHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); + assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); + } + + @Test + void privateFieldReflectionMatchesFieldHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField", fieldHint -> { + })); + assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); + } + + @Test + void privateFieldReflectionDoesNotMatchPublicFieldsHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); + assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "privateField")); + } + + @Test + void privateFieldReflectionMatchesDeclaredFieldsHint() { + runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); + assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); + } - // Reflection on method - - @Test - void methodIntrospectionMatchesMethodHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> { - })); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); - } - - @Test - void methodIntrospectionMatchesIntrospectPublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); - } - - @Test - void methodIntrospectionMatchesInvokePublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); - } - - @Test - void methodIntrospectionMatchesIntrospectDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); - } - - @Test - void methodIntrospectionMatchesInvokeDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); - } - - @Test - void methodInvocationDoesNotMatchMethodHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> { - })); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); - } - - @Test - void methodInvocationMatchesMethodInvocationHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("publicMethod", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE))); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); - } - - @Test - void methodInvocationDoesNotMatchIntrospectPublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); - } - - @Test - void methodInvocationMatchesInvokePublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); } - @Test - void methodInvocationDoesNotMatchIntrospectDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); - } - - @Test - void methodInvocationMatchesInvokeDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); - } - - @Test - void privateMethodIntrospectionMatchesMethodHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> { - })); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); - } - - @Test - void privateMethodIntrospectionDoesNotMatchIntrospectPublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); - } - - @Test - void privateMethodIntrospectionDoesNotMatchInvokePublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); - } - - @Test - void privateMethodIntrospectionMatchesIntrospectDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); - } - - @Test - void privateMethodIntrospectionMatchesInvokeDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); - } - - @Test - void privateMethodInvocationDoesNotMatchMethodHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> { - })); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - @Test - void privateMethodInvocationMatchesMethodInvocationHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMethod("privateMethod", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE))); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - @Test - void privateMethodInvocationDoesNotMatchIntrospectPublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - @Test - void privateMethodInvocationDoesNotMatchInvokePublicMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - @Test - void privateMethodInvocationDoesNotMatchIntrospectDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); - assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - @Test - void privateMethodInvocationMatchesInvokeDeclaredMethods() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); - assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); - } - - // Reflection on field - - @Test - void shouldFailForMissingField() { - assertThatThrownBy(() -> reflection.onField(SampleClass.class, "missingField")).isInstanceOf(IllegalArgumentException.class); - } - - @Test - void fieldReflectionMatchesFieldHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { - })); - assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); - } - - @Test - void fieldWriteReflectionDoesNotMatchFieldHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { - })); - assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite()); - } - - @Test - void fieldUnsafeReflectionDoesNotMatchFieldHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { - })); - assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess()); - } - - @Test - void fieldWriteReflectionMatchesFieldHintWithWrite() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> - typeHint.withField("publicField", fieldHint -> fieldHint.allowWrite(true))); - assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite()); - } - - @Test - void fieldUnsafeReflectionMatchesFieldHintWithUnsafe() { - this.runtimeHints.reflection().registerType(SampleClass.class, - typeHint -> typeHint.withField("publicField", fieldHint -> fieldHint.allowUnsafeAccess(true))); - assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess()); - } - - @Test - void fieldReflectionMatchesPublicFieldsHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); - assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); - } - - @Test - void fieldReflectionMatchesDeclaredFieldsHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); - assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); - } - - @Test - void privateFieldReflectionMatchesFieldHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField", fieldHint -> { - })); - assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); - } - - @Test - void privateFieldReflectionDoesNotMatchPublicFieldsHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); - assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "privateField")); - } - - @Test - void privateFieldReflectionMatchesDeclaredFieldsHint() { - this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); - assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); - } - - private void assertPredicateMatches(Predicate predicate) { assertThat(predicate).accepts(this.runtimeHints); } @@ -482,6 +488,7 @@ class ReflectionHintsPredicatesTests { } + @SuppressWarnings("unused") static class SampleClass { private String privateField; @@ -489,19 +496,15 @@ class ReflectionHintsPredicatesTests { public String publicField; public SampleClass() { - } private SampleClass(String message) { - } public void publicMethod() { - } private void privateMethod() { - } }