public static void compileJavaFiles( @NotNull Collection<File> files, List<String> options, @Nullable File javaErrorFile) throws IOException { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager( diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8")); try { Iterable<? extends JavaFileObject> javaFileObjectsFromFiles = fileManager.getJavaFileObjectsFromFiles(files); JavaCompiler.CompilationTask task = javaCompiler.getTask( new StringWriter(), // do not write to System.err fileManager, diagnosticCollector, options, null, javaFileObjectsFromFiles); Boolean success = task.call(); // do NOT inline this variable, call() should complete before // errorsToString() if (javaErrorFile == null || !javaErrorFile.exists()) { Assert.assertTrue(errorsToString(diagnosticCollector, true), success); } else { assertEqualsToFile(javaErrorFile, errorsToString(diagnosticCollector, false)); } } finally { fileManager.close(); } }
public static Map<Lang, Result> convertFromFiles( ClassLoader loader, List<Lang> lang, String file, String fqn, String method) throws Exception { DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); StandardJavaFileManager manager = javac.getStandardFileManager(diagnostics, locale, charset); Iterable<? extends JavaFileObject> fileObjects = manager.getJavaFileObjects(file); StringWriter out = new StringWriter(); JavaCompiler.CompilationTask task = javac.getTask( out, manager, diagnostics, Collections.<String>emptyList(), Collections.<String>emptyList(), fileObjects); task.setLocale(locale); ConvertingProcessor processor = new ConvertingProcessor(lang, fqn, method); task.setProcessors(Collections.<Processor>singletonList(processor)); if (task.call()) { return processor.getResults(); } else { StringWriter message = new StringWriter(); PrintWriter writer = new PrintWriter(message); writer.append("Compilation of ").append(file).println(" failed:"); for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) { writer.append(diagnostic.getMessage(locale)); } writer.println("console:"); writer.append(out.getBuffer()); throw new Exception(message.toString()); } }
public static void main(String... args) throws Throwable { String testSrcDir = System.getProperty("test.src"); String testClassDir = System.getProperty("test.classes"); String self = T6361619.class.getName(); JavacTool tool = JavacTool.create(); final PrintWriter out = new PrintWriter(System.err, true); Iterable<String> flags = Arrays.asList( "-processorpath", testClassDir, "-processor", self, "-d", "."); DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { public void report(Diagnostic<? extends JavaFileObject> m) { out.println(m); } }; StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); Iterable<? extends JavaFileObject> f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); JavacTask task = tool.getTask(out, fm, dl, flags, null, f); MyTaskListener tl = new MyTaskListener(task); task.setTaskListener(tl); // should complete, without exceptions task.call(); }
/** * Wow! much faster than compiling outside of VM. Finicky though. Had rules called r and modulo. * Wouldn't compile til I changed to 'a'. */ protected boolean compile(String fileName) { String classpathOption = "-classpath"; String[] args = new String[] { "javac", "-d", tmpdir, classpathOption, tmpdir + pathSep + CLASSPATH, tmpdir + "/" + fileName }; String cmdLine = "javac" + " -d " + tmpdir + " " + classpathOption + " " + tmpdir + pathSep + CLASSPATH + " " + fileName; // System.out.println("compile: "+cmdLine); File f = new File(tmpdir, fileName); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // DiagnosticCollector<JavaFileObject> diagnostics = // new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f)); Iterable<String> compileOptions = Arrays.asList(new String[] {"-d", tmpdir, "-cp", tmpdir + pathSep + CLASSPATH}); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, compileOptions, null, compilationUnits); boolean ok = task.call(); try { fileManager.close(); } catch (IOException ioe) { ioe.printStackTrace(System.err); } // List<String> errors = new ArrayList<String>(); // for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { // errors.add( // String.valueOf(diagnostic.getLineNumber())+ // ": " + diagnostic.getMessage(null)); // } // if ( errors.size()>0 ) { // System.err.println("compile stderr from: "+cmdLine); // System.err.println(errors); // return false; // } return ok; /* File outputDir = new File(tmpdir); try { Process process = Runtime.getRuntime().exec(args, null, outputDir); StreamVacuum stdout = new StreamVacuum(process.getInputStream()); StreamVacuum stderr = new StreamVacuum(process.getErrorStream()); stdout.start(); stderr.start(); process.waitFor(); stdout.join(); stderr.join(); if ( stdout.toString().length()>0 ) { System.err.println("compile stdout from: "+cmdLine); System.err.println(stdout); } if ( stderr.toString().length()>0 ) { System.err.println("compile stderr from: "+cmdLine); System.err.println(stderr); } int ret = process.exitValue(); return ret==0; } catch (Exception e) { System.err.println("can't exec compilation"); e.printStackTrace(System.err); return false; } */ }