Add FieldMode for field hints and ensure that it cannot be downgraded
Add a `FieldMode` enum analogous to `ExecutableHint` and update `FieldHint` to ensure that registration cannot downgrade `WRITE` to `READ`. Fixes gh-29055
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aot.hint;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link FieldMode}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 6.0
|
||||
*/
|
||||
class FieldModeTests {
|
||||
|
||||
@Test
|
||||
void writeIncludesNullMode() {
|
||||
assertThat(FieldMode.WRITE.includes(null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeIncludesRead() {
|
||||
assertThat(FieldMode.WRITE.includes(FieldMode.READ)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeIncludesWrite() {
|
||||
assertThat(FieldMode.WRITE.includes(FieldMode.WRITE)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void readIncludesNullMode() {
|
||||
assertThat(FieldMode.READ.includes(null)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void readIncludesRead() {
|
||||
assertThat(FieldMode.READ.includes(FieldMode.READ)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void readDoesNotIncludeWrite() {
|
||||
assertThat(FieldMode.READ.includes(FieldMode.WRITE)).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -127,7 +127,7 @@ class ReflectionHintsTests {
|
||||
this.reflectionHints.registerField(field);
|
||||
assertTestTypeFieldHint(fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("field");
|
||||
assertThat(fieldHint.isAllowWrite()).isTrue();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isFalse();
|
||||
});
|
||||
}
|
||||
@@ -139,7 +139,7 @@ class ReflectionHintsTests {
|
||||
this.reflectionHints.registerField(field, fieldHint -> {});
|
||||
assertTestTypeFieldHint(fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("field");
|
||||
assertThat(fieldHint.isAllowWrite()).isTrue();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isFalse();
|
||||
});
|
||||
}
|
||||
@@ -149,14 +149,26 @@ class ReflectionHintsTests {
|
||||
Field field = ReflectionUtils.findField(TestType.class, "field");
|
||||
assertThat(field).isNotNull();
|
||||
this.reflectionHints.registerField(field, fieldHint ->
|
||||
fieldHint.allowWrite(false).allowUnsafeAccess(true));
|
||||
fieldHint.withMode(FieldMode.READ).allowUnsafeAccess(true));
|
||||
assertTestTypeFieldHint(fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("field");
|
||||
assertThat(fieldHint.isAllowWrite()).isFalse();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.READ);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // gh-29055
|
||||
void registerFieldWithCustomizersCannotDowngradeWrite() {
|
||||
Field field = ReflectionUtils.findField(TestType.class, "field");
|
||||
assertThat(field).isNotNull();
|
||||
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.WRITE));
|
||||
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.READ));
|
||||
assertTestTypeFieldHint(fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("field");
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
});
|
||||
}
|
||||
|
||||
private void assertTestTypeFieldHint(Consumer<FieldHint> fieldHint) {
|
||||
assertThat(this.reflectionHints.typeHints()).singleElement().satisfies(typeHint -> {
|
||||
assertThat(typeHint.getType().getCanonicalName()).isEqualTo(TestType.class.getCanonicalName());
|
||||
|
||||
@@ -59,7 +59,7 @@ class TypeHintTests {
|
||||
assertFieldHint(TypeHint.of(TypeReference.of(String.class))
|
||||
.withField("value"), fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("value");
|
||||
assertThat(fieldHint.isAllowWrite()).isTrue();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isFalse();
|
||||
});
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class TypeHintTests {
|
||||
assertFieldHint(TypeHint.of(TypeReference.of(String.class))
|
||||
.withField("value", fieldHint -> {}), fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("value");
|
||||
assertThat(fieldHint.isAllowWrite()).isTrue();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isFalse();
|
||||
});
|
||||
}
|
||||
@@ -78,11 +78,11 @@ class TypeHintTests {
|
||||
void createWithFieldAndCustomizerAppliesCustomization() {
|
||||
assertFieldHint(TypeHint.of(TypeReference.of(String.class))
|
||||
.withField("value", fieldHint -> {
|
||||
fieldHint.allowWrite(false);
|
||||
fieldHint.withMode(FieldMode.READ);
|
||||
fieldHint.allowUnsafeAccess(true);
|
||||
}), fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("value");
|
||||
assertThat(fieldHint.isAllowWrite()).isFalse();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.READ);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isTrue();
|
||||
});
|
||||
}
|
||||
@@ -92,12 +92,12 @@ class TypeHintTests {
|
||||
Builder builder = TypeHint.of(TypeReference.of(String.class));
|
||||
builder.withField("value", fieldHint -> fieldHint.allowUnsafeAccess(true));
|
||||
builder.withField("value", fieldHint -> {
|
||||
fieldHint.allowWrite(true);
|
||||
fieldHint.withMode(FieldMode.WRITE);
|
||||
fieldHint.allowUnsafeAccess(false);
|
||||
});
|
||||
assertFieldHint(builder, fieldHint -> {
|
||||
assertThat(fieldHint.getName()).isEqualTo("value");
|
||||
assertThat(fieldHint.isAllowWrite()).isTrue();
|
||||
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);
|
||||
assertThat(fieldHint.isAllowUnsafeAccess()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.FieldMode;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
@@ -471,7 +472,7 @@ class ReflectionHintsPredicatesTests {
|
||||
@Test
|
||||
void fieldWriteReflectionDoesNotMatchFieldHint() {
|
||||
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField",
|
||||
fieldHint -> fieldHint.allowWrite(false)));
|
||||
fieldHint -> fieldHint.withMode(FieldMode.WRITE)));
|
||||
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite());
|
||||
}
|
||||
|
||||
@@ -485,7 +486,7 @@ class ReflectionHintsPredicatesTests {
|
||||
@Test
|
||||
void fieldWriteReflectionMatchesFieldHintWithWrite() {
|
||||
runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
|
||||
typeHint.withField("publicField", fieldHint -> fieldHint.allowWrite(true)));
|
||||
typeHint.withField("publicField", fieldHint -> fieldHint.withMode(FieldMode.WRITE)));
|
||||
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.skyscreamer.jsonassert.JSONCompareMode;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.FieldMode;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.ProxyHints;
|
||||
import org.springframework.aot.hint.ReflectionHints;
|
||||
@@ -106,9 +107,9 @@ public class FileNativeConfigurationWriterTests {
|
||||
MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS,
|
||||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS,
|
||||
MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES)
|
||||
.withField("DEFAULT_CHARSET", fieldBuilder -> fieldBuilder.allowWrite(false))
|
||||
.withField("DEFAULT_CHARSET", fieldBuilder -> fieldBuilder.withMode(FieldMode.READ))
|
||||
.withField("defaultCharset", fieldBuilder -> {
|
||||
fieldBuilder.allowWrite(true);
|
||||
fieldBuilder.withMode(FieldMode.WRITE);
|
||||
fieldBuilder.allowUnsafeAccess(true);
|
||||
})
|
||||
.withConstructor(TypeReference.listOf(List.class, boolean.class, MimeType.class), ExecutableMode.INTROSPECT)
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.skyscreamer.jsonassert.JSONCompareMode;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.FieldMode;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.ReflectionHints;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
@@ -57,9 +58,9 @@ public class ReflectionHintsWriterTests {
|
||||
MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS,
|
||||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS,
|
||||
MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES)
|
||||
.withField("DEFAULT_CHARSET", fieldBuilder -> fieldBuilder.allowWrite(false))
|
||||
.withField("DEFAULT_CHARSET", fieldBuilder -> fieldBuilder.withMode(FieldMode.READ))
|
||||
.withField("defaultCharset", fieldBuilder -> {
|
||||
fieldBuilder.allowWrite(true);
|
||||
fieldBuilder.withMode(FieldMode.WRITE);
|
||||
fieldBuilder.allowUnsafeAccess(true);
|
||||
})
|
||||
.withConstructor(TypeReference.listOf(List.class, boolean.class, MimeType.class), ExecutableMode.INTROSPECT)
|
||||
|
||||
Reference in New Issue
Block a user