From 7ace1f9dc528ba0047a6a6110daab7675e2626d6 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 2 Sep 2022 18:15:50 +0200 Subject: [PATCH] Align RuntimeHintsPredicates with new FieldMode Closes gh-29063 --- ...nBeanRegistrationAotContributionTests.java | 4 +-- .../aot/agent/InstrumentedMethod.java | 2 +- .../springframework/aot/hint/FieldMode.java | 2 +- .../predicate/ReflectionHintsPredicates.java | 29 +++++++++++++++++-- .../ReflectionHintsPredicatesTests.java | 4 +-- .../support/InjectionCodeGeneratorTests.java | 2 +- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java index fa955a96fa..998889ba19 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java @@ -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(); diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java index 2291d0daa1..c242d8575a 100644 --- a/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java +++ b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java @@ -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()), /* diff --git a/spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java b/spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java index 9a8b3a04c5..289e479314 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/FieldMode.java @@ -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()); } diff --git a/spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java b/spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java index ca8f857bee..9d35519a0f 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java @@ -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())); } } diff --git a/spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java b/spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java index 2407af9057..957c58aa1a 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicatesTests.java @@ -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 diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java index e3ca321286..499087c9f9 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/support/InjectionCodeGeneratorTests.java @@ -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); }