|
1 | 1 | package liquidjava.playground; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
| 4 | +import com.google.gson.reflect.TypeToken; |
4 | 5 | import java.nio.file.Files; |
5 | 6 | import java.nio.file.Path; |
6 | 7 | import java.util.ArrayList; |
7 | 8 | import java.util.LinkedHashMap; |
| 9 | +import java.util.List; |
8 | 10 | import java.util.Map; |
9 | 11 | import liquidjava.diagnostics.Diagnostics; |
10 | 12 | import liquidjava.diagnostics.LJDiagnostic; |
|
26 | 28 | import spoon.support.compiler.jdt.JDTBasedSpoonCompiler; |
27 | 29 |
|
28 | 30 | public final class BrowserRunner { |
29 | | - public static String verify(String source, String jar, String javaBase) { |
| 31 | + public static String verify(String sources, String jar, String javaBase) { |
30 | 32 | Map<String, Object> result = new LinkedHashMap<>(); |
31 | 33 | ArrayList<Map<String, Object>> issues = new ArrayList<>(); |
32 | 34 | try { |
33 | 35 | Path dir = Path.of("/files/playground"); |
34 | 36 | Files.createDirectories(dir); |
35 | | - Path input = dir.resolve("Example.java"); |
36 | | - Files.writeString(input, source); |
| 37 | + try (var previous = Files.list(dir)) { |
| 38 | + for (Path file : previous.toList()) Files.delete(file); |
| 39 | + } |
| 40 | + Map<String, String> files = new Gson().fromJson(sources, new TypeToken<Map<String, String>>() {}.getType()); |
| 41 | + if (files == null || files.isEmpty()) throw new IllegalArgumentException("No Java files supplied"); |
| 42 | + List<String> inputs = new ArrayList<>(); |
| 43 | + for (var file : files.entrySet()) { |
| 44 | + if (!file.getKey().matches("[A-Za-z_$][A-Za-z0-9_$]*\\.java")) |
| 45 | + throw new IllegalArgumentException("Invalid Java filename"); |
| 46 | + Path input = dir.resolve(file.getKey()); |
| 47 | + Files.writeString(input, file.getValue()); |
| 48 | + inputs.add(input.toString()); |
| 49 | + } |
37 | 50 | Diagnostics.getInstance().clear(); |
38 | 51 | ContextHistory.getInstance().clearHistory(); |
39 | 52 | Launcher launcher = new Launcher(); |
40 | | - launcher.addInputResource(input.toString()); |
| 53 | + launcher.addInputResource(dir.toString()); |
41 | 54 | launcher.getEnvironment().setNoClasspath(true); |
42 | 55 | launcher.getEnvironment().setComplianceLevel(8); |
43 | 56 | launcher.getEnvironment().setSourceClasspath(new String[] {jar}); |
44 | 57 | JDTBuilder builder = new JDTBuilderImpl() |
45 | 58 | .classpathOptions(new ClasspathOptions().classpath(jar).bootclasspath(javaBase)) |
46 | 59 | .complianceOptions(new ComplianceOptions().compliance(8)) |
47 | 60 | .advancedOptions(new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()) |
48 | | - .sources(new SourceOptions().sources(input.toString())); |
| 61 | + .sources(new SourceOptions().sources(inputs.toArray(String[]::new))); |
49 | 62 | JDTBasedSpoonCompiler compiler = (JDTBasedSpoonCompiler) launcher.getModelBuilder(); |
50 | 63 | boolean built = compiler.build(builder); |
51 | 64 | for (CategorizedProblem problem : compiler.getProblems()) { |
52 | 65 | if (!problem.isError()) continue; |
53 | 66 | Map<String, Object> issue = new LinkedHashMap<>(); |
54 | 67 | issue.put("severity", "error"); |
| 68 | + issue.put("title", "Java error"); |
| 69 | + issue.put("message", problem.getMessage()); |
55 | 70 | issue.put("output", problem.toString()); |
56 | 71 | issue.put("line", problem.getSourceLineNumber()); |
57 | 72 | issue.put("from", problem.getSourceStart()); |
@@ -84,6 +99,8 @@ public static String verify(String source, String jar, String javaBase) { |
84 | 99 | private static Map<String, Object> issue(LJDiagnostic issue, String severity) { |
85 | 100 | Map<String, Object> result = new LinkedHashMap<>(); |
86 | 101 | result.put("severity", severity); |
| 102 | + result.put("title", issue.getTitle()); |
| 103 | + result.put("message", issue.getMessage()); |
87 | 104 | result.put("output", issue.toString()); |
88 | 105 | if (issue.getPosition() != null && issue.getPosition().isValidPosition()) { |
89 | 106 | result.put("line", issue.getPosition().getLine()); |
|
0 commit comments