|
| 1 | +package com.codingame.codemachine.compiler.java; |
| 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 | + |
| 8 | +import javax.tools.Diagnostic; |
| 9 | +import javax.tools.Diagnostic.Kind; |
| 10 | +import javax.tools.DiagnosticCollector; |
| 11 | +import javax.tools.JavaCompiler; |
| 12 | +import javax.tools.JavaFileObject; |
| 13 | +import javax.tools.StandardJavaFileManager; |
| 14 | +import javax.tools.ToolProvider; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.OutputStream; |
| 17 | +import java.io.PrintStream; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static java.util.Collections.singletonList; |
| 22 | + |
| 23 | +public class CodinGameJavaCompiler { |
| 24 | + |
| 25 | + private static class NullOutputStream extends OutputStream { |
| 26 | + @Override |
| 27 | + public void write(int b) throws IOException { |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String... args) throws IOException { |
| 32 | + PrintStream realOut = System.out; |
| 33 | + System.setOut(new PrintStream(new NullOutputStream(), true)); |
| 34 | + System.setErr(new PrintStream(new NullOutputStream(), true)); |
| 35 | + |
| 36 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 37 | + DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>(); |
| 38 | + StandardJavaFileManager fileManager = |
| 39 | + compiler.getStandardFileManager(diagnosticsCollector, null, null); |
| 40 | + |
| 41 | + List<String> files = new ArrayList<>(); |
| 42 | + List<String> options = new ArrayList<>(); |
| 43 | + |
| 44 | + |
| 45 | + for (int i = 0; i < args.length; ++i) { |
| 46 | + String arg = args[i]; |
| 47 | + |
| 48 | + if (arg.startsWith("-")) { |
| 49 | + int paramCount = compiler.isSupportedOption(arg); |
| 50 | + if (paramCount < 0) { |
| 51 | + paramCount = fileManager.isSupportedOption(arg); |
| 52 | + } |
| 53 | + if (paramCount < 0) { |
| 54 | + // unsupported, let javacTask show the error |
| 55 | + paramCount = 0; |
| 56 | + } |
| 57 | + options.add(arg); |
| 58 | + for (int j = 0; j < paramCount; ++j, i++) { |
| 59 | + options.add(args[i + 1]); |
| 60 | + } |
| 61 | + } |
| 62 | + else { |
| 63 | + files.add(arg); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (!files.isEmpty()) { |
| 68 | + List<CompilationLogDto> logs = new ArrayList<>(); |
| 69 | + |
| 70 | + Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files); |
| 71 | + JavaCompiler.CompilationTask task = |
| 72 | + compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits); |
| 73 | + boolean success = task.call(); |
| 74 | + for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) { |
| 75 | + switch (diagnostic.getKind()) { |
| 76 | + case ERROR: |
| 77 | + case WARNING: |
| 78 | + CompilationLogDto log = new CompilationLogDto(); |
| 79 | + log.setFilename(diagnostic.getSource().getName()); |
| 80 | + log.setLine(diagnostic.getLineNumber()); |
| 81 | + log.setColumn(diagnostic.getColumnNumber()); |
| 82 | + log.setMessage(diagnostic.getMessage(null)); |
| 83 | + log.setKind(diagnostic.getKind() == Kind.ERROR ? CompilationLogKind.ERROR : CompilationLogKind.WARNING); |
| 84 | + logs.add(log); |
| 85 | + break; |
| 86 | + default: |
| 87 | + // nothing |
| 88 | + } |
| 89 | + } |
| 90 | + fileManager.close(); |
| 91 | + |
| 92 | + CompilationResult result = new CompilationResult(); |
| 93 | + result.setSuccess(success); |
| 94 | + result.setLogs(logs); |
| 95 | + realOut.println(new Gson().toJson(result)); |
| 96 | + System.exit(success ? 0 : 1); |
| 97 | + } |
| 98 | + else { |
| 99 | + CompilationResult result = new CompilationResult(); |
| 100 | + result.setSuccess(false); |
| 101 | + CompilationLogDto log = new CompilationLogDto(); |
| 102 | + log.setKind(CompilationLogKind.ERROR); |
| 103 | + log.setMessage("no source file"); |
| 104 | + result.setLogs(singletonList(log)); |
| 105 | + realOut.println(new Gson().toJson(result)); |
| 106 | + System.exit(2); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments