Write runtime hints with deterministic order
This commit updates the JSON writers to use a deterministic order for arrays. Previously, the order could change with the same content, breaking caching. Closes gh-31852
This commit is contained in:
@@ -79,6 +79,10 @@ public abstract class AbstractTypeReference implements TypeReference {
|
||||
|
||||
protected abstract boolean isPrimitive();
|
||||
|
||||
@Override
|
||||
public int compareTo(TypeReference other) {
|
||||
return this.getCanonicalName().compareToIgnoreCase(other.getCanonicalName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -19,8 +19,10 @@ package org.springframework.aot.hint;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -32,7 +34,7 @@ import org.springframework.util.Assert;
|
||||
* @author Stephane Nicoll
|
||||
* @since 6.0
|
||||
*/
|
||||
public final class ExecutableHint extends MemberHint {
|
||||
public final class ExecutableHint extends MemberHint implements Comparable<ExecutableHint> {
|
||||
|
||||
private final List<TypeReference> parameterTypes;
|
||||
|
||||
@@ -91,6 +93,17 @@ public final class ExecutableHint extends MemberHint {
|
||||
return builder -> builder.withMode(mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ExecutableHint other) {
|
||||
return Comparator.comparing(ExecutableHint::getName, String::compareToIgnoreCase)
|
||||
.thenComparing(ExecutableHint::getParameterTypes, Comparator.comparingInt(List::size))
|
||||
.thenComparing(ExecutableHint::getParameterTypes, (params1, params2) -> {
|
||||
String left = params1.stream().map(TypeReference::getCanonicalName).collect(Collectors.joining(","));
|
||||
String right = params2.stream().map(TypeReference::getCanonicalName).collect(Collectors.joining(","));
|
||||
return left.compareTo(right);
|
||||
}).compare(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link ExecutableHint}.
|
||||
*/
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.lang.Nullable;
|
||||
* @author Sebastien Deleuze
|
||||
* @since 6.0
|
||||
*/
|
||||
public interface TypeReference {
|
||||
public interface TypeReference extends Comparable<TypeReference> {
|
||||
|
||||
/**
|
||||
* Return the fully qualified name of this type reference.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -16,11 +16,14 @@
|
||||
|
||||
package org.springframework.aot.nativex;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.aot.hint.JdkProxyHint;
|
||||
import org.springframework.aot.hint.ProxyHints;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
|
||||
/**
|
||||
* Write {@link JdkProxyHint}s contained in a {@link ProxyHints} to the JSON
|
||||
@@ -38,8 +41,18 @@ class ProxyHintsWriter {
|
||||
|
||||
public static final ProxyHintsWriter INSTANCE = new ProxyHintsWriter();
|
||||
|
||||
private static final Comparator<JdkProxyHint> JDK_PROXY_HINT_COMPARATOR =
|
||||
(left, right) -> {
|
||||
String leftSignature = left.getProxiedInterfaces().stream()
|
||||
.map(TypeReference::getCanonicalName).collect(Collectors.joining(","));
|
||||
String rightSignature = right.getProxiedInterfaces().stream()
|
||||
.map(TypeReference::getCanonicalName).collect(Collectors.joining(","));
|
||||
return leftSignature.compareTo(rightSignature);
|
||||
};
|
||||
|
||||
public void write(BasicJsonWriter writer, ProxyHints hints) {
|
||||
writer.writeArray(hints.jdkProxyHints().map(this::toAttributes).toList());
|
||||
writer.writeArray(hints.jdkProxyHints().sorted(JDK_PROXY_HINT_COMPARATOR)
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> toAttributes(JdkProxyHint hint) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.aot.nativex;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -49,7 +50,9 @@ class ReflectionHintsWriter {
|
||||
public static final ReflectionHintsWriter INSTANCE = new ReflectionHintsWriter();
|
||||
|
||||
public void write(BasicJsonWriter writer, ReflectionHints hints) {
|
||||
writer.writeArray(hints.typeHints().map(this::toAttributes).toList());
|
||||
writer.writeArray(hints.typeHints()
|
||||
.sorted(Comparator.comparing(TypeHint::getType))
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> toAttributes(TypeHint hint) {
|
||||
@@ -58,7 +61,8 @@ class ReflectionHintsWriter {
|
||||
handleCondition(attributes, hint);
|
||||
handleCategories(attributes, hint.getMemberCategories());
|
||||
handleFields(attributes, hint.fields());
|
||||
handleExecutables(attributes, Stream.concat(hint.constructors(), hint.methods()).toList());
|
||||
handleExecutables(attributes, Stream.concat(
|
||||
hint.constructors(), hint.methods()).sorted().toList());
|
||||
return attributes;
|
||||
}
|
||||
|
||||
@@ -71,7 +75,9 @@ class ReflectionHintsWriter {
|
||||
}
|
||||
|
||||
private void handleFields(Map<String, Object> attributes, Stream<FieldHint> fields) {
|
||||
addIfNotEmpty(attributes, "fields", fields.map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "fields", fields
|
||||
.sorted(Comparator.comparing(FieldHint::getName, String::compareToIgnoreCase))
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> toAttributes(FieldHint hint) {
|
||||
@@ -97,7 +103,7 @@ class ReflectionHintsWriter {
|
||||
}
|
||||
|
||||
private void handleCategories(Map<String, Object> attributes, Set<MemberCategory> categories) {
|
||||
categories.forEach(category -> {
|
||||
categories.stream().sorted().forEach(category -> {
|
||||
switch (category) {
|
||||
case PUBLIC_FIELDS -> attributes.put("allPublicFields", true);
|
||||
case DECLARED_FIELDS -> attributes.put("allDeclaredFields", true);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.aot.nativex;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -44,6 +45,13 @@ class ResourceHintsWriter {
|
||||
|
||||
public static final ResourceHintsWriter INSTANCE = new ResourceHintsWriter();
|
||||
|
||||
private static final Comparator<ResourcePatternHint> RESOURCE_PATTERN_HINT_COMPARATOR =
|
||||
Comparator.comparing(ResourcePatternHint::getPattern);
|
||||
|
||||
private static final Comparator<ResourceBundleHint> RESOURCE_BUNDLE_HINT_COMPARATOR =
|
||||
Comparator.comparing(ResourceBundleHint::getBaseName);
|
||||
|
||||
|
||||
public void write(BasicJsonWriter writer, ResourceHints hints) {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
addIfNotEmpty(attributes, "resources", toAttributes(hints));
|
||||
@@ -53,15 +61,21 @@ class ResourceHintsWriter {
|
||||
|
||||
private Map<String, Object> toAttributes(ResourceHints hint) {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
addIfNotEmpty(attributes, "includes", hint.resourcePatternHints().map(ResourcePatternHints::getIncludes)
|
||||
.flatMap(List::stream).distinct().map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "excludes", hint.resourcePatternHints().map(ResourcePatternHints::getExcludes)
|
||||
.flatMap(List::stream).distinct().map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "includes", hint.resourcePatternHints()
|
||||
.map(ResourcePatternHints::getIncludes).flatMap(List::stream).distinct()
|
||||
.sorted(RESOURCE_PATTERN_HINT_COMPARATOR)
|
||||
.map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "excludes", hint.resourcePatternHints()
|
||||
.map(ResourcePatternHints::getExcludes).flatMap(List::stream).distinct()
|
||||
.sorted(RESOURCE_PATTERN_HINT_COMPARATOR)
|
||||
.map(this::toAttributes).toList());
|
||||
return attributes;
|
||||
}
|
||||
|
||||
private void handleResourceBundles(Map<String, Object> attributes, Stream<ResourceBundleHint> resourceBundles) {
|
||||
addIfNotEmpty(attributes, "bundles", resourceBundles.map(this::toAttributes).toList());
|
||||
addIfNotEmpty(attributes, "bundles", resourceBundles
|
||||
.sorted(RESOURCE_BUNDLE_HINT_COMPARATOR)
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> toAttributes(ResourceBundleHint hint) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.aot.nativex;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,8 +39,13 @@ class SerializationHintsWriter {
|
||||
|
||||
public static final SerializationHintsWriter INSTANCE = new SerializationHintsWriter();
|
||||
|
||||
private static final Comparator<JavaSerializationHint> JAVA_SERIALIZATION_HINT_COMPARATOR =
|
||||
Comparator.comparing(JavaSerializationHint::getType);
|
||||
|
||||
public void write(BasicJsonWriter writer, SerializationHints hints) {
|
||||
writer.writeArray(hints.javaSerializationHints().map(this::toAttributes).toList());
|
||||
writer.writeArray(hints.javaSerializationHints()
|
||||
.sorted(JAVA_SERIALIZATION_HINT_COMPARATOR)
|
||||
.map(this::toAttributes).toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> toAttributes(JavaSerializationHint serializationHint) {
|
||||
|
||||
Reference in New Issue
Block a user