Skip to content

Commit 7ace1f9

Browse files
committed
Align RuntimeHintsPredicates with new FieldMode
Closes gh-29063
1 parent 4368e25 commit 7ace1f9

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void contributeWhenPrivateFieldInjectionInjectsUsingReflection() {
7272
RegisteredBean registeredBean = getAndApplyContribution(
7373
PrivateFieldInjectionSample.class);
7474
assertThat(RuntimeHintsPredicates.reflection()
75-
.onField(PrivateFieldInjectionSample.class, "environment").allowWrite())
75+
.onField(PrivateFieldInjectionSample.class, "environment").withWriteMode())
7676
.accepts(this.generationContext.getRuntimeHints());
7777
compile(registeredBean, (postProcessor, compiled) -> {
7878
PrivateFieldInjectionSample instance = new PrivateFieldInjectionSample();
@@ -91,7 +91,7 @@ void contributeWhenPackagePrivateFieldInjectionInjectsUsingConsumer() {
9191
RegisteredBean registeredBean = getAndApplyContribution(
9292
PackagePrivateFieldInjectionSample.class);
9393
assertThat(RuntimeHintsPredicates.reflection()
94-
.onField(PackagePrivateFieldInjectionSample.class, "environment").allowWrite())
94+
.onField(PackagePrivateFieldInjectionSample.class, "environment").withWriteMode())
9595
.accepts(this.generationContext.getRuntimeHints());
9696
compile(registeredBean, (postProcessor, compiled) -> {
9797
PackagePrivateFieldInjectionSample instance = new PackagePrivateFieldInjectionSample();

spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ enum InstrumentedMethod {
272272
* {@link Field#set(Object, Object)}.
273273
*/
274274
FIELD_SET(Field.class, "set", HintType.REFLECTION,
275-
invocation -> RuntimeHintsPredicates.reflection().onField(invocation.getInstance()).allowWrite()),
275+
invocation -> RuntimeHintsPredicates.reflection().onField(invocation.getInstance()).withWriteMode()),
276276

277277

278278
/*

spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum FieldMode {
4444
* @param other the other mode to check
4545
* @return {@code true} if this mode includes the other mode
4646
*/
47-
boolean includes(@Nullable FieldMode other) {
47+
public boolean includes(@Nullable FieldMode other) {
4848
return (other == null || this.ordinal() >= other.ordinal());
4949
}
5050

spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public static class FieldHintPredicate implements Predicate<RuntimeHints> {
352352

353353
private final Field field;
354354

355-
private boolean allowWrite;
355+
private FieldMode mode = FieldMode.READ;
356356

357357
private boolean allowUnsafeAccess;
358358

@@ -364,12 +364,35 @@ public static class FieldHintPredicate implements Predicate<RuntimeHints> {
364364
* Refine the current predicate to match if write access is allowed on the field.
365365
* @return the refined {@link RuntimeHints} predicate
366366
* @see FieldHint#isAllowWrite()
367+
* @deprecated in favor of {@link #withReadMode()} or {@link #withWriteMode()}
367368
*/
369+
@Deprecated
368370
public FieldHintPredicate allowWrite() {
369-
this.allowWrite = true;
371+
this.mode = FieldMode.WRITE;
370372
return this;
371373
}
372374

375+
/**
376+
* Refine the current predicate to match if read access is allowed on the field.
377+
* @return the refined {@link RuntimeHints} predicate
378+
* @see FieldHint#getMode()
379+
*/
380+
public FieldHintPredicate withReadMode() {
381+
// FieldMode.READ is already the default and should not override a writeMode() call.
382+
return this;
383+
}
384+
385+
/**
386+
* Refine the current predicate to match if write access is allowed on the field.
387+
* @return the refined {@link RuntimeHints} predicate
388+
* @see FieldHint#getMode()
389+
*/
390+
public FieldHintPredicate withWriteMode() {
391+
this.mode = FieldMode.WRITE;
392+
return this;
393+
}
394+
395+
373396
/**
374397
* Refine the current predicate to match if unsafe access is allowed on the field.
375398
* @return the refined {@link RuntimeHints} predicate
@@ -402,7 +425,7 @@ private boolean memberCategoryMatch(TypeHint typeHint) {
402425
private boolean exactMatch(TypeHint typeHint) {
403426
return typeHint.fields().anyMatch(fieldHint ->
404427
this.field.getName().equals(fieldHint.getName())
405-
&& (!this.allowWrite || fieldHint.getMode() == FieldMode.WRITE)
428+
&& (fieldHint.getMode().includes(this.mode))
406429
&& (!this.allowUnsafeAccess || this.allowUnsafeAccess == fieldHint.isAllowUnsafeAccess()));
407430
}
408431
}

spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ void fieldReflectionMatchesFieldHint() {
452452
void fieldWriteReflectionDoesNotMatchFieldHint() {
453453
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField",
454454
FieldMode.READ));
455-
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite());
455+
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").withWriteMode());
456456
}
457457

458458
@Test
@@ -465,7 +465,7 @@ void fieldUnsafeReflectionDoesNotMatchFieldHint() {
465465
void fieldWriteReflectionMatchesFieldHintWithWrite() {
466466
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
467467
typeHint.withField("publicField", FieldMode.WRITE));
468-
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
468+
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").withWriteMode());
469469
}
470470

471471
@Test

spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void generateCodeWhenPrivateFieldAddsHint() {
8787
TestBean bean = new TestBean();
8888
Field field = ReflectionUtils.findField(bean.getClass(), "age");
8989
this.generator.generateInjectionCode(field, INSTANCE_VARIABLE, CodeBlock.of("$L", 123));
90-
assertThat(RuntimeHintsPredicates.reflection().onField(TestBean.class, "age").allowWrite())
90+
assertThat(RuntimeHintsPredicates.reflection().onField(TestBean.class, "age").withWriteMode())
9191
.accepts(this.hints);
9292
}
9393

0 commit comments

Comments
 (0)