Apply consistent RuntimeHints defaults
This commit harmonizes the registration of an executable so that the default method and the method that takes an empty customizer produces the same hint. The same applies to the readable flag of a field hint. Rather than returning a list of executable modes, the "highest" mode is retained. See gh-29011
This commit is contained in:
@@ -19,12 +19,9 @@ package org.springframework.aot.hint;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* A hint that describes the need for reflection on a {@link Method} or
|
||||
@@ -37,13 +34,13 @@ public final class ExecutableHint extends MemberHint {
|
||||
|
||||
private final List<TypeReference> parameterTypes;
|
||||
|
||||
private final List<ExecutableMode> modes;
|
||||
private final ExecutableMode mode;
|
||||
|
||||
|
||||
private ExecutableHint(Builder builder) {
|
||||
super(builder.name);
|
||||
this.parameterTypes = List.copyOf(builder.parameterTypes);
|
||||
this.modes = List.copyOf(builder.modes);
|
||||
this.mode = (builder.mode != null ? builder.mode : ExecutableMode.INVOKE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,11 +72,11 @@ public final class ExecutableHint extends MemberHint {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@linkplain ExecutableMode modes} that apply to this hint.
|
||||
* Return the {@linkplain ExecutableMode mode} that apply to this hint.
|
||||
* @return the modes
|
||||
*/
|
||||
public List<ExecutableMode> getModes() {
|
||||
return this.modes;
|
||||
public ExecutableMode getMode() {
|
||||
return this.mode;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +89,8 @@ public final class ExecutableHint extends MemberHint {
|
||||
|
||||
private final List<TypeReference> parameterTypes;
|
||||
|
||||
private final Set<ExecutableMode> modes = new LinkedHashSet<>();
|
||||
@Nullable
|
||||
private ExecutableMode mode;
|
||||
|
||||
|
||||
Builder(String name, List<TypeReference> parameterTypes) {
|
||||
@@ -101,12 +99,14 @@ public final class ExecutableHint extends MemberHint {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified {@linkplain ExecutableMode mode} if necessary.
|
||||
* @param mode the mode to add
|
||||
* Specify that the {@linkplain ExecutableMode mode} is required.
|
||||
* @param mode the required mode
|
||||
* @return {@code this}, to facilitate method chaining
|
||||
*/
|
||||
public Builder withMode(ExecutableMode mode) {
|
||||
this.modes.add(mode);
|
||||
if (this.mode == null || !this.mode.includes(mode)) {
|
||||
this.mode = mode;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -114,11 +114,15 @@ public final class ExecutableHint extends MemberHint {
|
||||
* Set the {@linkplain ExecutableMode modes} to use.
|
||||
* @param modes the mode to use
|
||||
* @return {@code this}, to facilitate method chaining
|
||||
* @deprecated only a single mode can be set, use {@link #withMode(ExecutableMode)} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public Builder setModes(ExecutableMode... modes) {
|
||||
this.modes.clear();
|
||||
if (!ObjectUtils.isEmpty(modes)) {
|
||||
this.modes.addAll(Arrays.asList(modes));
|
||||
if (modes.length > 1) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
if (modes.length == 1) {
|
||||
withMode(modes[0]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.aot.hint;
|
||||
|
||||
import java.lang.reflect.Executable;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Represent the need of reflection for a given {@link Executable}.
|
||||
*
|
||||
@@ -37,4 +39,13 @@ public enum ExecutableMode {
|
||||
*/
|
||||
INVOKE;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
boolean includes(@Nullable ExecutableMode other) {
|
||||
return (other == null || this.ordinal() >= other.ordinal());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.aot.hint;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* A hint that describes the need of reflection on a {@link Field}.
|
||||
*
|
||||
@@ -33,7 +35,7 @@ public final class FieldHint extends MemberHint {
|
||||
|
||||
private FieldHint(Builder builder) {
|
||||
super(builder.name);
|
||||
this.allowWrite = builder.allowWrite;
|
||||
this.allowWrite = (builder.allowWrite != null) ? builder.allowWrite : true;
|
||||
this.allowUnsafeAccess = builder.allowUnsafeAccess;
|
||||
}
|
||||
|
||||
@@ -61,7 +63,8 @@ public final class FieldHint extends MemberHint {
|
||||
|
||||
private final String name;
|
||||
|
||||
private boolean allowWrite;
|
||||
@Nullable
|
||||
private Boolean allowWrite;
|
||||
|
||||
private boolean allowUnsafeAccess;
|
||||
|
||||
|
||||
@@ -165,8 +165,7 @@ public class ReflectionHints {
|
||||
* @return {@code this}, to facilitate method chaining
|
||||
*/
|
||||
public ReflectionHints registerConstructor(Constructor<?> constructor) {
|
||||
return registerConstructor(constructor, constructorHint ->
|
||||
constructorHint.withMode(ExecutableMode.INVOKE));
|
||||
return registerConstructor(constructor, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,10 +20,7 @@ import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.aot.hint.ExecutableHint.Builder;
|
||||
import org.springframework.aot.hint.ExecutableMode;
|
||||
import org.springframework.aot.hint.ReflectionHints;
|
||||
|
||||
/**
|
||||
@@ -36,8 +33,6 @@ import org.springframework.aot.hint.ReflectionHints;
|
||||
*/
|
||||
public class SimpleReflectiveProcessor implements ReflectiveProcessor {
|
||||
|
||||
private static final Consumer<Builder> INVOKE_EXECUTABLE = hint -> hint.setModes(ExecutableMode.INVOKE);
|
||||
|
||||
@Override
|
||||
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
|
||||
if (element instanceof Class<?> type) {
|
||||
@@ -69,7 +64,7 @@ public class SimpleReflectiveProcessor implements ReflectiveProcessor {
|
||||
* @param constructor the constructor to process
|
||||
*/
|
||||
protected void registerConstructorHint(ReflectionHints hints, Constructor<?> constructor) {
|
||||
hints.registerConstructor(constructor, INVOKE_EXECUTABLE);
|
||||
hints.registerConstructor(constructor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +82,7 @@ public class SimpleReflectiveProcessor implements ReflectiveProcessor {
|
||||
* @param method the method to process
|
||||
*/
|
||||
protected void registerMethodHint(ReflectionHints hints, Method method) {
|
||||
hints.registerMethod(method, INVOKE_EXECUTABLE);
|
||||
hints.registerMethod(method);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -263,11 +263,11 @@ public class ReflectionHintsPredicates {
|
||||
* and the configured {@code ExecutableMode} is compatibe
|
||||
*/
|
||||
static boolean includes(ExecutableHint hint, String name,
|
||||
List<TypeReference> parameterTypes, List<ExecutableMode> executableModes) {
|
||||
List<TypeReference> parameterTypes, ExecutableMode executableModes) {
|
||||
return hint.getName().equals(name)
|
||||
&& hint.getParameterTypes().equals(parameterTypes)
|
||||
&& (hint.getModes().contains(ExecutableMode.INVOKE)
|
||||
|| !executableModes.contains(ExecutableMode.INVOKE));
|
||||
&& (hint.getMode().equals(ExecutableMode.INVOKE)
|
||||
|| !executableModes.equals(ExecutableMode.INVOKE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ public class ReflectionHintsPredicates {
|
||||
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes())
|
||||
.map(TypeReference::of).toList();
|
||||
return includes(executableHint, "<init>",
|
||||
parameters, List.of(this.executableMode));
|
||||
parameters, this.executableMode);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ public class ReflectionHintsPredicates {
|
||||
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes())
|
||||
.map(TypeReference::of).toList();
|
||||
return includes(executableHint, this.executable.getName(),
|
||||
parameters, List.of(this.executableMode));
|
||||
parameters, this.executableMode);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -85,10 +85,10 @@ class ReflectionHintsWriter {
|
||||
|
||||
private void handleExecutables(Map<String, Object> attributes, List<ExecutableHint> hints) {
|
||||
addIfNotEmpty(attributes, "methods", hints.stream()
|
||||
.filter(h -> h.getModes().contains(ExecutableMode.INVOKE) || h.getModes().isEmpty())
|
||||
.filter(h -> h.getMode().equals(ExecutableMode.INVOKE))
|
||||
.map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "queriedMethods", hints.stream()
|
||||
.filter(h -> h.getModes().contains(ExecutableMode.INTROSPECT))
|
||||
.filter(h -> h.getMode().equals(ExecutableMode.INTROSPECT))
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user