Browse Source

Align RuntimeHintsPredicates with new FieldMode

Closes gh-29063
pull/29066/head
Brian Clozel 2 years ago
parent
commit
7ace1f9dc5
  1. 4
      spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java
  2. 2
      spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java
  3. 2
      spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java
  4. 29
      spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java
  5. 4
      spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java
  6. 2
      spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java

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

@ -72,7 +72,7 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests { @@ -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 { @@ -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();

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

@ -272,7 +272,7 @@ enum InstrumentedMethod { @@ -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()),
/*

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

@ -44,7 +44,7 @@ public enum FieldMode { @@ -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());
}

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

@ -352,7 +352,7 @@ public class ReflectionHintsPredicates { @@ -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 { @@ -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 { @@ -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()));
}
}

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

@ -452,7 +452,7 @@ class ReflectionHintsPredicatesTests { @@ -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 { @@ -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

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

@ -87,7 +87,7 @@ class InjectionCodeGeneratorTests { @@ -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…
Cancel
Save