Search in sources :

Example 41 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class DisjunctiveTypeWellFormednessTest method run.

void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source));
    ct.analyze();
    check();
}
Also used : JavacTask(com.sun.source.util.JavacTask)

Example 42 with JavacTask

use of com.sun.source.util.JavacTask 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 43 with JavacTask

use of com.sun.source.util.JavacTask 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 44 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Example 45 with JavacTask

use of com.sun.source.util.JavacTask 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)

Aggregations

JavacTask (com.sun.source.util.JavacTask)46 JavaCompiler (javax.tools.JavaCompiler)20 JavacTool (com.sun.tools.javac.api.JavacTool)12 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)10 JavaFileObject (javax.tools.JavaFileObject)9 StandardJavaFileManager (javax.tools.StandardJavaFileManager)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 Context (com.sun.tools.javac.util.Context)5 File (java.io.File)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)4 ClassTree (com.sun.source.tree.ClassTree)3 Trees (com.sun.source.util.Trees)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 CeyloncTool (com.redhat.ceylon.compiler.java.tools.CeyloncTool)2 Tree (com.sun.source.tree.Tree)2