Migrate hint registration to shortcuts

Migrate code to make use of the `MemberCategory` and `FieldMode`
shortcuts.

See gh-29011
This commit is contained in:
Phillip Webb
2022-09-01 16:30:25 -07:00
parent bc0bf1fac3
commit 505da5c602
17 changed files with 147 additions and 198 deletions

View File

@@ -73,8 +73,7 @@ class ReflectionHintsTests {
@Test
void getTypeUsingTypeReference() {
this.reflectionHints.registerType(String.class,
hint -> hint.withMembers(MemberCategory.DECLARED_FIELDS));
this.reflectionHints.registerType(String.class, MemberCategory.DECLARED_FIELDS);
assertThat(this.reflectionHints.getTypeHint(TypeReference.of(String.class))).satisfies(
typeWithMemberCategories(String.class, MemberCategory.DECLARED_FIELDS));
}
@@ -87,7 +86,7 @@ class ReflectionHintsTests {
@Test
void registerTypeReuseBuilder() {
this.reflectionHints.registerType(TypeReference.of(String.class),
typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
Field field = ReflectionUtils.findField(String.class, "value");
assertThat(field).isNotNull();
this.reflectionHints.registerField(field);
@@ -100,8 +99,7 @@ class ReflectionHintsTests {
@Test
void registerClass() {
this.reflectionHints.registerType(Integer.class,
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.reflectionHints.registerType(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertThat(this.reflectionHints.typeHints()).singleElement().satisfies(
typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
}
@@ -117,7 +115,7 @@ class ReflectionHintsTests {
@Test
void registerTypesApplyTheSameHints() {
this.reflectionHints.registerTypes(TypeReference.listOf(Integer.class, String.class, Double.class),
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
TypeHint.builtWith(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertThat(this.reflectionHints.typeHints())
.anySatisfy(
typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
@@ -180,8 +178,8 @@ class ReflectionHintsTests {
void registerFieldWithCustomizersCannotDowngradeWrite() {
Field field = ReflectionUtils.findField(TestType.class, "field");
assertThat(field).isNotNull();
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.WRITE));
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.READ));
this.reflectionHints.registerField(field, FieldMode.WRITE);
this.reflectionHints.registerField(field, FieldMode.READ);
assertTestTypeFieldHint(fieldHint -> {
assertThat(fieldHint.getName()).isEqualTo("field");
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,8 +33,7 @@ class RuntimeHintsTests {
@Test
void reflectionHintWithClass() {
this.hints.reflection().registerType(String.class,
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
this.hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertThat(this.hints.reflection().typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType().getCanonicalName()).isEqualTo(String.class.getCanonicalName());
assertThat(typeHint.fields()).isEmpty();

View File

@@ -279,8 +279,7 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Override
protected void registerMethodHint(ReflectionHints hints, Method method) {
super.registerMethodHint(hints, method);
hints.registerType(method.getReturnType(), type ->
type.withMembers(MemberCategory.INVOKE_DECLARED_METHODS));
hints.registerType(method.getReturnType(), MemberCategory.INVOKE_DECLARED_METHODS);
}
}

View File

@@ -66,81 +66,77 @@ class ReflectionHintsPredicatesTests {
@Test
void reflectionOnClassShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {});
runtimeHints.reflection().registerType(SampleClass.class);
assertPredicateMatches(reflection.onType(SampleClass.class));
}
@Test
void reflectionOnTypeReferenceShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {});
runtimeHints.reflection().registerType(SampleClass.class);
assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class)));
}
@Test
void reflectionOnDifferentClassShouldNotMatchIntrospection() {
runtimeHints.reflection().registerType(Integer.class, builder -> {});
runtimeHints.reflection().registerType(Integer.class);
assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class)));
}
@Test
void typeWithMemberCategoryFailsWithNullCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
}
@Test
void typeWithMemberCategoriesMatchesCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS,
MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateMatches(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithMemberCategoriesDoesNotMatchMissingCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
@Test
void typeWithAnyMemberCategoryFailsWithNullCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder ->
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS,
MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS));
}
@@ -164,29 +160,25 @@ class ReflectionHintsPredicatesTests {
@Test
void constructorIntrospectionMatchesIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
}
@@ -206,29 +198,25 @@ class ReflectionHintsPredicatesTests {
@Test
void constructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
}
@Test
void constructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
}
@@ -241,29 +229,25 @@ class ReflectionHintsPredicatesTests {
@Test
void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
}
@Test
void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
}
@@ -283,29 +267,25 @@ class ReflectionHintsPredicatesTests {
@Test
void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
}
@Test
void privateConstructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke());
}
@@ -323,25 +303,25 @@ class ReflectionHintsPredicatesTests {
@Test
void methodIntrospectionMatchesIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
}
@@ -361,25 +341,25 @@ class ReflectionHintsPredicatesTests {
@Test
void methodInvocationDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
}
@@ -392,25 +372,25 @@ class ReflectionHintsPredicatesTests {
@Test
void privateMethodIntrospectionDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect());
}
@@ -430,25 +410,25 @@ class ReflectionHintsPredicatesTests {
@Test
void privateMethodInvocationDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, 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));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke());
}
@@ -464,29 +444,27 @@ class ReflectionHintsPredicatesTests {
@Test
void fieldReflectionMatchesFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField"));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
}
@Test
void fieldWriteReflectionDoesNotMatchFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField",
fieldHint -> fieldHint.withMode(FieldMode.WRITE)));
FieldMode.READ));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite());
}
@Test
void fieldUnsafeReflectionDoesNotMatchFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField"));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess());
}
@Test
void fieldWriteReflectionMatchesFieldHintWithWrite() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withField("publicField", fieldHint -> fieldHint.withMode(FieldMode.WRITE)));
typeHint.withField("publicField", FieldMode.WRITE));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
}
@@ -499,32 +477,31 @@ class ReflectionHintsPredicatesTests {
@Test
void fieldReflectionMatchesPublicFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.PUBLIC_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
}
@Test
void fieldReflectionMatchesDeclaredFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.DECLARED_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
}
@Test
void privateFieldReflectionMatchesFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField", fieldHint -> {
}));
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField"));
assertPredicateMatches(reflection.onField(SampleClass.class, "privateField"));
}
@Test
void privateFieldReflectionDoesNotMatchPublicFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.PUBLIC_FIELDS);
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "privateField"));
}
@Test
void privateFieldReflectionMatchesDeclaredFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS));
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.DECLARED_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "privateField"));
}