Browse Source

Adapt to BasicJsonWriter

pull/28332/head
Stephane Nicoll 4 years ago
parent
commit
4e9306fa1b
  1. 33
      spring-core/src/main/java/org/springframework/aot/nativex/JavaSerializationHintsSerializer.java
  2. 42
      spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsSerializer.java
  3. 171
      spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsSerializer.java
  4. 100
      spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsSerializer.java

33
spring-core/src/main/java/org/springframework/aot/nativex/JavaSerializationHintsSerializer.java

@ -16,35 +16,36 @@
package org.springframework.aot.nativex; package org.springframework.aot.nativex;
import java.util.Iterator; import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.aot.hint.JavaSerializationHints; import org.springframework.aot.hint.JavaSerializationHints;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
/** /**
* Serialize a {@link JavaSerializationHints} to the JSON file expected by GraalVM {@code native-image} compiler, * Serialize a {@link JavaSerializationHints} to the JSON output expected by the
* typically named {@code serialization-config.json}. * GraalVM {@code native-image} compiler, typically named
* {@code serialization-config.json}.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0 * @since 6.0
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
*/ */
class JavaSerializationHintsSerializer { class JavaSerializationHintsSerializer {
public String serialize(JavaSerializationHints hints) { public String serialize(JavaSerializationHints hints) {
StringBuilder builder = new StringBuilder(); StringWriter sw = new StringWriter();
builder.append("[\n"); BasicJsonWriter writer = new BasicJsonWriter(sw, " ");
Iterator<TypeReference> typeIterator = hints.types().iterator(); writer.writeArray(hints.types().map(this::toAttributes).toList());
while (typeIterator.hasNext()) { return sw.toString();
TypeReference type = typeIterator.next(); }
String name = JsonUtils.escape(type.getCanonicalName());
builder.append("{ \"name\": \"").append(name).append("\" }"); private Map<String, Object> toAttributes(TypeReference typeReference) {
if (typeIterator.hasNext()) { LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
builder.append(",\n"); attributes.put("name", typeReference.getCanonicalName());
} return attributes;
}
builder.append("\n]\n");
return builder.toString();
} }
} }

42
spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsSerializer.java

