Search in sources :

Example 1 with JavaCompiler

use of org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class JavacProcessingEnvironment method initProcessorClassLoader.

private void initProcessorClassLoader() {
    JavaFileManager fileManager = context.get(JavaFileManager.class);
    try {
        // If processorpath is not explicitly set, use the classpath.
        processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) : fileManager.getClassLoader(CLASS_PATH);
        if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
            JavaCompiler compiler = JavaCompiler.instance(context);
            compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
        }
    } catch (SecurityException e) {
        processorClassLoaderException = e;
    }
}
Also used : JavaFileManager(org.eclipse.ceylon.javax.tools.JavaFileManager) StandardJavaFileManager(org.eclipse.ceylon.javax.tools.StandardJavaFileManager) Closeable(java.io.Closeable) JavaCompiler(org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)

Example 2 with JavaCompiler

use of org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class LanguageCompiler method instance.

/**
 * Get the JavaCompiler instance for this context.
 */
public static JavaCompiler instance(Context context) {
    Options options = Options.instance(context);
    options.put("-Xprefer", "source");
    // make sure it's registered
    // Log log = CeylonLog.instance(context);
    CeylonEnter.instance(context);
    CeylonClassWriter.instance(context);
    JavaCompiler instance = context.get(compilerKey);
    if (instance == null)
        instance = new LanguageCompiler(context);
    return instance;
}
Also used : Options(org.eclipse.ceylon.langtools.tools.javac.util.Options) JavaCompiler(org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)

Example 3 with JavaCompiler

use of org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class Main method compile.

/**
 * Programmatic interface for main function.
 * @param args The command line parameters.
 */
@Override
public Result compile(String[] args, String[] classNames, Context context, List<JavaFileObject> fileObjects, Iterable<? extends Processor> processors) {
    context.put(Log.outKey, out);
    log = CeylonLog.instance(context);
    if (options == null) {
        // creates a new one
        options = Options.instance(context);
    }
    filenames = new LinkedHashSet<File>();
    classnames = new ListBuffer<String>();
    exitState = ExitState.cmderror();
    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()) {
            // super.help();
            this.exitState = ExitState.cmderror();
            return CMDERR;
        }
        Collection<File> filenames = processArgs(CommandLine.parse(args), classNames);
        if (filenames == null) {
            // null signals an error in options, abort
            this.exitState = ExitState.cmderror();
            return 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 OK;
            error("err.no.source.files");
            this.exitState = ExitState.cmderror();
            return 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);
        try {
            comp = LanguageCompiler.instance(context);
        } catch (Overrides.OverrideException e) {
            CeylonLog.instance(context).error("ceylon.overrides", e.getMessage());
            this.exitState = new ExitState(ERROR, CeylonState.ERROR, 0, e);
            return CMDERR;
        }
        if (comp == null) {
            this.exitState = ExitState.systemError(null, null);
            return SYSERR;
        }
        if (!classnames.isEmpty()) {
            this.filenames.addAll(addModuleFiles(filenames));
            classnames.clear();
        }
        if (!this.filenames.isEmpty()) {
            // add filenames to fileObjects
            List<JavaFileObject> otherFiles = List.nil();
            JavacFileManager dfm = (JavacFileManager) fileManager;
            for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(this.filenames)) {
                otherFiles = otherFiles.append(fo);
            }
            fileObjects = fileObjects.prependList(otherFiles);
        }
        if (fileObjects.isEmpty()) {
            error("err.no.source.files");
            this.exitState = ExitState.cmderror();
            return 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 ERROR;
        }
    } catch (IOException ex) {
        ioMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return SYSERR;
    } catch (OutOfMemoryError ex) {
        resourceMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return SYSERR;
    } catch (StackOverflowError ex) {
        resourceMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return SYSERR;
    } catch (FatalError ex) {
        this.exitState = ExitState.systemError(comp, ex);
        if (this.exitState.javacExitCode == SYSERR) {
            feMessage(ex);
        }
        return this.exitState.javacExitCode;
    } catch (AnnotationProcessingError ex) {
        apMessage(ex);
        this.exitState = ExitState.systemError(null, ex);
        return SYSERR;
    } catch (ClientCodeException ex) {
        // and javax.tools.JavaCompiler.CompilationTask#call
        throw new RuntimeException(ex.getCause());
    } catch (PropagatedException ex) {
        throw ex.getCause();
    } catch (RepositoryException ex) {
        // this should have logged an error, if so fine. if not we will have a problematic error code
        this.exitState = ExitState.abnormal(comp, ex, options);
        return ABNORMAL;
    } 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 ABNORMAL;
    } finally {
        if (comp != null)
            comp.close();
        filenames = null;
        options = null;
        if (timer != null) {
            timer.end();
        }
        timer = null;
    }
    this.exitState = ExitState.ok();
    return OK;
}
Also used : PropagatedException(org.eclipse.ceylon.langtools.tools.javac.util.PropagatedException) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) FatalError(org.eclipse.ceylon.langtools.tools.javac.util.FatalError) PrintWriter(java.io.PrintWriter) JavaFileManager(org.eclipse.ceylon.javax.tools.JavaFileManager) JavaCompiler(org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler) RepositoryException(org.eclipse.ceylon.model.cmr.RepositoryException) IOException(java.io.IOException) JavacFileManager(org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager) AnnotationProcessingError(org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError) ClientCodeException(org.eclipse.ceylon.langtools.tools.javac.util.ClientCodeException) Overrides(org.eclipse.ceylon.cmr.api.Overrides) File(java.io.File)

