Skip to content

Commit 3f83a51

Browse files
committed
[GR-42811] Use JsonWriter for build output JSON.
PullRequest: graal/13338
2 parents 380851f + 5f0ff21 commit 3f83a51

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

docs/reference-manual/native-image/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ Below, example commands are listed per operating system:
228228
# Ring the terminal bell
229229
native-image -jar App.jar ... ; printf '\a'
230230
231+
# Use libnotify to create a desktop notification
232+
native-image -jar App.jar ... ; notify-send "GraalVM Native Image build completed with exit code $?"
233+
231234
# Use Zenity to open an info dialog box with text
232235
native-image -jar App.jar ... ; zenity --info --text="GraalVM Native Image build completed with exit code $?"
233236
```

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/ExitStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.svm.core.util;
2626

27+
/** Exit status codes to be used at build time (in driver and builder). */
2728
public enum ExitStatus {
2829
OK(0),
2930
BUILDER_ERROR(1),

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/json/JsonWriter.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,8 @@
3030
import java.nio.file.Files;
3131
import java.nio.file.OpenOption;
3232
import java.nio.file.Path;
33+
import java.util.Iterator;
34+
import java.util.Map;
3335

3436
public class JsonWriter implements AutoCloseable {
3537
private final Writer writer;
@@ -54,15 +56,42 @@ public JsonWriter append(String s) throws IOException {
5456
return this;
5557
}
5658

59+
@SuppressWarnings("unchecked")
60+
public void print(Map<String, Object> map) throws IOException {
61+
if (map.isEmpty()) {
62+
append("{}");
63+
return;
64+
}
65+
append('{');
66+
Iterator<String> keySetIter = map.keySet().iterator();
67+
while (keySetIter.hasNext()) {
68+
String key = keySetIter.next();
69+
Object value = map.get(key);
70+
quote(key).append(':');
71+
if (value instanceof Map) {
72+
print((Map<String, Object>) value); // Must always be <String, Object>
73+
} else {
74+
quote(value);
75+
}
76+
if (keySetIter.hasNext()) {
77+
append(',');
78+
}
79+
}
80+
append('}');
81+
}
82+
5783
public JsonWriter quote(Object o) throws IOException {
5884
if (o == null) {
5985
return append("null");
6086
} else if (Boolean.TRUE.equals(o)) {
6187
return append("true");
6288
} else if (Boolean.FALSE.equals(o)) {
6389
return append("false");
90+
} else if (o instanceof Number) {
91+
return append(o.toString());
92+
} else {
93+
return quote(o.toString());
6494
}
65-
return quote(o.toString());
6695
}
6796

6897
public JsonWriter quote(String s) throws IOException {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ProgressReporterJsonHelper.java

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@
2626
package com.oracle.svm.hosted;
2727

2828
import java.io.File;
29-
import java.io.PrintWriter;
29+
import java.io.IOException;
3030
import java.lang.management.ManagementFactory;
3131
import java.lang.management.OperatingSystemMXBean;
3232
import java.nio.file.Path;
3333
import java.util.HashMap;
34-
import java.util.Iterator;
3534
import java.util.Map;
36-
import java.util.function.Consumer;
3735

3836
import com.oracle.graal.pointsto.reports.ReportUtils;
3937
import com.oracle.svm.core.util.VMError;
38+
import com.oracle.svm.core.util.json.JsonWriter;
4039

4140
class ProgressReporterJsonHelper {
4241
protected static final long UNAVAILABLE_METRIC = -1;
@@ -108,51 +107,13 @@ public Path printToFile() {
108107
recordSystemFixedValues();
109108
final File file = new File(jsonOutputFile);
110109
String description = "image statistics in json";
111-
return ReportUtils.report(description, file.getAbsoluteFile().toPath(), getReporter(), false);
112-
}
113-
114-
private Consumer<PrintWriter> getReporter() {
115-
return out -> {
116-
out.println(toJson());
117-
};
118-
}
119-
120-
private String toJson() {
121-
return mapToJson(statsHolder);
122-
}
123-
124-
private String mapToJson(Map<String, Object> map) {
125-
// base case
126-
if (map.isEmpty()) {
127-
return "{}";
128-
}
129-
StringBuilder builder = new StringBuilder();
130-
builder.append("{");
131-
Iterator<String> keySetIter = map.keySet().iterator();
132-
while (keySetIter.hasNext()) {
133-
String key = keySetIter.next();
134-
Object value = map.get(key);
135-
builder.append("\"" + key + "\":");
136-
if (value == null) {
137-
builder.append("null"); // null string
138-
} else if (value instanceof Map) {
139-
// Always a <String, Object> map
140-
@SuppressWarnings("unchecked")
141-
Map<String, Object> subMap = (Map<String, Object>) value;
142-
builder.append(mapToJson(subMap));
143-
} else if (value instanceof String) {
144-
builder.append("\"" + value + "\"");
145-
} else {
146-
assert value instanceof Number;
147-
// Numeric value
148-
builder.append(value);
110+
return ReportUtils.report(description, file.getAbsoluteFile().toPath(), out -> {
111+
try {
112+
new JsonWriter(out).print(statsHolder);
113+
} catch (IOException e) {
114+
throw VMError.shouldNotReachHere("Failed to create " + jsonOutputFile, e);
149115
}
150-
if (keySetIter.hasNext()) {
151-
builder.append(",");
152-
}
153-
}
154-
builder.append("}");
155-
return builder.toString();
116+
}, false);
156117
}
157118

158119
interface JsonMetric {

0 commit comments

Comments
 (0)