Search in sources :

Example 1 with JavacTool

use of com.sun.tools.javac.api.JavacTool 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 2 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class T6397044 method main.

public static void main(String[] args) throws Exception {
    String srcDir = System.getProperty("test.src", ".");
    String self = T6397044.class.getName();
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(srcDir, self + ".java")));
    JavacTask task = tool.getTask(null, fm, null, null, null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    Checker checker = new Checker();
    for (CompilationUnitTree tree : trees) checker.check(tree);
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool)

Example 3 with JavacTool

use of com.sun.tools.javac.api.JavacTool 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 4 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class CheckAttributedTree method read.

/**
     * Read a file.
     * @param file the file to be read
     * @return the tree for the content of the file
     * @throws IOException if any IO errors occur
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
     */
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
    JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
    final List<Element> analyzedElems = new ArrayList<>();
    task.setTaskListener(new TaskListener() {

        public void started(TaskEvent e) {
            if (e.getKind() == TaskEvent.Kind.ANALYZE)
                analyzedElems.add(e.getTypeElement());
        }

        public void finished(TaskEvent e) {
        }
    });
    try {
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
        //System.out.println("Try to add pairs. Elems are " + analyzedElems);
        for (CompilationUnitTree t : trees) {
            JCCompilationUnit cu = (JCCompilationUnit) t;
            for (JCTree def : cu.defs) {
                if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
                    //System.out.println("Adding pair...");
                    res.add(new Pair<>(cu, def));
                }
            }
        }
        return res;
    } catch (Throwable t) {
        throw new AttributionException("Exception while attributing file: " + file);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTool(com.sun.tools.javac.api.JavacTool) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) TaskListener(com.sun.source.util.TaskListener) TaskEvent(com.sun.source.util.TaskEvent) JavacTask(com.sun.source.util.JavacTask) Pair(com.sun.tools.javac.util.Pair)

Example 5 with JavacTool

use of com.sun.tools.javac.api.JavacTool in project ceylon-compiler by ceylon.

the class TestDocComments method run.

/**
     * method-run.
     */
void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File file = new File(testSrc, "TestDocComments.java");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects);
    Iterable<? extends CompilationUnitTree> units = task.parse();
    Trees trees = Trees.instance(task);
    CommentScanner s = new CommentScanner();
    int n = s.scan(units, trees);
    if (n != 12)
        error("Unexpected number of doc comments found: " + n);
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool)

Aggregations

JavacTool (com.sun.tools.javac.api.JavacTool)18 JavacTask (com.sun.source.util.JavacTask)11 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)4 File (java.io.File)3 IOException (java.io.IOException)3 StandardJavaFileManager (javax.tools.StandardJavaFileManager)3 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)2 Context (com.sun.tools.javac.util.Context)2 PrintWriter (java.io.PrintWriter)2 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 CharStreams (com.google.common.io.CharStreams)1 Truth.assertAbout (com.google.common.truth.Truth.assertAbout)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)1 SourceFile (com.google.errorprone.apply.SourceFile)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 Fix (com.google.errorprone.fixes.Fix)1 Description (com.google.errorprone.matchers.Description)1