public Compiler(Flags... flags) { setFlags(flags); this.tempDirs = new ArrayList<>(); this.postprocessors = new ArrayList<>(); this.systemJavaCompiler = ToolProvider.getSystemJavaCompiler(); this.fm = systemJavaCompiler.getStandardFileManager(null, null, null); }
public TestInput( Iterable<? extends JavaFileObject> files, Iterable<String> processors, String[] options) { this.compiler = ToolProvider.getSystemJavaCompiler(); this.fileManager = compiler.getStandardFileManager(null, null, null); this.files = files; this.processors = processors; this.options = new LinkedList<String>(); String classpath = System.getProperty("tests.classpath", "tests" + File.separator + "build"); String globalclasspath = System.getProperty("java.class.path", ""); this.options.add("-Xmaxerrs"); this.options.add("9999"); this.options.add("-g"); this.options.add("-d"); this.options.add(OUTDIR); this.options.add("-classpath"); this.options.add( "build" + File.pathSeparator + "junit.jar" + File.pathSeparator + classpath + File.pathSeparator + globalclasspath); this.options.addAll(Arrays.asList(options)); }
public static void main(String... args) { try { JavaCompiler javac = javax.tools.ToolProvider.getSystemJavaCompiler(); DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { public void report(Diagnostic<? extends JavaFileObject> message) { throw new NullPointerException(SILLY_BILLY); } }; StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromStrings(Arrays.asList("badfile.java")); javac.getTask(null, fm, dl, null, null, files).call(); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause instanceof NullPointerException && cause.getMessage().equals(SILLY_BILLY)) return; throw new Error("unexpected exception caught: " + e); } catch (Throwable t) { throw new Error("unexpected exception caught: " + t); } throw new Error("no exception caught"); }
/** * 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; } */ }