Example 4 with JavaCompiler

use of org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler in project ceylon by eclipse.

the class JavacProcessingEnvironment method doProcessing.

// TODO: internal catch clauses?; catch and rethrow an annotation
// processing error
public JavaCompiler doProcessing(Context context, List<JCCompilationUnit> roots, List<ClassSymbol> classSymbols, Iterable<? extends PackageSymbol> pckSymbols, Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
    log = Log.instance(context);
    Set<PackageSymbol> specifiedPackages = new LinkedHashSet<PackageSymbol>();
    for (PackageSymbol psym : pckSymbols) specifiedPackages.add(psym);
    this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages);
    Round round = new Round(context, roots, classSymbols, deferredDiagnosticHandler);
    boolean errorStatus;
    boolean moreToDo;
    do {
        // Run processors for round n
        round.run(false, false);
        // Processors for round n have run to completion.
        // Check for errors and whether there is more work to do.
        errorStatus = round.unrecoverableError();
        moreToDo = moreToDo();
        round.showDiagnostics(errorStatus || showResolveErrors);
        // Set up next round.
        // Copy mutable collections returned from filer.
        round = round.next(new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects()), new LinkedHashMap<String, JavaFileObject>(filer.getGeneratedClasses()));
        // Check for errors during setup.
        if (round.unrecoverableError())
            errorStatus = true;
    } while (moreToDo && !errorStatus);
    // run last round
    round.run(true, errorStatus);
    round.showDiagnostics(true);
    filer.warnIfUnclosedFiles();
    warnIfUnmatchedOptions();
    /*
         * If an annotation processor raises an error in a round,
         * that round runs to completion and one last round occurs.
         * The last round may also occur because no more source or
         * class files have been generated.  Therefore, if an error
         * was raised on either of the last *two* rounds, the compile
         * should exit with a nonzero exit code.  The current value of
         * errorStatus holds whether or not an error was raised on the
         * second to last round; errorRaised() gives the error status
         * of the last round.
         */
    if (messager.errorRaised() || werror && round.warningCount() > 0 && round.errorCount() > 0)
        errorStatus = true;
    Set<JavaFileObject> newSourceFiles = new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects());
    roots = cleanTrees(round.roots);
    JavaCompiler compiler = round.finalCompiler();
    // Ceylon: we need to call parseFiles even if we did not add anything, to reset
    // module stuff
    // if (newSourceFiles.size() > 0)
    roots = roots.appendList(compiler.parseFiles(newSourceFiles));
    errorStatus = errorStatus || (compiler.errorCount() > 0);
    // Free resources
    this.close();
    if (!taskListener.isEmpty())
        taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
    if (errorStatus) {
        if (compiler.errorCount() == 0)
            compiler.log.nerrors++;
        return compiler;
    }
    compiler.enterTreesIfNeeded(roots);
    return compiler;
}
Also used : JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) JavaCompiler(org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)

Aggregations

JavaCompiler (org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)4 JavaFileManager (org.eclipse.ceylon.javax.tools.JavaFileManager)2 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)2 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Overrides (org.eclipse.ceylon.cmr.api.Overrides)1 StandardJavaFileManager (org.eclipse.ceylon.javax.tools.StandardJavaFileManager)1 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)1 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)1 AnnotationProcessingError (org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError)1 ClientCodeException (org.eclipse.ceylon.langtools.tools.javac.util.ClientCodeException)1 FatalError (org.eclipse.ceylon.langtools.tools.javac.util.FatalError)1 Options (org.eclipse.ceylon.langtools.tools.javac.util.Options)1 PropagatedException (org.eclipse.ceylon.langtools.tools.javac.util.PropagatedException)1 RepositoryException (org.eclipse.ceylon.model.cmr.RepositoryException)1