Search in sources :

Example 41 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

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 42 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

the class T6993301 method testExceptionParameterCorrectKind.

public void testExceptionParameterCorrectKind() throws IOException {
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
    final JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ct.analyze();
    new TreePathScanner<Void, Void>() {

        @Override
        public Void visitVariable(VariableTree node, Void p) {
            Element el = Trees.instance(ct).getElement(getCurrentPath());
            assertNotNull(el);
            assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
            return super.visitVariable(node, p);
        }
    }.scan(cut, null);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree) JavaCompiler(javax.tools.JavaCompiler)

Example 43 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

the class T6557752 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    task.analyze();
    trees = Trees.instance(task);
    MyVisitor myVisitor = new MyVisitor();
    for (CompilationUnitTree ast : asts) {
        myVisitor.compilationUnit = ast;
        myVisitor.scan(ast, null);
    }
    if (!myVisitor.foundError) {
        throw new AssertionError("Expected error not found!");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler)

Example 44 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

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)

Example 45 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.

the class T6472751 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    trees = Trees.instance(task);
    positions = trees.getSourcePositions();
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    for (CompilationUnitTree ast : asts) {
        new MyVisitor().scan(ast, null);
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)56 JavacTask (com.sun.source.util.JavacTask)24 JavaCompiler (javax.tools.JavaCompiler)21 JavaFileObject (javax.tools.JavaFileObject)16 Tree (com.sun.source.tree.Tree)15 ClassTree (com.sun.source.tree.ClassTree)14 VariableTree (com.sun.source.tree.VariableTree)10 JavacTool (com.sun.tools.javac.api.JavacTool)9 JCTree (com.sun.tools.javac.tree.JCTree)9 IOException (java.io.IOException)9 MethodTree (com.sun.source.tree.MethodTree)8 ArrayList (java.util.ArrayList)8 Trees (com.sun.source.util.Trees)7 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)6 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 StandardJavaFileManager (javax.tools.StandardJavaFileManager)6 BinaryTree (com.sun.source.tree.BinaryTree)5 ExpressionTree (com.sun.source.tree.ExpressionTree)5 IdentifierTree (com.sun.source.tree.IdentifierTree)5 TreePath (com.sun.source.util.TreePath)5