Search in sources :

Example 21 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class Main method compile.

/**
     * Programmatic interface for main function.
     * @param args The command line parameters.
     */
public int compile(String[] args, Context context, List<JavaFileObject> fileObjects, Iterable<? extends Processor> processors) {
    if (options == null) {
        // creates a new one
        options = Options.instance(context);
    }
    filenames = new ListBuffer<File>();
    classnames = new ListBuffer<String>();
    exitState = null;
    JavaCompiler comp = null;
    /* TODO: Logic below about what is an acceptable command line should be
         * updated to take annotation processing semantics into account. */
    try {
        if (args.length == 0 && fileObjects.isEmpty()) {
            help();
            this.exitState = ExitState.cmderror();
            return EXIT_CMDERR;
        }
        List<File> filenames = processArgs(args);
        if (filenames == null) {
            // null signals an error in options, abort
            this.exitState = ExitState.cmderror();
            return EXIT_CMDERR;
        } else if (filenames.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
            // or version info
            if (options.get("-help") != null || options.get("-jhelp") != null || options.get("-X") != null || options.get("-version") != null || options.get("-fullversion") != null)
                return EXIT_OK;
            error("err.no.source.files");
            this.exitState = ExitState.cmderror();
            return EXIT_CMDERR;
        }
        // Set up the timer *after* we've processed to options
        // because it needs to know if we need logging or not
        timer = Timer.instance(context);
        timer.init();
        boolean forceStdOut = options.get("stdout") != null;
        if (forceStdOut) {
            out.flush();
            out = new PrintWriter(System.out, true);
        }
        context.put(Log.outKey, out);
        fileManager = context.get(JavaFileManager.class);
        comp = LanguageCompiler.instance(context);
        if (comp == null) {
            this.exitState = ExitState.systemError(null, null);
            return EXIT_SYSERR;
        }
        if (!classnames.isEmpty()) {
            filenames = addModuleFiles(filenames);
            classnames.clear();
        }
        if (!filenames.isEmpty()) {
            // add filenames to fileObjects
            List<JavaFileObject> otherFiles = List.nil();
            JavacFileManager dfm = (JavacFileManager) fileManager;
            for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(filenames)) {
                otherFiles = otherFiles.append(fo);
            }
            fileObjects = fileObjects.prependList(otherFiles);
        }
        if (fileObjects.isEmpty()) {
            error("err.no.source.files");
            this.exitState = ExitState.cmderror();
            return EXIT_CMDERR;
        }
        comp.compile(fileObjects, classnames.toList(), processors);
        int errorCount = comp.errorCount();
        //ceylonBackendErrors = comp.log instanceof CeylonLog ? ((CeylonLog)comp.log).ceylonBackendErrors() : false;
        if (errorCount != 0) {
            this.exitState = ExitState.error(comp);
            return EXIT_ERROR;
        }
    } catch (IOException ex) {
        ioMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return EXIT_SYSERR;
    } catch (OutOfMemoryError ex) {
        resourceMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return EXIT_SYSERR;
    } catch (StackOverflowError ex) {
        resourceMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return EXIT_SYSERR;
    } catch (FatalError ex) {
        this.exitState = ExitState.systemError(comp, ex);
        if (this.exitState.javacExitCode == EXIT_SYSERR) {
            feMessage(ex);
        }
        return this.exitState.javacExitCode;
    } catch (AnnotationProcessingError ex) {
        apMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return EXIT_SYSERR;
    } catch (ClientCodeException ex) {
        // and javax.tools.JavaCompiler.CompilationTask#call
        throw new RuntimeException(ex.getCause());
    } catch (PropagatedException ex) {
        throw ex.getCause();
    } catch (RepositoryException ex) {
        throw new EnvironmentException(ex);
    } catch (Throwable ex) {
        // exceptions.
        if (comp == null || comp.errorCount() == 0 || options == null || options.get("dev") != null) {
            bugMessage(ex);
        }
        this.exitState = ExitState.abnormal(comp, ex, options);
        return EXIT_ABNORMAL;
    } finally {
        if (comp != null)
            comp.close();
        filenames = null;
        options = null;
        if (timer != null) {
            timer.end();
        }
        timer = null;
    }
    this.exitState = ExitState.ok();
    return EXIT_OK;
}
Also used : JavaFileManager(javax.tools.JavaFileManager) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) PropagatedException(com.sun.tools.javac.util.PropagatedException) RepositoryException(com.redhat.ceylon.model.cmr.RepositoryException) IOException(java.io.IOException) EnvironmentException(com.redhat.ceylon.compiler.EnvironmentException) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) AnnotationProcessingError(com.sun.tools.javac.processing.AnnotationProcessingError) JavaFileObject(javax.tools.JavaFileObject) FatalError(com.sun.tools.javac.util.FatalError) ClientCodeException(com.sun.tools.javac.util.ClientCodeException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 22 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class Main method compile.

/**
     * Programmatic interface for main function.
     * @param args The command line parameters.
     */
public int compile(String[] args) {
    Context context = new Context();
    // can't create it until Log
    CeyloncFileManager.preRegister(context);
    // has been set up
    CeylonLog.preRegister(context);
    int result = compile(args, context);
    if (fileManager instanceof JavacFileManager) {
        // A fresh context was created above, so jfm must be a
        // JavacFileManager
        ((JavacFileManager) fileManager).close();
    }
    return result;
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager)

Example 23 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class Test method createFileManager.

JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
    Context c = new Context();
    Options options = Options.instance(c);
    options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
    if (!useSymbolFile) {
        options.put("ignore.symbol.file", "true");
    }
    return new JavacFileManager(c, false, null);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Options(com.sun.tools.javac.util.Options)

Example 24 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager 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)

Example 25 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project bazel by bazelbuild.

the class ImplicitDependencyExtractor method getPlatformJars.

/** Collect the set of jars on the compilation bootclasspath. */
public static Set<String> getPlatformJars(JavaFileManager fileManager) {
    if (fileManager instanceof JavacFileManager) {
        JavacFileManager jfm = (JavacFileManager) fileManager;
        ImmutableSet.Builder<String> result = ImmutableSet.builder();
        for (Path path : jfm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH)) {
            result.add(path.toString());
        }
        return result.build();
    }
    // implementation that matters.
    throw new IllegalStateException("Unsupported file manager type: " + fileManager.getClass().getName());
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet)

Aggregations

JavacFileManager (com.sun.tools.javac.file.JavacFileManager)35 Context (com.sun.tools.javac.util.Context)19 JavaFileObject (javax.tools.JavaFileObject)10 File (java.io.File)9 IOException (java.io.IOException)7 PrintWriter (java.io.PrintWriter)7 JavaFileManager (javax.tools.JavaFileManager)7 VirtualFile (com.redhat.ceylon.compiler.typechecker.io.VirtualFile)5 Path (java.nio.file.Path)5 JavacTask (com.sun.source.util.JavacTask)4 Options (com.sun.tools.javac.util.Options)4 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedWriter (java.io.BufferedWriter)3 Deps (com.google.devtools.build.lib.view.proto.Deps)2 JavacTool (com.sun.tools.javac.api.JavacTool)2 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)2 AnnotationProcessingError (com.sun.tools.javac.processing.AnnotationProcessingError)2 PropagatedException (com.sun.tools.javac.util.PropagatedException)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 Test (org.junit.Test)2