Search in sources :

Example 1 with JavacTask

use of org.eclipse.ceylon.langtools.source.util.JavacTask 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)

Example 2 with JavacTask

use of org.eclipse.ceylon.langtools.source.util.JavacTask in project ceylon by eclipse.

the class CeylonDocToolTests method compileJavaModule.

private void compileJavaModule(String pathname, String... fileNames) throws Exception {
    CeyloncTool compiler = new CeyloncTool();
    List<String> options = Arrays.asList("-src", pathname, "-out", "build/ceylon-cars", "-sysrep", "../dist/dist/repo", "-cp", CompilerTests.getClassPathAsPath());
    JavacFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    List<String> qualifiedNames = new ArrayList<String>(fileNames.length);
    for (String name : fileNames) {
        qualifiedNames.add(pathname + File.separator + name);
    }
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromStrings(qualifiedNames);
    JavacTask task = compiler.getTask(null, null, null, options, null, fileObjects);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
}
Also used : JavacFileManager(org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager) CeyloncTool(org.eclipse.ceylon.compiler.java.tools.CeyloncTool) ArrayList(java.util.ArrayList) JavacTask(org.eclipse.ceylon.langtools.source.util.JavacTask)

Example 3 with JavacTask

use of org.eclipse.ceylon.langtools.source.util.JavacTask in project ceylon by eclipse.

the class CeylonDocToolTests method compile.

private void compile(String pathname, String destDir, String moduleName) throws Exception {
    CeyloncTool compiler = new CeyloncTool();
    List<String> options = Arrays.asList("-src", pathname, "-out", destDir, "-sysrep", "../dist/dist/repo", "-cp", CompilerTests.getClassPathAsPath());
    JavacTask task = compiler.getTask(null, null, null, options, Arrays.asList(moduleName), null);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
}
Also used : CeyloncTool(org.eclipse.ceylon.compiler.java.tools.CeyloncTool) JavacTask(org.eclipse.ceylon.langtools.source.util.JavacTask)

Example 4 with JavacTask

use of org.eclipse.ceylon.langtools.source.util.JavacTask in project ceylon by eclipse.

the class JavacTrees method init.

private void init(Context context) {
    attr = Attr.instance(context);
    enter = Enter.instance(context);
    elements = JavacElements.instance(context);
    log = Log.instance(context);
    resolve = Resolve.instance(context);
    treeMaker = TreeMaker.instance(context);
    memberEnter = MemberEnter.instance(context);
    names = Names.instance(context);
    types = Types.instance(context);
    JavacTask t = context.get(JavacTask.class);
    if (t instanceof JavacTaskImpl)
        javacTaskImpl = (JavacTaskImpl) t;
}
Also used : JavacTask(org.eclipse.ceylon.langtools.source.util.JavacTask)

Aggregations

JavacTask (org.eclipse.ceylon.langtools.source.util.JavacTask)4 CeyloncTool (org.eclipse.ceylon.compiler.java.tools.CeyloncTool)2 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)2 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 JavaFileManager (org.eclipse.ceylon.javax.tools.JavaFileManager)1 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)1 Plugin (org.eclipse.ceylon.langtools.source.util.Plugin)1 BasicJavacTask (org.eclipse.ceylon.langtools.tools.javac.api.BasicJavacTask)1 AnnotationProcessingError (org.eclipse.ceylon.langtools.tools.javac.processing.AnnotationProcessingError)1 JavacProcessingEnvironment (org.eclipse.ceylon.langtools.tools.javac.processing.JavacProcessingEnvironment)1