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,428 +51,434 @@ class ReflectionHintsPredicatesTests {
publicConstructor = SampleClass.class.getConstructor(); publicConstructor = SampleClass.class.getConstructor();
} }
@BeforeEach @Nested
void setup() { class ReflectionOnType {
this.runtimeHints = new RuntimeHints();
} @Test
void shouldFailForNullType() {
// Reflection on type assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType((TypeReference) null));
}
@Test
void shouldFailForNullType() { @Test
assertThatThrownBy(() -> reflection.onType((TypeReference) null)).isInstanceOf(IllegalArgumentException.class); void reflectionOnClassShouldMatchIntrospection() {
} runtimeHints.reflection().registerType(SampleClass.class, builder -> {
});
@Test assertPredicateMatches(reflection.onType(SampleClass.class));
void reflectionOnClassShouldMatchIntrospection() { }
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> {
}); @Test
assertPredicateMatches(reflection.onType(SampleClass.class)); void reflectionOnTypeReferenceShouldMatchIntrospection() {
} runtimeHints.reflection().registerType(SampleClass.class, builder -> {
});
@Test assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class)));
void reflectionOnTypeReferenceShouldMatchIntrospection() { }
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> {
}); @Test
assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class))); void reflectionOnDifferentClassShouldNotMatchIntrospection() {
} runtimeHints.reflection().registerType(Integer.class, builder -> {
});
@Test assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class)));
void reflectionOnDifferentClassShouldNotMatchIntrospection() { }
this.runtimeHints.reflection().registerType(Integer.class, builder -> {
}); @Test
assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class))); void typeWithMemberCategoryFailsWithNullCategory() {
} runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() -> reflection.onType(SampleClass.class).withMemberCategory(null));
@Test }
void typeWithMemberCategoryFailsWithNullCategory() {
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); @Test
assertThatThrownBy(() -> reflection.onType(SampleClass.class).withMemberCategory(null)).isInstanceOf(IllegalArgumentException.class); 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 typeWithMemberCategoryMatchesCategory() {
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); @Test
assertPredicateMatches(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); 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 typeWithMemberCategoryDoesNotMatchOtherCategory() {
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); @Test
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class).withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS)); 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 typeWithAnyMemberCategoryFailsWithNullCategories() {
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); @Test
assertThatThrownBy(() -> reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[]{})).isInstanceOf(IllegalArgumentException.class); 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 typeWithAnyMemberCategoryMatchesCategory() {
this.runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); @Test
assertPredicateMatches(reflection.onType(SampleClass.class).withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); 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));
@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));
} @Nested
class ReflectionOnConstructor {
// Reflection on constructor
@Test
@Test void constructorIntrospectionMatchesConstructorHint() {
void constructorIntrospectionMatchesConstructorHint() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
this.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)); runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> {
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
this.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)); runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> {
this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> { }));
})); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); }
}
@Test
@Test void privateConstructorInvocationMatchesConstructorInvocationHint() {
void privateConstructorInvocationMatchesConstructorInvocationHint() { List<TypeReference> parameterTypes = Collections.singletonList(TypeReference.of(String.class));
List<TypeReference> parameterTypes = Collections.singletonList(TypeReference.of(String.class)); runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
this.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() { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
this.runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke());
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<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() {
} }
} }