Search in sources :

Example 11 with JavacTool

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

the class Test method test.

void test(File test) throws Exception {
    JavacTool tool1 = JavacTool.create();
    StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
    // parse test file into a tree, and write it out to a stringbuffer using Pretty
    JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Iterable<? extends CompilationUnitTree> trees = t1.parse();
    for (CompilationUnitTree tree : trees) {
        new Pretty(pw, true).printExpr((JCTree) tree);
    }
    pw.close();
    final String out = sw.toString();
    System.err.println("generated code:\n" + out + "\n");
    // verify the generated code is valid Java by compiling it
    JavacTool tool2 = JavacTool.create();
    JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return out;
        }
    };
    JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
    boolean ok = t2.call();
    if (!ok)
        throw new Exception("compilation of generated code failed");
    File expectedClass = new File(test.getName().replace(".java", ".class"));
    if (!expectedClass.exists())
        throw new Exception(expectedClass + " not found");
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) Pretty(com.sun.tools.javac.tree.Pretty) JavacTask(com.sun.source.util.JavacTask)

Example 12 with JavacTool

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

the class AbstractTreeScannerTest 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
     */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavacTool(com.sun.tools.javac.api.JavacTool) JavacTask(com.sun.source.util.JavacTask)

Example 13 with JavacTool

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

the class TreePosTest 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
     */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavacTool(com.sun.tools.javac.api.JavacTool) JavacTask(com.sun.source.util.JavacTask)

Example 14 with JavacTool

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

the class Anno method run.

void run() throws IOException {
    JavacTool tool = JavacTool.create();
    DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {

        public void report(Diagnostic d) {
            error(d.toString());
        }
    };
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
    Iterable<String> opts = Arrays.asList("-d", ".");
    System.err.println("simple compilation, no processing");
    JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
    task.setTaskListener(new MyTaskListener(task));
    if (!task.call())
        throw new AssertionError("compilation failed");
    opts = Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self);
    System.err.println();
    System.err.println("compilation with processing");
    task = tool.getTask(out, fm, dl, opts, null, files);
    if (!task.call())
        throw new AssertionError("compilation failed");
    if (errors > 0)
        throw new AssertionError(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool)

Example 15 with JavacTool

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

the class T6345974 method main.

public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();
    Scanner s = new Scanner();
    for (CompilationUnitTree t : trees) s.scan(t, null);
}
Also used : TreeScanner(com.sun.source.util.TreeScanner) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) PrintWriter(java.io.PrintWriter)

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