Align RuntimeHintsPredicates with new FieldMode
Closes gh-29063
This commit is contained in:
parent
4368e2563f
commit
7ace1f9dc5
|
@ -72,7 +72,7 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests {
|
|||
RegisteredBean registeredBean = getAndApplyContribution(
|
||||
PrivateFieldInjectionSample.class);
|
||||
assertThat(RuntimeHintsPredicates.reflection()
|
||||
.onField(PrivateFieldInjectionSample.class, "environment").allowWrite())
|
||||
.onField(PrivateFieldInjectionSample.class, "environment").withWriteMode())
|
||||
.accepts(this.generationContext.getRuntimeHints());
|
||||
compile(registeredBean, (postProcessor, compiled) -> {
|
||||
PrivateFieldInjectionSample instance = new PrivateFieldInjectionSample();
|
||||
|
@ -91,7 +91,7 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests {
|
|||
RegisteredBean registeredBean = getAndApplyContribution(
|
||||
PackagePrivateFieldInjectionSample.class);
|
||||
assertThat(RuntimeHintsPredicates.reflection()
|
||||
.onField(PackagePrivateFieldInjectionSample.class, "environment").allowWrite())
|
||||
.onField(PackagePrivateFieldInjectionSample.class, "environment").withWriteMode())
|
||||
.accepts(this.generationContext.getRuntimeHints());
|
||||
compile(registeredBean, (postProcessor, compiled) -> {
|
||||
PackagePrivateFieldInjectionSample instance = new PackagePrivateFieldInjectionSample();
|
||||
|
|
|
@ -272,7 +272,7 @@ enum InstrumentedMethod {
|
|||
* {@link Field#set(Object, Object)}.
|
||||
*/
|
||||
FIELD_SET(Field.class, "set", HintType.REFLECTION,
|
||||
invocation -> RuntimeHintsPredicates.reflection().onField(invocation.getInstance()).allowWrite()),
|
||||
invocation -> RuntimeHintsPredicates.reflection().onField(invocation.getInstance()).withWriteMode()),
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -87,7 +87,7 @@ class InjectionCodeGeneratorTests {
|
|||
TestBean bean = new TestBean();
|
||||
Field field = ReflectionUtils.findField(bean.getClass(), "age");
|
||||
this.generator.generateInjectionCode(field, INSTANCE_VARIABLE, CodeBlock.of("$L", 123));
|
||||
assertThat(RuntimeHintsPredicates.reflection().onField(TestBean.class, "age").allowWrite())
|
||||
assertThat(RuntimeHintsPredicates.reflection().onField(TestBean.class, "age").withWriteMode())
|
||||
.accepts(this.hints);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue