pub const DRIVER_JAVA: &str = r#"package rustanything;
import java.io.BufferedWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** Generated by rust-anything: calls the unit's static methods by name. */
public final class RustAnythingDriver {
private RustAnythingDriver() {}
private static final Map<String, Method> METHODS = new LinkedHashMap<>();
@SuppressWarnings("unchecked")
private static void load(String targetsPath) throws Exception {
Map<String, Object> targets = (Map<String, Object>) RustAnythingJson.parse(Files.readString(Path.of(targetsPath)));
for (Map.Entry<String, Object> e : targets.entrySet()) {
List<Object> t = (List<Object>) e.getValue();
try {
Class<?> c = Class.forName((String) t.get(0));
for (Method m : c.getMethods()) {
if (m.getName().equals(t.get(1)) && Modifier.isStatic(m.getModifiers())) {
METHODS.put(e.getKey(), m);
}
}
} catch (ClassNotFoundException ex) {
// missing: reported per case
}
}
}
/** {"ok": v} | {"error": msg, "error_type": name} | {"invalid": true} | {"missing": true}. */
@SuppressWarnings("unchecked")
private static Map<String, Object> call(Map<String, Object> c) {
Map<String, Object> out = new LinkedHashMap<>();
Method m = METHODS.get((String) c.get("fn"));
if (m == null) {
out.put("missing", true);
return out;
}
List<Object> args = (List<Object>) c.get("args");
Type[] types = m.getGenericParameterTypes();
Object[] values = new Object[types.length];
try {
for (int i = 0; i < types.length; i++) {
Object raw = i < args.size() ? args.get(i) : null;
if (raw instanceof Map && ((Map<?, ?>) raw).containsKey("$undefined")) {
raw = null;
}
values[i] = RustAnythingJson.convert(raw, types[i]);
}
} catch (RustAnythingJson.Mismatch e) {
out.put("invalid", true);
return out;
}
try {
Object result = m.invoke(null, values);
out.put("ok", RustAnythingJson.parse(RustAnythingJson.write(result)));
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
out.put("error", String.valueOf(cause.getMessage()));
out.put("error_type", cause.getClass().getSimpleName());
} catch (ReflectiveOperationException e) {
out.put("error", e.toString());
out.put("error_type", "ReflectiveOperationException");
} catch (StackOverflowError e) {
out.put("error", "stack overflow");
out.put("error_type", "StackOverflowError");
}
return out;
}
@SuppressWarnings("unchecked")
private static List<Map<String, Object>> lines(String path) throws Exception {
List<Map<String, Object>> out = new ArrayList<>();
for (String line : Files.readAllLines(Path.of(path), StandardCharsets.UTF_8)) {
if (!line.isBlank()) {
out.add((Map<String, Object>) RustAnythingJson.parse(line));
}
}
return out;
}
private static boolean same(Map<String, Object> expected, Map<String, Object> got) {
if (String.valueOf(got.get("error")).contains("Rust panic:")) {
return false;
}
if (expected.containsKey("error")) {
return got.containsKey("error") && String.valueOf(expected.get("error_type")).equals(String.valueOf(got.get("error_type")));
}
return expected.containsKey("ok") && got.containsKey("ok")
&& RustAnythingJson.write(expected.get("ok")).equals(RustAnythingJson.write(got.get("ok")));
}
@SuppressWarnings("unchecked")
public static void main(String[] argv) throws Exception {
String mode = argv[0];
load(argv[1]);
String casesPath = argv[2];
Path outPath = Path.of(argv[3]);
if (mode.equals("record")) {
List<Object> cases = (List<Object>) RustAnythingJson.parse(Files.readString(Path.of(casesPath)));
String progress = System.getenv("RUST_ANYTHING_PROGRESS");
try (BufferedWriter w = Files.newBufferedWriter(outPath, StandardCharsets.UTF_8)) {
for (int index = 0; index < cases.size(); index++) {
if (progress != null) {
Files.writeString(Path.of(progress), index + "\n", StandardCharsets.UTF_8,
java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.APPEND);
}
Map<String, Object> c = (Map<String, Object>) cases.get(index);
Map<String, Object> result = call(c);
if (result.containsKey("invalid") || result.containsKey("missing")) {
continue;
}
Map<String, Object> line = new LinkedHashMap<>();
line.put("fn", c.get("fn"));
line.put("args", c.get("args"));
line.putAll(result);
w.write(RustAnythingJson.write(line));
w.write("\n");
w.flush();
}
}
} else if (mode.equals("diff")) {
int total = 0, passed = 0, failed = 0;
List<Object> missing = new ArrayList<>();
List<Object> mismatches = new ArrayList<>();
for (Map<String, Object> c : lines(casesPath)) {
total++;
Map<String, Object> got = call(c);
if (got.containsKey("missing")) {
failed++;
if (!missing.contains(c.get("fn"))) {
missing.add(c.get("fn"));
}
continue;
}
Map<String, Object> expected = new LinkedHashMap<>();
for (String k : new String[] {"ok", "error", "error_type"}) {
if (c.containsKey(k)) {
expected.put(k, c.get(k));
}
}
if (same(expected, got)) {
passed++;
} else {
failed++;
Map<String, Object> m = new LinkedHashMap<>();
m.put("fn", c.get("fn"));
m.put("args", c.get("args"));
m.put("expected", expected);
m.put("actual", got);
mismatches.add(m);
}
}
Map<String, Object> summary = new LinkedHashMap<>();
summary.put("total", total);
summary.put("passed", passed);
summary.put("failed", failed);
summary.put("missing", missing);
summary.put("mismatches", mismatches);
Files.writeString(outPath, RustAnythingJson.write(summary));
} else if (mode.equals("bench")) {
long minMs = Long.parseLong(argv[4]);
Map<String, List<Map<String, Object>>> groups = new LinkedHashMap<>();
for (Map<String, Object> c : lines(casesPath)) {
groups.computeIfAbsent((String) c.get("fn"), k -> new ArrayList<>()).add(c);
}
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, List<Map<String, Object>>> g : groups.entrySet()) {
List<Map<String, Object>> cases = g.getValue();
// Arguments are converted once: only the calls are timed.
Method m = METHODS.get(g.getKey());
if (m == null) {
continue;
}
Type[] types = m.getGenericParameterTypes();
List<Object[]> prepared = new ArrayList<>();
for (Map<String, Object> c : cases) {
List<Object> args = (List<Object>) c.get("args");
Object[] values = new Object[types.length];
try {
for (int i = 0; i < types.length; i++) {
Object raw = i < args.size() ? args.get(i) : null;
if (raw instanceof Map && ((Map<?, ?>) raw).containsKey("$undefined")) {
raw = null;
}
values[i] = RustAnythingJson.convert(raw, types[i]);
}
prepared.add(values);
} catch (RustAnythingJson.Mismatch e) {
// not a valid call
}
}
if (prepared.isEmpty()) {
continue;
}
Runnable pass = () -> {
for (Object[] values : prepared) {
try {
m.invoke(null, values);
} catch (ReflectiveOperationException | RuntimeException e) {
// errors count as calls
}
}
};
// Warm up the JIT before timing.
long warm = System.nanoTime();
while (System.nanoTime() - warm < minMs * 1_000_000L / 2) {
pass.run();
}
long passes = 0;
long start = System.nanoTime();
while (true) {
pass.run();
passes++;
long elapsed = System.nanoTime() - start;
if (elapsed / 1_000_000 >= minMs) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("cases", prepared.size());
row.put("ns", (double) elapsed / (passes * prepared.size()));
result.put(g.getKey(), row);
break;
}
}
}
Files.writeString(outPath, RustAnythingJson.write(result));
}
}
}
"#;Expand description
rustanything/RustAnythingDriver.java: calls static methods by name for record, diff and bench.