|
1 | 1 | package com.codingame.codemachine.compiler.java;
|
2 | 2 |
|
3 |
| -import com.codingame.codemachine.compiler.java.core.CompilationLogDto; |
4 |
| -import com.codingame.codemachine.compiler.java.core.CompilationLogKind; |
5 |
| -import com.codingame.codemachine.compiler.java.core.CompilationResult; |
6 |
| -import com.google.gson.Gson; |
7 |
| -import org.apache.commons.io.FileUtils; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.LineNumberReader; |
| 5 | +import java.io.StringReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
8 | 8 |
|
9 | 9 | import javax.tools.Diagnostic;
|
10 |
| -import javax.tools.Diagnostic.Kind; |
11 | 10 | import javax.tools.DiagnosticCollector;
|
12 | 11 | import javax.tools.JavaCompiler;
|
13 | 12 | import javax.tools.JavaFileObject;
|
14 | 13 | import javax.tools.StandardJavaFileManager;
|
15 | 14 | import javax.tools.ToolProvider;
|
16 |
| -import java.io.File; |
17 |
| -import java.io.IOException; |
18 |
| -import java.io.OutputStream; |
19 |
| -import java.io.PrintStream; |
20 |
| -import java.util.ArrayList; |
21 |
| -import java.util.List; |
22 |
| - |
23 |
| -import static java.util.Collections.singletonList; |
24 | 15 |
|
25 | 16 | public class CodinGameJavaCompiler {
|
26 |
| - private static final String DEFAULT_OUTPUT = "-"; |
27 |
| - |
28 |
| - private static class NullOutputStream extends OutputStream { |
29 |
| - @Override |
30 |
| - public void write(int b) throws IOException { |
31 |
| - } |
32 |
| - } |
33 | 17 |
|
34 | 18 | public static void main(String... args) throws IOException {
|
35 |
| - PrintStream realOut = System.out; |
36 |
| - PrintStream realErr = System.err; |
37 |
| - System.setOut(new PrintStream(new NullOutputStream(), true)); |
38 |
| - System.setErr(new PrintStream(new NullOutputStream(), true)); |
39 |
| - |
40 | 19 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
41 | 20 | DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
|
42 |
| - StandardJavaFileManager fileManager = |
43 |
| - compiler.getStandardFileManager(diagnosticsCollector, null, null); |
44 |
| - |
45 |
| - List<String> files = new ArrayList<>(); |
46 |
| - List<String> options = new ArrayList<>(); |
47 |
| - |
48 |
| - for (int i = 0; i < args.length; ++i) { |
49 |
| - String arg = args[i]; |
| 21 | + int resultCode = 1; |
| 22 | + try(StandardJavaFileManager fileManager = |
| 23 | + compiler.getStandardFileManager(diagnosticsCollector, null, null)) { |
50 | 24 |
|
51 |
| - if (arg.startsWith("-")) { |
52 |
| - int paramCount = compiler.isSupportedOption(arg); |
53 |
| - if (paramCount < 0) { |
54 |
| - paramCount = fileManager.isSupportedOption(arg); |
| 25 | + List<String> files = new ArrayList<>(); |
| 26 | + List<String> options = new ArrayList<>(); |
| 27 | + |
| 28 | + for (int i = 0; i < args.length; ++i) { |
| 29 | + String arg = args[i]; |
| 30 | + |
| 31 | + if (arg.startsWith("-")) { |
| 32 | + int paramCount = compiler.isSupportedOption(arg); |
| 33 | + if (paramCount < 0) { |
| 34 | + paramCount = fileManager.isSupportedOption(arg); |
| 35 | + } |
| 36 | + if (paramCount < 0) { |
| 37 | + // unsupported, let javacTask show the error |
| 38 | + paramCount = 0; |
| 39 | + } |
| 40 | + options.add(arg); |
| 41 | + for (int j = 0; j < paramCount; ++j, i++) { |
| 42 | + options.add(args[i + 1]); |
| 43 | + } |
55 | 44 | }
|
56 |
| - if (paramCount < 0) { |
57 |
| - // unsupported, let javacTask show the error |
58 |
| - paramCount = 0; |
59 |
| - } |
60 |
| - options.add(arg); |
61 |
| - for (int j = 0; j < paramCount; ++j, i++) { |
62 |
| - options.add(args[i + 1]); |
| 45 | + else { |
| 46 | + files.add(arg); |
63 | 47 | }
|
64 | 48 | }
|
65 |
| - else { |
66 |
| - files.add(arg); |
67 |
| - } |
68 |
| - } |
69 |
| - |
70 |
| - CompilationResult result = new CompilationResult(); |
71 |
| - int resultCode = 1; |
72 |
| - if (!files.isEmpty()) { |
73 |
| - List<CompilationLogDto> logs = new ArrayList<>(); |
74 |
| - |
75 |
| - Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files); |
76 |
| - JavaCompiler.CompilationTask task = |
77 |
| - compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits); |
78 |
| - boolean success = task.call(); |
79 |
| - for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) { |
80 |
| - switch (diagnostic.getKind()) { |
| 49 | + |
| 50 | + if (!files.isEmpty()) { |
| 51 | + Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files); |
| 52 | + JavaCompiler.CompilationTask task = |
| 53 | + compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits); |
| 54 | + boolean success = task.call(); |
| 55 | + for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) { |
| 56 | + |
| 57 | + LineNumberReader reader = new LineNumberReader(new StringReader(diagnostic.getSource().getCharContent(true).toString())); |
| 58 | + String line = reader.lines().skip(diagnostic.getLineNumber() - 1).limit(1).findAny().get(); |
| 59 | + |
| 60 | + String type = null; |
| 61 | + switch (diagnostic.getKind()) { |
81 | 62 | case ERROR:
|
| 63 | + type = "ERROR"; |
| 64 | + break; |
82 | 65 | case WARNING:
|
83 |
| - CompilationLogDto log = new CompilationLogDto(); |
84 |
| - log.setFilename(diagnostic.getSource().getName()); |
85 |
| - log.setLine(diagnostic.getLineNumber()); |
86 |
| - log.setColumn(diagnostic.getColumnNumber()); |
87 |
| - log.setMessage(diagnostic.getMessage(null)); |
88 |
| - log.setKind(diagnostic.getKind() == Kind.ERROR ? CompilationLogKind.ERROR : CompilationLogKind.WARNING); |
89 |
| - logs.add(log); |
| 66 | + case MANDATORY_WARNING: |
| 67 | + type = "WARNING"; |
90 | 68 | break;
|
91 |
| - default: |
92 |
| - // nothing |
| 69 | + case NOTE: |
| 70 | + type = "INFO"; |
| 71 | + break; |
| 72 | + case OTHER: |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + System.err.println(String.format("%s:%d: %s: %s\n%s\n%"+diagnostic.getColumnNumber()+"s", |
| 77 | + diagnostic.getSource().getName(), |
| 78 | + diagnostic.getLineNumber(), |
| 79 | + diagnostic.getKind().name().toLowerCase(), |
| 80 | + diagnostic.getMessage(null), |
| 81 | + line, |
| 82 | + "^" |
| 83 | + )); |
| 84 | + |
| 85 | + System.out.println(String.format("CG> annotate --type \"%s\" --file \"%s\" --position \"%s\" --message \"%s\"", |
| 86 | + type, |
| 87 | + diagnostic.getSource().getName(), |
| 88 | + diagnostic.getLineNumber()+":"+diagnostic.getColumnNumber(), |
| 89 | + diagnostic.getMessage(null).replaceAll("\"","\\\"") |
| 90 | + )); |
93 | 91 | }
|
| 92 | + resultCode = success ? 0 : 1; |
94 | 93 | }
|
95 |
| - fileManager.close(); |
96 |
| - |
97 |
| - result.setSuccess(success); |
98 |
| - result.setLogs(logs); |
99 |
| - resultCode = success ? 0 : 1; |
100 |
| - } |
101 |
| - else { |
102 |
| - result.setSuccess(false); |
103 |
| - CompilationLogDto log = new CompilationLogDto(); |
104 |
| - log.setKind(CompilationLogKind.ERROR); |
105 |
| - log.setMessage("no source file"); |
106 |
| - result.setLogs(singletonList(log)); |
107 |
| - resultCode = 2; |
108 |
| - } |
109 |
| - |
110 |
| - String resultOutput = System.getProperty("codingame.compiler.output", DEFAULT_OUTPUT); |
111 |
| - String resultStr = new Gson().toJson(result); |
112 |
| - if (DEFAULT_OUTPUT.equals(resultOutput)) { |
113 |
| - realOut.println(resultStr); |
114 |
| - } |
115 |
| - else { |
116 |
| - try { |
117 |
| - FileUtils.writeStringToFile(new File(resultOutput), resultStr); |
118 |
| - } |
119 |
| - catch (IOException e) { |
120 |
| - realErr.println(e.getMessage()); |
121 |
| - System.exit(3); |
| 94 | + else { |
| 95 | + System.err.println("no source file"); |
| 96 | + resultCode = 2; |
122 | 97 | }
|
123 | 98 | }
|
| 99 | + |
124 | 100 | System.exit(resultCode);
|
125 | 101 | }
|
126 | 102 | }
|
0 commit comments