Polish "Adapt FieldHint to recent GraalVM versions"

See gh-29130
This commit is contained in:
Stephane Nicoll
2022-09-10 15:56:26 +02:00
parent 1cb5f00723
commit 042a4f3518
16 changed files with 49 additions and 388 deletions

View File

@@ -18,34 +18,16 @@ package org.springframework.aot.hint;
import java.lang.reflect.Field;
import org.springframework.lang.Nullable;
/**
* Represents the need of reflection for a given {@link Field}.
* A hint that describes the need for reflection on a {@link Field}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 6.0
* @see ReflectionHints
*/
public enum FieldMode {
public final class FieldHint extends MemberHint {
/**
* Only field read is required.
*/
READ,
/**
* Full field read and write is required.
*/
WRITE;
/**
* Specify if this mode already includes the specified {@code other} mode.
* @param other the other mode to check
* @return {@code true} if this mode includes the other mode
*/
public boolean includes(@Nullable FieldMode other) {
return (other == null || this.ordinal() >= other.ordinal());
FieldHint(String name) {
super(name);
}
}

View File

@@ -148,34 +148,13 @@ public class ReflectionHints {
}
/**
* Register the need for reflection on the specified {@link Field},
* enabling {@link FieldMode#WRITE}.
* Register the need for reflection on the specified {@link Field}.
* @param field the field that requires reflection
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerField(Field field) {
return registerField(field, FieldMode.WRITE);
}
/**
* Register the need for reflection on the specified {@link Field}
* using the specified {@link FieldMode}.
* @param field the field that requires reflection
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerField(Field field, FieldMode mode) {
return registerField(field, FieldHint.builtWith(mode));
}
/**
* Register the need for reflection on the specified {@link Field}.
* @param field the field that requires reflection
* @param fieldHint a builder to further customize the hints of this field
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerField(Field field, Consumer<FieldHint.Builder> fieldHint) {
return registerType(TypeReference.of(field.getDeclaringClass()),
typeHint -> typeHint.withField(field.getName(), fieldHint));
typeHint -> typeHint.withField(field.getName()));
}
/**

View File

@@ -44,7 +44,7 @@ public final class TypeHint implements ConditionalHint {
@Nullable
private final TypeReference reachableType;
private final Set<String> fields;
private final Set<FieldHint> fields;
private final Set<ExecutableHint> constructors;
@@ -57,7 +57,7 @@ public final class TypeHint implements ConditionalHint {
this.type = builder.type;
this.reachableType = builder.reachableType;
this.memberCategories = Set.copyOf(builder.memberCategories);
this.fields = builder.fields;
this.fields = builder.fields.stream().map(FieldHint::new).collect(Collectors.toSet());
this.constructors = builder.constructors.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet());
this.methods = builder.methods.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet());
}
@@ -89,10 +89,10 @@ public final class TypeHint implements ConditionalHint {
/**
* Return the fields that require reflection.
* @return a stream of Strings
* @return a stream of {@link FieldHint}
*/
public Set<String> fields() {
return this.fields;
public Stream<FieldHint> fields() {
return this.fields.stream();
}
/**
@@ -185,16 +185,15 @@ public final class TypeHint implements ConditionalHint {
}
/**
* Register the need for reflection on the field with the specified name,
* enabling write access.
* Register the need for reflection on the field with the specified name.
* @param name the name of the field
* @return {@code this}, to facilitate method chaining
*/
public Builder withField(String name) {
return withField(name);
this.fields.add(name);
return this;
}
/**
* Register the need for reflection on the constructor with the specified
* parameter types, enabling {@link ExecutableMode#INVOKE}.

View File

@@ -28,8 +28,6 @@ import java.util.function.Predicate;
import org.springframework.aot.hint.ExecutableHint;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.FieldHint;
import org.springframework.aot.hint.FieldMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
@@ -375,57 +373,10 @@ public class ReflectionHintsPredicates {
private final Field field;
private FieldMode mode = FieldMode.READ;
private boolean allowUnsafeAccess;
FieldHintPredicate(Field field) {
this.field = field;
}
/**
* 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.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
* @see FieldHint#isAllowUnsafeAccess() ()
*/
public FieldHintPredicate allowUnsafeAccess() {
this.allowUnsafeAccess = true;
return this;
}
@Override
public boolean test(RuntimeHints runtimeHints) {
TypeHint typeHint = runtimeHints.reflection().getTypeHint(this.field.getDeclaringClass());
@@ -447,9 +398,7 @@ public class ReflectionHintsPredicates {
private boolean exactMatch(TypeHint typeHint) {
return typeHint.fields().anyMatch(fieldHint ->
this.field.getName().equals(fieldHint.getName())
&& (fieldHint.getMode().includes(this.mode))
&& (!this.allowUnsafeAccess || this.allowUnsafeAccess == fieldHint.isAllowUnsafeAccess()));
this.field.getName().equals(fieldHint.getName()));
}
}

View File

@@ -26,7 +26,6 @@ import java.util.stream.Stream;
import org.springframework.aot.hint.ExecutableHint;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.FieldHint;
import org.springframework.aot.hint.FieldMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeHint;
@@ -78,12 +77,6 @@ class ReflectionHintsWriter {
private Map<String, Object> toAttributes(FieldHint hint) {
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put("name", hint.getName());
if (hint.getMode() == FieldMode.WRITE) {
attributes.put("allowWrite", true);
}
if (hint.isAllowUnsafeAccess()) {
attributes.put("allowUnsafeAccess", hint.isAllowUnsafeAccess());
}
return attributes;
}