Search in sources :

Example 16 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class JavadocTool method getRootDocImpl.

public RootDocImpl getRootDocImpl(String doclocale, String encoding, ModifierFilter filter, List<String> javaNames, List<String[]> options, boolean breakiterator, List<String> subPackages, List<String> excludedPackages, boolean docClasses, boolean legacyDoclet, boolean quiet) throws IOException {
    docenv = DocEnv.instance(context);
    docenv.showAccess = filter;
    docenv.quiet = quiet;
    docenv.breakiterator = breakiterator;
    docenv.setLocale(doclocale);
    docenv.setEncoding(encoding);
    docenv.docClasses = docClasses;
    docenv.legacyDoclet = legacyDoclet;
    reader.sourceCompleter = docClasses ? null : this;
    ListBuffer<String> names = new ListBuffer<String>();
    ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<JCCompilationUnit>();
    ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<JCCompilationUnit>();
    try {
        StandardJavaFileManager fm = (StandardJavaFileManager) docenv.fileManager;
        for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) {
            String name = it.head;
            if (!docClasses && name.endsWith(".java") && new File(name).exists()) {
                JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next();
                docenv.notice("main.Loading_source_file", name);
                JCCompilationUnit tree = parse(fo);
                classTrees.append(tree);
            } else if (isValidPackageName(name)) {
                names = names.append(name);
            } else if (name.endsWith(".java")) {
                docenv.error(null, "main.file_not_found", name);
            } else {
                docenv.error(null, "main.illegal_package_name", name);
            }
        }
        if (!docClasses) {
            // Recursively search given subpackages.  If any packages
            //are found, add them to the list.
            Map<String, List<JavaFileObject>> packageFiles = searchSubPackages(subPackages, names, excludedPackages);
            // Parse the packages
            for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) {
                // Parse sources ostensibly belonging to package.
                String packageName = packs.head;
                parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages);
            }
            if (messager.nerrors() != 0)
                return null;
            // Enter symbols for all files
            docenv.notice("main.Building_tree");
            enter.main(classTrees.toList().appendList(packTrees.toList()));
        }
    } catch (Abort ex) {
    }
    if (messager.nerrors() != 0)
        return null;
    if (docClasses)
        return new RootDocImpl(docenv, javaNames, options);
    else
        return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) ListBuffer(com.sun.tools.javac.util.ListBuffer) JavaFileObject(javax.tools.JavaFileObject) Abort(com.sun.tools.javac.util.Abort) StandardJavaFileManager(javax.tools.StandardJavaFileManager) List(com.sun.tools.javac.util.List) File(java.io.File)

Example 17 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class JavahTask method run.

public boolean run() throws Util.Exit {
    Util util = new Util(log, diagnosticListener);
    if (noArgs || help) {
        showHelp();
        // treat noArgs as an error for purposes of exit code
        return help;
    }
    if (version || fullVersion) {
        showVersion(fullVersion);
        return true;
    }
    util.verbose = verbose;
    Gen g;
    if (llni)
        g = new LLNI(doubleAlign, util);
    else {
        //            if (stubs)
        //                throw new BadArgs("jni.no.stubs");
        g = new JNI(util);
    }
    if (ofile != null) {
        if (!(fileManager instanceof StandardJavaFileManager)) {
            diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-o"));
            return false;
        }
        Iterable<? extends JavaFileObject> iter = ((StandardJavaFileManager) fileManager).getJavaFileObjectsFromFiles(Collections.singleton(ofile));
        JavaFileObject fo = iter.iterator().next();
        g.setOutFile(fo);
    } else {
        if (odir != null) {
            if (!(fileManager instanceof StandardJavaFileManager)) {
                diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-d"));
                return false;
            }
            if (!odir.exists())
                if (!odir.mkdirs())
                    util.error("cant.create.dir", odir.toString());
            try {
                ((StandardJavaFileManager) fileManager).setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(odir));
            } catch (IOException e) {
                Object msg = e.getLocalizedMessage();
                if (msg == null) {
                    msg = e;
                }
                diagnosticListener.report(createDiagnostic("err.ioerror", odir, msg));
                return false;
            }
        }
        g.setFileManager(fileManager);
    }
    /*
         * Force set to false will turn off smarts about checking file
         * content before writing.
         */
    g.setForce(force);
    if (fileManager instanceof JavahFileManager)
        ((JavahFileManager) fileManager).setIgnoreSymbolFile(true);
    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    List<String> opts = new ArrayList<String>();
    opts.add("-proc:only");
    opts.addAll(javac_extras);
    CompilationTask t = c.getTask(log, fileManager, diagnosticListener, opts, internalize(classes), null);
    JavahProcessor p = new JavahProcessor(g);
    t.setProcessors(Collections.singleton(p));
    boolean ok = t.call();
    if (p.exit != null)
        throw new Util.Exit(p.exit);
    return ok;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavaFileObject(javax.tools.JavaFileObject)

Example 18 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Example 19 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class Foo method main.

public static void main(String... args) throws IOException {
    String testSrc = System.getProperty("test.src", ".");
    String testClasses = System.getProperty("test.classes", ".");
    JavacTool tool = JavacTool.create();
    MyDiagListener dl = new MyDiagListener();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName() + ".java")));
    JavacTask task = tool.getTask(null, fm, dl, null, null, files);
    task.parse();
    task.analyze();
    task.generate();
    if (dl.notes != 2)
        throw new AssertionError(dl.notes + " notes given");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Example 20 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class T6400872 method compile.

static void compile(File classOutDir, Iterable<File> classPath, File... files) throws IOException {
    System.err.println("compile...");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    try {
        Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
        List<String> options = new ArrayList<String>();
        if (classOutDir != null) {
            options.add("-d");
            options.add(classOutDir.getPath());
        }
        if (classPath != null) {
            options.add("-classpath");
            options.add(join(classPath, File.pathSeparator));
        }
        options.add("-verbose");
        JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null, options, null, fileObjects);
        if (!task.call())
            throw new AssertionError("compilation failed");
    } finally {
        fm.close();
    }
}
Also used : StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)57 JavaCompiler (javax.tools.JavaCompiler)42 JavaFileObject (javax.tools.JavaFileObject)24 File (java.io.File)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 DiagnosticCollector (javax.tools.DiagnosticCollector)15 CompilationTask (javax.tools.JavaCompiler.CompilationTask)9 JavacTask (com.sun.source.util.JavacTask)7 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 JavacTool (com.sun.tools.javac.api.JavacTool)4 FileOutputStream (java.io.FileOutputStream)4 StringWriter (java.io.StringWriter)3 ZipFile (java.util.zip.ZipFile)3 Test (org.junit.Test)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 PrintWriter (java.io.PrintWriter)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2