@ -16,17 +16,21 @@
package org.springframework.aot.nativex; package org.springframework.aot.nativex;
import java.util.Iterator; import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.aot.hint.JdkProxyHint; import org.springframework.aot.hint.JdkProxyHint;
import org.springframework.aot.hint.ProxyHints; import org.springframework.aot.hint.ProxyHints;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
/** /**
* Serialize {@link JdkProxyHint}s contained in a {@link ProxyHints} to the JSON file expected by GraalVM * Serialize {@link JdkProxyHint}s contained in a {@link ProxyHints} to the JSON
* {@code native-image} compiler, typically named {@code proxy-config.json}. * output expected by the GraalVM {@code native-image} compiler, typically named
* {@code proxy-config.json}.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0 * @since 6.0
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/DynamicProxy/">Dynamic Proxy in Native Image</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/DynamicProxy/">Dynamic Proxy in Native Image</a>
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
@ -34,27 +38,17 @@ import org.springframework.aot.hint.TypeReference;
class ProxyHintsSerializer { class ProxyHintsSerializer {
public String serialize(ProxyHints hints) { public String serialize(ProxyHints hints) {
StringBuilder builder = new StringBuilder(); StringWriter sw = new StringWriter();
builder.append("[\n"); BasicJsonWriter writer = new BasicJsonWriter(sw, " ");
Iterator<JdkProxyHint> hintIterator = hints.jdkProxies().iterator(); writer.writeArray(hints.jdkProxies().map(this::toAttributes).toList());
while (hintIterator.hasNext()) { return sw.toString();
builder.append("{ \"interfaces\": [ "); }
JdkProxyHint hint = hintIterator.next();
Iterator<TypeReference> interfaceIterator = hint.getProxiedInterfaces().iterator(); private Map<String, Object> toAttributes(JdkProxyHint hint) {
while (interfaceIterator.hasNext()) { Map<String, Object> attributes = new LinkedHashMap<>();
String name = JsonUtils.escape(interfaceIterator.next().getCanonicalName()); attributes.put("interfaces", hint.getProxiedInterfaces().stream()
builder.append("\"").append(name).append("\""); .map(TypeReference::getCanonicalName).toList());
if (interfaceIterator.hasNext()) { return attributes;
builder.append(", ");
}
}
builder.append(" ] }");
if (hintIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n]\n");
return builder.toString();
} }
} }

171
spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsSerializer.java

@ -16,8 +16,12 @@
package org.springframework.aot.nativex; package org.springframework.aot.nativex;
import java.util.Iterator; import java.io.StringWriter;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.springframework.aot.hint.ExecutableHint; import org.springframework.aot.hint.ExecutableHint;
@ -27,129 +31,100 @@ import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints; import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeHint; import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.lang.Nullable;
/** /**
* Serialize {@link ReflectionHints} to the JSON file expected by GraalVM {@code native-image} compiler, * Serialize {@link ReflectionHints} to the JSON output expected by the GraalV
* typically named {@code reflect-config.json}. * {@code native-image} compiler, typically named {@code reflect-config.json}.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0 * @since 6.0
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Reflection/">Reflection Use in Native Images</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Reflection/">Reflection Use in Native Images</a>
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
*/ */
@SuppressWarnings("serial")
class ReflectionHintsSerializer { class ReflectionHintsSerializer {
public String serialize(ReflectionHints hints) { public String serialize(ReflectionHints hints) {
StringBuilder builder = new StringBuilder(); StringWriter sw = new StringWriter();
builder.append("[\n"); BasicJsonWriter writer = new BasicJsonWriter(sw, " ");
Iterator<TypeHint> hintIterator = hints.typeHints().iterator(); writer.writeArray(hints.typeHints().map(this::toAttributes).toList());
while (hintIterator.hasNext()) { return sw.toString();
TypeHint hint = hintIterator.next(); }
String name = JsonUtils.escape(hint.getType().getCanonicalName());
builder.append("{\n\"name\": \"").append(name).append("\""); private Map<String, Object> toAttributes(TypeHint hint) {
serializeCondition(hint, builder); Map<String, Object> attributes = new LinkedHashMap<>();
serializeMembers(hint, builder); attributes.put("name", hint.getType().getCanonicalName());
serializeFields(hint, builder); handleCondition(attributes, hint);
serializeExecutables(hint, builder); handleCategories(attributes, hint.getMemberCategories());
builder.append(" }"); handleFields(attributes, hint.fields());
if (hintIterator.hasNext()) { handleExecutables(attributes, Stream.concat(hint.constructors(), hint.methods()).toList());
builder.append(",\n"); return attributes;
}
}
builder.append("\n]");
return builder.toString();
} }
private void serializeCondition(TypeHint hint, StringBuilder builder) { private void handleCondition(Map<String, Object> attributes, TypeHint hint) {
if (hint.getReachableType() != null) { if (hint.getReachableType() != null) {
String name = JsonUtils.escape(hint.getReachableType().getCanonicalName()); Map<String, Object> conditionAttributes = new LinkedHashMap<>();
builder.append(",\n\"condition\": { \"typeReachable\": \"").append(name).append("\" }"); conditionAttributes.put("typeReachable", hint.getReachableType().getCanonicalName());
attributes.put("condition", conditionAttributes);
} }
} }
private void serializeFields(TypeHint hint, StringBuilder builder) { private void handleFields(Map<String, Object> attributes, Stream<FieldHint> fields) {
Iterator<FieldHint> fieldIterator = hint.fields().iterator(); addIfNotEmpty(attributes, "fields", fields.map(this::toAttributes).toList());
if (fieldIterator.hasNext()) {
builder.append(",\n\"fields\": [\n");
while (fieldIterator.hasNext()) {
FieldHint fieldHint = fieldIterator.next();
String name = JsonUtils.escape(fieldHint.getName());
builder.append("{ \"name\": \"").append(name).append("\"");
if (fieldHint.isAllowWrite()) {
builder.append(", \"allowWrite\": ").append(fieldHint.isAllowWrite());
}
if (fieldHint.isAllowUnsafeAccess()) {
builder.append(", \"allowUnsafeAccess\": ").append(fieldHint.isAllowUnsafeAccess());
}
builder.append(" }");
if (fieldIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n]");
}
} }
private void serializeExecutables(TypeHint hint, StringBuilder builder) { private Map<String, Object> toAttributes(FieldHint hint) {
List<ExecutableHint> executables = Stream.concat(hint.constructors(), hint.methods()).toList(); Map<String, Object> attributes = new LinkedHashMap<>();
Iterator<ExecutableHint> methodIterator = executables.stream().filter(h -> h.getModes().contains(ExecutableMode.INVOKE) || h.getModes().isEmpty()).iterator(); attributes.put("name", hint.getName());
Iterator<ExecutableHint> queriedMethodIterator = executables.stream().filter(h -> h.getModes().contains(ExecutableMode.INTROSPECT)).iterator(); if (hint.isAllowWrite()) {
if (methodIterator.hasNext()) { attributes.put("allowWrite", hint.isAllowWrite());
builder.append(",\n");
serializeMethods("methods", methodIterator, builder);
} }
if (queriedMethodIterator.hasNext()) { if (hint.isAllowUnsafeAccess()) {
builder.append(",\n"); attributes.put("allowUnsafeAccess", hint.isAllowUnsafeAccess());
serializeMethods("queriedMethods", queriedMethodIterator, builder);
} }
return attributes;
} }
private void serializeMethods(String fieldName, Iterator<ExecutableHint> methodIterator, StringBuilder builder) { private void handleExecutables(Map<String, Object> attributes, List<ExecutableHint> hints) {
builder.append("\"").append(JsonUtils.escape(fieldName)).append("\": [\n"); addIfNotEmpty(attributes, "methods", hints.stream()
while (methodIterator.hasNext()) { .filter(h -> h.getModes().contains(ExecutableMode.INVOKE) || h.getModes().isEmpty())
ExecutableHint hint = methodIterator.next(); .map(this::toAttributes).toList());
String name = JsonUtils.escape(hint.getName()); addIfNotEmpty(attributes, "queriedMethods", hints.stream()
builder.append("{\n\"name\": \"").append(name).append("\", ").append("\"parameterTypes\": [ "); .filter(h -> h.getModes().contains(ExecutableMode.INTROSPECT))
Iterator<TypeReference> parameterIterator = hint.getParameterTypes().iterator(); .map(this::toAttributes).toList());
while (parameterIterator.hasNext()) {
String parameterName = JsonUtils.escape(parameterIterator.next().getCanonicalName());
builder.append("\"").append(parameterName).append("\"");
if (parameterIterator.hasNext()) {
builder.append(", ");
}
}
builder.append(" ] }\n");
if (methodIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("]\n");
} }
private void serializeMembers(TypeHint hint, StringBuilder builder) { private Map<String, Object> toAttributes(ExecutableHint hint) {
Iterator<MemberCategory> categoryIterator = hint.getMemberCategories().iterator(); Map<String, Object> attributes = new LinkedHashMap<>();
if (categoryIterator.hasNext()) { attributes.put("name", hint.getName());
builder.append(",\n"); attributes.put("parameterTypes", hint.getParameterTypes().stream().map(TypeReference::getCanonicalName).toList());
while (categoryIterator.hasNext()) { return attributes;
switch (categoryIterator.next()) { }
case PUBLIC_FIELDS -> builder.append("\"allPublicFields\": true");
case DECLARED_FIELDS -> builder.append("\"allDeclaredFields\": true"); private void handleCategories(Map<String, Object> attributes, Set<MemberCategory> categories) {
case INTROSPECT_PUBLIC_CONSTRUCTORS -> builder.append("\"queryAllPublicConstructors\": true"); categories.forEach(category -> {
case INTROSPECT_DECLARED_CONSTRUCTORS -> builder.append("\"queryAllDeclaredConstructors\": true"); switch (category) {
case INVOKE_PUBLIC_CONSTRUCTORS -> builder.append("\"allPublicConstructors\": true"); case PUBLIC_FIELDS -> attributes.put("allPublicFields", true);
case INVOKE_DECLARED_CONSTRUCTORS -> builder.append("\"allDeclaredConstructors\": true"); case DECLARED_FIELDS -> attributes.put("allDeclaredFields", true);
case INTROSPECT_PUBLIC_METHODS -> builder.append("\"queryAllPublicMethods\": true"); case INTROSPECT_PUBLIC_CONSTRUCTORS -> attributes.put("queryAllPublicConstructors", true);
case INTROSPECT_DECLARED_METHODS -> builder.append("\"queryAllDeclaredMethods\": true"); case INTROSPECT_DECLARED_CONSTRUCTORS -> attributes.put("queryAllDeclaredConstructors", true);
case INVOKE_PUBLIC_METHODS -> builder.append("\"allPublicMethods\": true"); case INVOKE_PUBLIC_CONSTRUCTORS -> attributes.put("allPublicConstructors", true);
case INVOKE_DECLARED_METHODS -> builder.append("\"allDeclaredMethods\": true"); case INVOKE_DECLARED_CONSTRUCTORS -> attributes.put("allDeclaredConstructors", true);
case PUBLIC_CLASSES -> builder.append("\"allPublicClasses\": true"); case INTROSPECT_PUBLIC_METHODS -> attributes.put("queryAllPublicMethods", true);
case DECLARED_CLASSES -> builder.append("\"allDeclaredClasses\": true"); case INTROSPECT_DECLARED_METHODS -> attributes.put("queryAllDeclaredMethods", true);
} case INVOKE_PUBLIC_METHODS -> attributes.put("allPublicMethods", true);
if (categoryIterator.hasNext()) { case INVOKE_DECLARED_METHODS -> attributes.put("allDeclaredMethods", true);
builder.append(",\n"); case PUBLIC_CLASSES -> attributes.put("allPublicClasses", true);
case DECLARED_CLASSES -> attributes.put("allDeclaredClasses", true);
}
} }
} );
}
private void addIfNotEmpty(Map<String, Object> attributes, String name, @Nullable Object value) {
if (value != null && (value instanceof Collection<?> collection && !collection.isEmpty())) {
attributes.put(name, value);
} }
} }

100
spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsSerializer.java

@ -16,20 +16,27 @@
package org.springframework.aot.nativex; package org.springframework.aot.nativex;
import java.io.StringWriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.aot.hint.ResourceBundleHint; import org.springframework.aot.hint.ResourceBundleHint;
import org.springframework.aot.hint.ResourceHints; import org.springframework.aot.hint.ResourceHints;
import org.springframework.aot.hint.ResourcePatternHint; import org.springframework.aot.hint.ResourcePatternHint;
import org.springframework.lang.Nullable;
/** /**
* Serialize a {@link ResourceHints} to the JSON file expected by GraalVM {@code native-image} compiler, * Serialize a {@link ResourceHints} to the JSON output expected by the GraalVM
* typically named {@code resource-config.json}. * {@code native-image} compiler, typically named {@code resource-config.json}.
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Stephane Nicoll
* @since 6.0 * @since 6.0
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Resources/">Accessing Resources in Native Images</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Resources/">Accessing Resources in Native Images</a>
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a> * @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
@ -37,71 +44,48 @@ import org.springframework.aot.hint.ResourcePatternHint;
class ResourceHintsSerializer { class ResourceHintsSerializer {
public String serialize(ResourceHints hints) { public String serialize(ResourceHints hints) {
StringBuilder builder = new StringBuilder(); StringWriter out = new StringWriter();
builder.append("{\n\"resources\" : {\n"); BasicJsonWriter writer = new BasicJsonWriter(out, "\t");
serializeInclude(hints, builder); Map<String, Object> attributes = new LinkedHashMap<>();
serializeExclude(hints, builder); attributes.put("resources", toAttributes(hints));
builder.append("},\n"); handleResourceBundles(attributes, hints.resourceBundles());
serializeBundles(hints, builder); writer.writeObject(attributes);
builder.append("}\n"); return out.toString();
return builder.toString();
} }
private void serializeInclude(ResourceHints hints, StringBuilder builder) { private Map<String, Object> toAttributes(ResourceHints hint) {
builder.append("\"includes\" : [\n"); Map<String, Object> attributes = new LinkedHashMap<>();
Iterator<ResourcePatternHint> patternIterator = hints.resourcePatterns().iterator(); addIfNotEmpty(attributes, "includes", hint.resourcePatterns().map(ResourcePatternHint::getIncludes)
while (patternIterator.hasNext()) { .flatMap(List::stream).distinct().map(this::toAttributes).toList());
ResourcePatternHint hint = patternIterator.next(); addIfNotEmpty(attributes, "excludes", hint.resourcePatterns().map(ResourcePatternHint::getExcludes)
Iterator<String> includeIterator = hint.getIncludes().iterator(); .flatMap(List::stream).distinct().map(this::toAttributes).toList());
while (includeIterator.hasNext()) { return attributes;
String pattern = JsonUtils.escape(patternToRegexp(includeIterator.next()));
builder.append("{ \"pattern\": \"").append(pattern).append("\" }");
if (includeIterator.hasNext()) {
builder.append(", ");
}
}
if (patternIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n],\n");
} }
private void serializeExclude(ResourceHints hints, StringBuilder builder) { private void handleResourceBundles(Map<String, Object> attributes, Stream<ResourceBundleHint> ressourceBundles) {
builder.append("\"excludes\" : [\n"); addIfNotEmpty(attributes, "bundles", ressourceBundles.map(this::toAttributes).toList());
Iterator<ResourcePatternHint> patternIterator = hints.resourcePatterns().iterator();
while (patternIterator.hasNext()) {
ResourcePatternHint hint = patternIterator.next();
Iterator<String> excludeIterator = hint.getExcludes().iterator();
while (excludeIterator.hasNext()) {
String pattern = JsonUtils.escape(patternToRegexp(excludeIterator.next()));
builder.append("{ \"pattern\": \"").append(pattern).append("\" }");
if (excludeIterator.hasNext()) {
builder.append(", ");
}
}
if (patternIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n]\n");
} }
private void serializeBundles(ResourceHints hints, StringBuilder builder) { private Map<String, Object> toAttributes(ResourceBundleHint hint) {
builder.append("\"bundles\" : [\n"); Map<String, Object> attributes = new LinkedHashMap<>();
Iterator<ResourceBundleHint> bundleIterator = hints.resourceBundles().iterator(); attributes.put("name", hint.getBaseName());
while (bundleIterator.hasNext()) { return attributes;
String baseName = JsonUtils.escape(bundleIterator.next().getBaseName()); }
builder.append("{ \"name\": \"").append(baseName).append("\" }");
if (bundleIterator.hasNext()) { private Map<String, Object> toAttributes(String pattern) {
builder.append(",\n"); Map<String, Object> attributes = new LinkedHashMap<>();
} attributes.put("pattern", patternToRegexp(pattern));
} return attributes;
builder.append("]\n");
} }
private String patternToRegexp(String pattern) { private String patternToRegexp(String pattern) {
return Arrays.stream(pattern.split("\\*")).map(Pattern::quote).collect(Collectors.joining(".*")); return Arrays.stream(pattern.split("\\*")).map(Pattern::quote).collect(Collectors.joining(".*"));
} }
private void addIfNotEmpty(Map<String, Object> attributes, String name, @Nullable Object value) {
if (value != null && (value instanceof Collection<?> collection && !collection.isEmpty())) {
attributes.put(name, value);
}
}
} }

Loading…
Cancel
Save