Align RuntimeHintsPredicates with new FieldMode

Closes gh-29063
This commit is contained in:
Brian Clozel
2022-09-02 18:15:50 +02:00
parent 4368e2563f
commit 7ace1f9dc5
6 changed files with 33 additions and 10 deletions

View File

@@ -44,7 +44,7 @@ public enum FieldMode {
* @param other the other mode to check
* @return {@code true} if this mode includes the other mode
*/
boolean includes(@Nullable FieldMode other) {
public boolean includes(@Nullable FieldMode other) {
return (other == null || this.ordinal() >= other.ordinal());
}

View File

@@ -352,7 +352,7 @@ public class ReflectionHintsPredicates {
private final Field field;
private boolean allowWrite;
private FieldMode mode = FieldMode.READ;
private boolean allowUnsafeAccess;
@@ -364,12 +364,35 @@ public class ReflectionHintsPredicates {
* Refine the current predicate to match if write access is allowed on the field.
* @return the refined {@link RuntimeHints} predicate
* @see FieldHint#isAllowWrite()
* @deprecated in favor of {@link #withReadMode()} or {@link #withWriteMode()}
*/
@Deprecated
public FieldHintPredicate allowWrite() {
this.allowWrite = true;
this.mode = FieldMode.WRITE;
return this;
}
/**
* Refine the current predicate to match if read access is allowed on the field.
* @return the refined {@link RuntimeHints} predicate
* @see FieldHint#getMode()
*/
public FieldHintPredicate withReadMode() {
// FieldMode.READ is already the default and should not override a writeMode() call.
return this;
}
/**
* Refine the current predicate to match if write access is allowed on the field.
* @return the refined {@link RuntimeHints} predicate
* @see FieldHint#getMode()
*/
public FieldHintPredicate withWriteMode() {
this.mode = FieldMode.WRITE;
return this;
}
/**
* Refine the current predicate to match if unsafe access is allowed on the field.
* @return the refined {@link RuntimeHints} predicate
@@ -402,7 +425,7 @@ public class ReflectionHintsPredicates {
private boolean exactMatch(TypeHint typeHint) {
return typeHint.fields().anyMatch(fieldHint ->
this.field.getName().equals(fieldHint.getName())
&& (!this.allowWrite || fieldHint.getMode() == FieldMode.WRITE)
&& (fieldHint.getMode().includes(this.mode))
&& (!this.allowUnsafeAccess || this.allowUnsafeAccess == fieldHint.isAllowUnsafeAccess()));
}
}

View File

@@ -452,7 +452,7 @@ class ReflectionHintsPredicatesTests {
void fieldWriteReflectionDoesNotMatchFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField",
FieldMode.READ));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite());
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").withWriteMode());
}
@Test
@@ -465,7 +465,7 @@ class ReflectionHintsPredicatesTests {
void fieldWriteReflectionMatchesFieldHintWithWrite() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withField("publicField", FieldMode.WRITE));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").withWriteMode());
}
@Test