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()); } }
/** * Compiles the input file and generates the output class file. * * @param fullFilePath input .java file path. * @return class file path. */ public static String compileFile(File fullFilePath) throws IOException { Preconditions.checkArgument(fullFilePath != null && fullFilePath.isFile()); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); Preconditions.checkNotNull(compiler); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(fullFilePath)); List<String> args = Arrays.asList("-Xlint:none"); boolean result = compiler.getTask(null, fileManager, diagnostics, args, null, compilationUnit).call(); String msg = ""; if (!result) { for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { msg += String.format( "Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getMessage(null)); } tracer.err(msg); } fileManager.close(); return msg; }
public TestRun run() { StringWriter output = new StringWriter(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); if (debug) { System.out.printf( "TestInput.run:%n options: %s%n processors: %s%n files: %s%n", this.options, this.processors, this.files); } JavaCompiler.CompilationTask task = compiler.getTask( output, fileManager, diagnostics, this.options, this.processors, this.files); /* * In Eclipse, std out and std err for multiple tests appear as one * long stream. When selecting a specific failed test, one sees the * expected/unexpected messages, but not the std out/err messages from * that particular test. Can we improve this somehow? */ Boolean result = task.call(); return new TestRun(result, output.toString(), diagnostics.getDiagnostics()); }
@NotNull private static String errorsToString( @NotNull DiagnosticCollector<JavaFileObject> diagnosticCollector, boolean humanReadable) { StringBuilder builder = new StringBuilder(); for (javax.tools.Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) { if (diagnostic.getKind() != javax.tools.Diagnostic.Kind.ERROR) continue; if (humanReadable) { builder.append(diagnostic).append("\n"); } else { builder .append(diagnostic.getSource().getName()) .append(":") .append(diagnostic.getLineNumber()) .append(":") .append(diagnostic.getColumnNumber()) .append(":") .append(diagnostic.getCode()) .append("\n"); } } return builder.toString(); }