Polishing

This commit is contained in:
Sam Brannen 2022-06-13 17:35:50 +02:00
parent c3b29960ed
commit f722dbe5a8
1 changed files with 429 additions and 426 deletions

View File

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