Search in sources :

Example 1 with AnnotationProcessingError

use of com.sun.tools.javac.processing.AnnotationProcessingError 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>();
    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();
            return EXIT_CMDERR;
        }
        List<File> files;
        try {
            files = processArgs(CommandLine.parse(args));
            if (files == null) {
                // null signals an error in options, abort
                return EXIT_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 EXIT_OK;
                if (JavaCompiler.explicitAnnotationProcessingRequested(options)) {
                    error("err.no.source.files.classes");
                } else {
                    error("err.no.source.files");
                }
                return EXIT_CMDERR;
            }
        } catch (java.io.FileNotFoundException e) {
            Log.printLines(out, ownName + ": " + getLocalizedString("err.file.not.found", e.getMessage()));
            return EXIT_SYSERR;
        }
        boolean forceStdOut = options.isSet("stdout");
        if (forceStdOut) {
            out.flush();
            out = new PrintWriter(System.out, true);
        }
        context.put(Log.outKey, out);
        // allow System property in following line as a Mustang legacy
        boolean batchMode = (options.isUnset("nonBatchMode") && System.getProperty("nonBatchMode") == null);
        if (batchMode)
            CacheFSInfo.preRegister(context);
        fileManager = context.get(JavaFileManager.class);
        comp = JavaCompiler.instance(context);
        if (comp == null)
            return EXIT_SYSERR;
        Log log = Log.instance(context);
        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.printLines(log.noticeWriter, "all expected diagnostics found");
                return EXIT_OK;
            } else {
                Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys);
                return EXIT_ERROR;
            }
        }
        if (comp.errorCount() != 0)
            return EXIT_ERROR;
    } catch (IOException ex) {
        ioMessage(ex);
        return EXIT_SYSERR;
    } catch (OutOfMemoryError ex) {
        resourceMessage(ex);
        return EXIT_SYSERR;
    } catch (StackOverflowError ex) {
        resourceMessage(ex);
        return EXIT_SYSERR;
    } catch (FatalError ex) {
        feMessage(ex);
        return EXIT_SYSERR;
    } catch (AnnotationProcessingError ex) {
        if (apiMode)
            throw new RuntimeException(ex.getCause());
        apMessage(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 (Throwable ex) {
        // exceptions.
        if (comp == null || comp.errorCount() == 0 || options == null || options.isSet("dev"))
            bugMessage(ex);
        return EXIT_ABNORMAL;
    } finally {
        if (comp != null) {
            try {
                comp.close();
            } catch (ClientCodeException ex) {
                throw new RuntimeException(ex.getCause());
            }
        }
        filenames = null;
        options = null;
    }
    return EXIT_OK;
}
Also used : JavaFileManager(javax.tools.JavaFileManager) IOException(java.io.IOException) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) AnnotationProcessingError(com.sun.tools.javac.processing.AnnotationProcessingError) JavaFileObject(javax.tools.JavaFileObject) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with AnnotationProcessingError

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

Aggregations

JavacFileManager (com.sun.tools.javac.file.JavacFileManager)2 AnnotationProcessingError (com.sun.tools.javac.processing.AnnotationProcessingError)2 File (java.io.File)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 JavaFileManager (javax.tools.JavaFileManager)2 JavaFileObject (javax.tools.JavaFileObject)2 EnvironmentException (com.redhat.ceylon.compiler.EnvironmentException)1 RepositoryException (com.redhat.ceylon.model.cmr.RepositoryException)1 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)1 ClientCodeException (com.sun.tools.javac.util.ClientCodeException)1 FatalError (com.sun.tools.javac.util.FatalError)1 PropagatedException (com.sun.tools.javac.util.PropagatedException)1