Browse Source

Switch serializable flag to primitive boolean

This commit improves the change made to TypeHint to use a primitive
boolean for the serializable flag.

See gh-36379
pull/36422/head
Stéphane Nicoll 2 weeks ago
parent
commit
03391dfa94
  1. 22
      spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java
  2. 20
      spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java
  3. 34
      spring-core/src/main/java/org/springframework/aot/hint/predicate/ReflectionHintsPredicates.java
  4. 8
      spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsAttributes.java
  5. 4
      spring-core/src/test/java/org/springframework/aot/hint/ProxyHintsTests.java
  6. 22
      spring-core/src/test/java/org/springframework/aot/nativex/RuntimeHintsWriterTests.java

22
spring-core/src/main/java/org/springframework/aot/hint/JdkProxyHint.java

@ -37,13 +37,13 @@ public final class JdkProxyHint implements ConditionalHint {
private final @Nullable TypeReference reachableType; private final @Nullable TypeReference reachableType;
private final @Nullable Boolean serializable; private final boolean javaSerialization;
private JdkProxyHint(Builder builder) { private JdkProxyHint(Builder builder) {
this.proxiedInterfaces = List.copyOf(builder.proxiedInterfaces); this.proxiedInterfaces = List.copyOf(builder.proxiedInterfaces);
this.reachableType = builder.reachableType; this.reachableType = builder.reachableType;
this.serializable = builder.serializable; this.javaSerialization = builder.javaSerialization;
} }
/** /**
@ -79,12 +79,12 @@ public final class JdkProxyHint implements ConditionalHint {
} }
/** /**
* Return whether to register this proxy for Java serialization. * Return whether this hint registers the proxy for Java serialization.
* @return whether to register this proxy for Java serialization. * @return whether the proxy is registered for Java serialization
* @since 7.0.6 * @since 7.0.6
*/ */
public @Nullable Boolean getSerializable() { public boolean hasJavaSerialization() {
return this.serializable; return this.javaSerialization;
} }
@Override @Override
@ -92,7 +92,7 @@ public final class JdkProxyHint implements ConditionalHint {
return (this == other || (other instanceof JdkProxyHint that && return (this == other || (other instanceof JdkProxyHint that &&
this.proxiedInterfaces.equals(that.proxiedInterfaces) && this.proxiedInterfaces.equals(that.proxiedInterfaces) &&
Objects.equals(this.reachableType, that.reachableType) && Objects.equals(this.reachableType, that.reachableType) &&
Objects.equals(this.serializable, that.serializable))); Objects.equals(this.javaSerialization, that.javaSerialization)));
} }
@Override @Override
@ -110,7 +110,7 @@ public final class JdkProxyHint implements ConditionalHint {
private @Nullable TypeReference reachableType; private @Nullable TypeReference reachableType;
private @Nullable Boolean serializable; private boolean javaSerialization;
Builder() { Builder() {
this.proxiedInterfaces = new LinkedList<>(); this.proxiedInterfaces = new LinkedList<>();
@ -148,12 +148,12 @@ public final class JdkProxyHint implements ConditionalHint {
/** /**
* Specify if this proxy should be registered for Java serialization. * Specify if this proxy should be registered for Java serialization.
* @param serializable whether to register this proxy for Java serialization. * @param javaSerialization whether to register this proxy for Java serialization
* @return {@code this}, to facilitate method chaining * @return {@code this}, to facilitate method chaining
* @since 7.0.6 * @since 7.0.6
*/ */
public Builder javaSerialization(@Nullable Boolean serializable) { public Builder withJavaSerialization(boolean javaSerialization) {
this.serializable = serializable; this.javaSerialization = javaSerialization;
return this; return this;
} }

20
spring-core/src/main/java/org/springframework/aot/hint/TypeHint.java

@ -53,7 +53,7 @@ public final class TypeHint implements ConditionalHint {
private final Set<MemberCategory> memberCategories; private final Set<MemberCategory> memberCategories;
private final @Nullable Boolean serializable; private final boolean javaSerialization;
private TypeHint(Builder builder) { private TypeHint(Builder builder) {
@ -63,7 +63,7 @@ public final class TypeHint implements ConditionalHint {
this.fields = builder.fields.stream().map(FieldHint::new).collect(Collectors.toSet()); 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.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()); this.methods = builder.methods.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet());
this.serializable = builder.serializable; this.javaSerialization = builder.javaSerialization;
} }
/** /**
@ -124,12 +124,12 @@ public final class TypeHint implements ConditionalHint {
} }
/** /**
* Return whether to register this type for Java serialization. * Return whether this hint registers the type for Java serialization.
* @return whether to register this type for Java serialization. * @return whether the type is registered for Java serialization
* @since 7.0.6 * @since 7.0.6
*/ */
public @Nullable Boolean getSerializable() { public boolean hasJavaSerialization() {
return this.serializable; return this.javaSerialization;
} }
@Override @Override
@ -165,7 +165,7 @@ public final class TypeHint implements ConditionalHint {
private final Set<MemberCategory> memberCategories = new HashSet<>(); private final Set<MemberCategory> memberCategories = new HashSet<>();
private @Nullable Boolean serializable; private boolean javaSerialization;
Builder(TypeReference type) { Builder(TypeReference type) {
this.type = type; this.type = type;
@ -276,12 +276,12 @@ public final class TypeHint implements ConditionalHint {
/** /**
* Specify if this type should be registered for Java serialization. * Specify if this type should be registered for Java serialization.
* @param serializable whether to register this type for Java serialization. * @param javaSerialization whether to register this type for Java serialization.
* @return {@code this}, to facilitate method chaining * @return {@code this}, to facilitate method chaining
* @since 7.0.6 * @since 7.0.6
*/ */
public Builder withJavaSerialization(@Nullable Boolean serializable) { public Builder withJavaSerialization(boolean javaSerialization) {
this.serializable = serializable; this.javaSerialization = javaSerialization;
return this; return this;
} }

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

@ -23,7 +23,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -261,7 +260,7 @@ public class ReflectionHintsPredicates {
} }
/** /**
* Return a predicate that checks whether an invocation hint is registered for the field. * Return a predicate that checks whether a reflective field access hint is registered for the field.
* This looks up a field on the given type with the expected name, if present. * This looks up a field on the given type with the expected name, if present.
* @param className the name of the class holding the field * @param className the name of the class holding the field
* @param fieldName the field name * @param fieldName the field name
@ -288,7 +287,8 @@ public class ReflectionHintsPredicates {
} }
/** /**
* Return a predicate that checks whether an invocation hint is registered for the given field. * Return a predicate that checks whether a reflective field access hint is
* registered for the given field.
* @param field the field * @param field the field
* @return the {@link RuntimeHints} predicate * @return the {@link RuntimeHints} predicate
* @since 7.0 * @since 7.0
@ -299,27 +299,29 @@ public class ReflectionHintsPredicates {
} }
/** /**
* Return a predicate that checks whether Java serialization is configured according to the given flag. * Return a predicate that checks whether Java serialization is configured
* for the type according to the given flag.
* @param type the type to check * @param type the type to check
* @param serializable the expected serializable flag * @param javaSerialization whether the type is expected to be registered for Java serialization
* @return the {@link RuntimeHints} predicate * @return the {@link RuntimeHints} predicate
* @since 7.0.6 * @since 7.0.6
*/ */
public Predicate<RuntimeHints> onJavaSerialization(Class<?> type, @Nullable Boolean serializable) { public Predicate<RuntimeHints> onJavaSerialization(Class<?> type, boolean javaSerialization) {
Assert.notNull(type, "'type' must not be null"); Assert.notNull(type, "'type' must not be null");
return new SerializationdHintPredicate(TypeReference.of(type), serializable); return new JavaSerializationHintPredicate(TypeReference.of(type), javaSerialization);
} }
/** /**
* Return a predicate that checks whether Java serialization is configured according to the given flag. * Return a predicate that checks whether Java serialization is configured
* for the type according to the given flag.
* @param typeReference the type reference to check * @param typeReference the type reference to check
* @param serializable the expected serializable flag * @param javaSerialization whether the type is expected to be registered for Java serialization
* @return the {@link RuntimeHints} predicate * @return the {@link RuntimeHints} predicate
* @since 7.0.6 * @since 7.0.6
*/ */
public Predicate<RuntimeHints> onJavaSerialization(TypeReference typeReference, @Nullable Boolean serializable) { public Predicate<RuntimeHints> onJavaSerialization(TypeReference typeReference, boolean javaSerialization) {
Assert.notNull(typeReference, "'typeReference' must not be null"); Assert.notNull(typeReference, "'typeReference' must not be null");
return new SerializationdHintPredicate(typeReference, serializable); return new JavaSerializationHintPredicate(typeReference, javaSerialization);
} }
@ -535,15 +537,15 @@ public class ReflectionHintsPredicates {
} }
public static class SerializationdHintPredicate implements Predicate<RuntimeHints> { public static class JavaSerializationHintPredicate implements Predicate<RuntimeHints> {
private final TypeReference typeReference; private final TypeReference typeReference;
private final @Nullable Boolean serializable; private final boolean javaSerialization;
SerializationdHintPredicate(TypeReference typeReference, @Nullable Boolean serializable) { JavaSerializationHintPredicate(TypeReference typeReference, boolean javaSerialization) {
this.typeReference = typeReference; this.typeReference = typeReference;
this.serializable = serializable; this.javaSerialization = javaSerialization;
} }
@Override @Override
@ -552,7 +554,7 @@ public class ReflectionHintsPredicates {
if (typeHint == null) { if (typeHint == null) {
return false; return false;
} }
return Objects.equals(typeHint.getSerializable(), this.serializable); return typeHint.hasJavaSerialization() == this.javaSerialization;
} }
} }

8
spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsAttributes.java

@ -111,7 +111,7 @@ class ReflectionHintsAttributes {
handleFields(attributes, hint.fields()); handleFields(attributes, hint.fields());
handleExecutables(attributes, Stream.concat( handleExecutables(attributes, Stream.concat(
hint.constructors(), hint.methods()).sorted().toList()); hint.constructors(), hint.methods()).sorted().toList());
handleSerializable(attributes, hint.getSerializable()); handleSerializable(attributes, hint.hasJavaSerialization());
return attributes; return attributes;
} }
@ -198,12 +198,12 @@ class ReflectionHintsAttributes {
Map<String, Object> attributes = new LinkedHashMap<>(); Map<String, Object> attributes = new LinkedHashMap<>();
handleCondition(attributes, hint); handleCondition(attributes, hint);
attributes.put("type", Map.of("proxy", hint.getProxiedInterfaces())); attributes.put("type", Map.of("proxy", hint.getProxiedInterfaces()));
handleSerializable(attributes, hint.getSerializable()); handleSerializable(attributes, hint.hasJavaSerialization());
return attributes; return attributes;
} }
private void handleSerializable(Map<String, Object> attributes, @Nullable Boolean serializable) { private void handleSerializable(Map<String, Object> attributes, boolean serializable) {
if (serializable != null) { if (serializable) {
attributes.put("serializable", serializable); attributes.put("serializable", serializable);
} }
} }

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

@ -84,11 +84,11 @@ class ProxyHintsTests {
void registerJdkProxyWithJavaSerialization() { void registerJdkProxyWithJavaSerialization() {
this.proxyHints.registerJdkProxy(hint -> { this.proxyHints.registerJdkProxy(hint -> {
hint.proxiedInterfaces(TypeReference.of("com.example.Test")); hint.proxiedInterfaces(TypeReference.of("com.example.Test"));
hint.javaSerialization(true); hint.withJavaSerialization(true);
}); });
assertThat(this.proxyHints.jdkProxyHints()).singleElement().satisfies(hint -> { assertThat(this.proxyHints.jdkProxyHints()).singleElement().satisfies(hint -> {
assertThat(hint.getProxiedInterfaces()).containsExactly(TypeReference.of("com.example.Test")); assertThat(hint.getProxiedInterfaces()).containsExactly(TypeReference.of("com.example.Test"));
assertThat(hint.getSerializable()).isTrue(); assertThat(hint.hasJavaSerialization()).isTrue();
}); });
} }

22
spring-core/src/test/java/org/springframework/aot/nativex/RuntimeHintsWriterTests.java

@ -208,7 +208,7 @@ class RuntimeHintsWriterTests {
} }
@Test @Test
void serializationEnabled() throws JSONException { void javaSerializationEnabled() throws JSONException {
RuntimeHints hints = new RuntimeHints(); RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(Integer.class, builder -> builder.withJavaSerialization(true)); hints.reflection().registerType(Integer.class, builder -> builder.withJavaSerialization(true));
@ -224,6 +224,22 @@ class RuntimeHintsWriterTests {
""", hints); """, hints);
} }
@Test
void javaSerializationDisabled() throws JSONException {
RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(Integer.class, builder -> builder.withJavaSerialization(false));
assertEquals("""
{
"reflection": [
{
"type": "java.lang.Integer"
}
]
}
""", hints);
}
@Test @Test
void ignoreLambda() throws JSONException { void ignoreLambda() throws JSONException {
Runnable anonymousRunnable = () -> {}; Runnable anonymousRunnable = () -> {};
@ -658,11 +674,11 @@ class RuntimeHintsWriterTests {
} }
@Test @Test
void shouldWriteSerialization() throws JSONException { void shouldWriteSerializable() throws JSONException {
RuntimeHints hints = new RuntimeHints(); RuntimeHints hints = new RuntimeHints();
hints.proxies().registerJdkProxy(hint -> { hints.proxies().registerJdkProxy(hint -> {
hint.proxiedInterfaces(Function.class); hint.proxiedInterfaces(Function.class);
hint.javaSerialization(true); hint.withJavaSerialization(true);
}); });
assertEquals(""" assertEquals("""
{ {

Loading…
Cancel
Save