Search in sources :

Example 1 with InvalidCommandLineException

use of com.google.devtools.build.buildjar.InvalidCommandLineException in project bazel by bazelbuild.

the class BlazeJavacMain method compile.

public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
    List<String> javacArguments = arguments.javacOptions();
    try {
        javacArguments = processPluginArgs(arguments.plugins(), javacArguments);
    } catch (InvalidCommandLineException e) {
        return BlazeJavacResult.error(e.getMessage());
    }
    Context context = new Context();
    setupBlazeJavaCompiler(arguments.plugins(), context);
    boolean ok = false;
    StringWriter errOutput = new StringWriter();
    // TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
    // it and handling exceptions directly in callers.
    PrintWriter errWriter = new PrintWriter(errOutput);
    Listener diagnostics = new Listener(context);
    BlazeJavaCompiler compiler;
    try (JavacFileManager fileManager = new ClassloaderMaskingFileManager()) {
        JavacTask task = JavacTool.create().getTask(errWriter, fileManager, diagnostics, javacArguments, ImmutableList.of(), /*classes*/
        fileManager.getJavaFileObjectsFromPaths(arguments.sourceFiles()), context);
        if (arguments.processors() != null) {
            task.setProcessors(arguments.processors());
        }
        fileManager.setContext(context);
        setLocations(fileManager, arguments);
        try {
            ok = task.call();
        } catch (PropagatedException e) {
            throw e.getCause();
        }
    } catch (Throwable t) {
        t.printStackTrace(errWriter);
        ok = false;
    } finally {
        compiler = (BlazeJavaCompiler) JavaCompiler.instance(context);
        if (ok) {
            // or empty source files.
            if (compiler.skippedFlowEvents() > 0 && compiler.flowEvents() == 0) {
                errWriter.println("Expected at least one FLOW event");
                ok = false;
            }
        }
    }
    errWriter.flush();
    return new BlazeJavacResult(ok, filterDiagnostics(diagnostics.build()), errOutput.toString(), compiler);
}
Also used : Context(com.sun.tools.javac.util.Context) Listener(com.google.devtools.build.buildjar.javac.FormattedDiagnostic.Listener) PropagatedException(com.sun.tools.javac.util.PropagatedException) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) StringWriter(java.io.StringWriter) InvalidCommandLineException(com.google.devtools.build.buildjar.InvalidCommandLineException) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter)

Aggregations

InvalidCommandLineException (com.google.devtools.build.buildjar.InvalidCommandLineException)1 Listener (com.google.devtools.build.buildjar.javac.FormattedDiagnostic.Listener)1 JavacTask (com.sun.source.util.JavacTask)1 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)1 Context (com.sun.tools.javac.util.Context)1 PropagatedException (com.sun.tools.javac.util.PropagatedException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1