Search in sources :

Example 1 with AnnotationProcessingError

use of org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError 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 2 with AnnotationProcessingError

use of org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError in project ceylon by eclipse.

the class Main method compile.

public Result compile(String[] args, String[] classNames, Context context, List<JavaFileObject> fileObjects, Iterable<? extends Processor> processors) {
    context.put(Log.outKey, out);
    log = Log.instance(context);
    if (options == null)
        // creates a new one
        options = Options.instance(context);
    filenames = new LinkedHashSet<File>();
    classnames = new ListBuffer<String>();
    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 && (classNames == null || classNames.length == 0) && fileObjects.isEmpty()) {
            Option.HELP.process(optionHelper, "-help");
            return Result.CMDERR;
        }
        Collection<File> files;
        try {
            files = processArgs(CommandLine.parse(args), classNames);
            if (files == null) {
                // null signals an error in options, abort
                return Result.CMDERR;
            } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
                // it is allowed to compile nothing if just asking for help or version info
                if (options.isSet(HELP) || options.isSet(X) || options.isSet(VERSION) || options.isSet(FULLVERSION))
                    return Result.OK;
                if (JavaCompiler.explicitAnnotationProcessingRequested(options)) {
                    error("err.no.source.files.classes");
                } else {
                    error("err.no.source.files");
                }
                return Result.CMDERR;
            }
        } catch (java.io.FileNotFoundException e) {
            warning("err.file.not.found", e.getMessage());
            return Result.SYSERR;
        }
        boolean forceStdOut = options.isSet("stdout");
        if (forceStdOut) {
            log.flush();
            log.setWriters(new PrintWriter(System.out, true));
        }
        // allow System property in following line as a Mustang legacy
        boolean batchMode = (options.isUnset("nonBatchMode") && System.getProperty("nonBatchMode") == null);
        if (batchMode)
            CacheFSInfo.preRegister(context);
        // FIXME: this code will not be invoked if using JavacTask.parse/analyze/generate
        // invoke any available plugins
        String plugins = options.get(PLUGIN);
        if (plugins != null) {
            JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context);
            ClassLoader cl = pEnv.getProcessorClassLoader();
            ServiceLoader<Plugin> sl = ServiceLoader.load(Plugin.class, cl);
            Set<List<String>> pluginsToCall = new LinkedHashSet<List<String>>();
            for (String plugin : plugins.split("\\x00")) {
                pluginsToCall.add(List.from(plugin.split("\\s+")));
            }
            JavacTask task = null;
            Iterator<Plugin> iter = sl.iterator();
            while (iter.hasNext()) {
                Plugin plugin = iter.next();
                for (List<String> p : pluginsToCall) {
                    if (plugin.getName().equals(p.head)) {
                        pluginsToCall.remove(p);
                        try {
                            if (task == null)
                                task = JavacTask.instance(pEnv);
                            plugin.init(task, p.tail.toArray(new String[p.tail.size()]));
                        } catch (Throwable ex) {
                            if (apiMode)
                                throw new RuntimeException(ex);
                            pluginMessage(ex);
                            return Result.SYSERR;
                        }
                    }
                }
            }
            for (List<String> p : pluginsToCall) {
                log.printLines(PrefixKind.JAVAC, "msg.plugin.not.found", p.head);
            }
        }
        comp = JavaCompiler.instance(context);
        fileManager = context.get(JavaFileManager.class);
        if (!files.isEmpty()) {
            // add filenames to fileObjects
            comp = JavaCompiler.instance(context);
            List<JavaFileObject> otherFiles = List.nil();
            JavacFileManager dfm = (JavacFileManager) fileManager;
            for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) otherFiles = otherFiles.prepend(fo);
            for (JavaFileObject fo : otherFiles) fileObjects = fileObjects.prepend(fo);
        }
        comp.compile(fileObjects, classnames.toList(), processors);
        if (log.expectDiagKeys != null) {
            if (log.expectDiagKeys.isEmpty()) {
                log.printRawLines("all expected diagnostics found");
                return Result.OK;
            } else {
                log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys);
                return Result.ERROR;
            }
        }
        if (comp.errorCount() != 0)
            return Result.ERROR;
    } catch (IOException ex) {
        ioMessage(ex);
        return Result.SYSERR;
    } catch (OutOfMemoryError ex) {
        resourceMessage(ex);
        return Result.SYSERR;
    } catch (StackOverflowError ex) {
        resourceMessage(ex);
        return Result.SYSERR;
    } catch (FatalError ex) {
        feMessage(ex);
        return Result.SYSERR;
    } catch (AnnotationProcessingError ex) {
        if (apiMode)
            throw new RuntimeException(ex.getCause());
        apMessage(ex);
        return Result.SYSERR;
    } catch (ClientCodeException ex) {
        // and org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask#call
        throw new RuntimeException(ex.getCause());
    } catch (PropagatedException ex) {
        throw ex.getCause();
    } catch (Throwable ex) {
        // exceptions.
        if (comp == null || comp.errorCount() == 0 || options == null || options.isSet("dev"))
            bugMessage(ex);
        return Result.ABNORMAL;
    } finally {
        if (comp != null) {
            try {
                comp.close();
            } catch (ClientCodeException ex) {
                throw new RuntimeException(ex.getCause());
            }
        }
        filenames = null;
        options = null;
    }
    return Result.OK;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) JavacTask(org.eclipse.ceylon.langtools.source.util.JavacTask) BasicJavacTask(org.eclipse.ceylon.langtools.tools.javac.api.BasicJavacTask) PrintWriter(java.io.PrintWriter) JavaFileManager(org.eclipse.ceylon.javax.tools.JavaFileManager) IOException(java.io.IOException) JavacFileManager(org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager) AnnotationProcessingError(org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError) JavacProcessingEnvironment(org.eclipse.ceylon.langtools.tools.javac.processing.JavacProcessingEnvironment) File(java.io.File) Plugin(org.eclipse.ceylon.langtools.source.util.Plugin)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 JavaFileManager (org.eclipse.ceylon.javax.tools.JavaFileManager)2 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)2 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)2 AnnotationProcessingError (org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError)2 LinkedHashSet (java.util.LinkedHashSet)1 Overrides (org.eclipse.ceylon.cmr.api.Overrides)1 JavacTask (org.eclipse.ceylon.langtools.source.util.JavacTask)1 Plugin (org.eclipse.ceylon.langtools.source.util.Plugin)1 BasicJavacTask (org.eclipse.ceylon.langtools.tools.javac.api.BasicJavacTask)1 JavaCompiler (org.eclipse.ceylon.langtools.tools.javac.main.JavaCompiler)1 JavacProcessingEnvironment (org.eclipse.ceylon.langtools.tools.javac.processing.JavacProcessingEnvironment)1 ClientCodeException (org.eclipse.ceylon.langtools.tools.javac.util.ClientCodeException)1 FatalError (org.eclipse.ceylon.langtools.tools.javac.util.FatalError)1 PropagatedException (org.eclipse.ceylon.langtools.tools.javac.util.PropagatedException)1 RepositoryException (org.eclipse.ceylon.model.cmr.RepositoryException)1