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 void report(Diagnostic<? extends JavaFileObject> diagnostic) { final CompilerMessage.Kind kind; switch (diagnostic.getKind()) { case ERROR: kind = BuildMessage.Kind.ERROR; myErrorCount++; break; case MANDATORY_WARNING: case WARNING: case NOTE: kind = BuildMessage.Kind.WARNING; myWarningCount++; break; default: kind = BuildMessage.Kind.INFO; } File sourceFile = null; try { // for eclipse compiler just an attempt to call getSource() may lead to an NPE, // so calling this method under try/catch to avoid induced compiler errors final JavaFileObject source = diagnostic.getSource(); sourceFile = source != null ? Utils.convertToFile(source.toUri()) : null; } catch (Exception e) { LOG.info(e); } final String srcPath = sourceFile != null ? FileUtil.toSystemIndependentName(sourceFile.getPath()) : null; String message = diagnostic.getMessage(Locale.US); if (Utils.IS_TEST_MODE) { LOG.info(message); } myContext.processMessage( new CompilerMessage( BUILDER_NAME, kind, message, srcPath, diagnostic.getStartPosition(), diagnostic.getEndPosition(), diagnostic.getPosition(), diagnostic.getLineNumber(), diagnostic.getColumnNumber())); }