Search in sources :

Example 11 with CompilationUnitTree

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

the class GenStubs method makeStub.

void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
    CompilationUnitTree tree2 = new StubMaker().translate(tree);
    CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
    String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
    JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, className, JavaFileObject.Kind.SOURCE, null);
    // System.err.println("Writing " + className + " to " + fo.getName());
    Writer out = fo.openWriter();
    try {
        new Pretty(out, true).printExpr((JCTree) tree3);
    } finally {
        out.close();
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Pretty(com.sun.tools.javac.tree.Pretty)

Example 12 with CompilationUnitTree

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

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)

Example 13 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree 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 14 with CompilationUnitTree

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

the class T6404194 method main.

public static void main(String... args) throws IOException {
    class MyFileObject extends SimpleJavaFileObject {

        MyFileObject() {
            super(URI.create("myfo:///Test.java"), SOURCE);
        }

        @Override
        public String getCharContent(boolean ignoreEncodingErrors) {
            //      01234567890123456 7890 123456789012345
            return "@SuppressWarning(\"foo\") @Deprecated class Test { Test() { } }";
        }
    }
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
    JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
    Trees trees = Trees.instance(task);
    CompilationUnitTree toplevel = task.parse().iterator().next();
    ClassTree classTree = (ClassTree) toplevel.getTypeDecls().get(0);
    List<? extends Tree> annotations = classTree.getModifiers().getAnnotations();
    Tree tree1 = annotations.get(0);
    Tree tree2 = annotations.get(1);
    long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1);
    if (pos != 0)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree1);
    if (pos != 23)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getStartPosition(toplevel, tree2);
    if (pos != 24)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree2, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree2);
    if (pos != 35)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree2, pos));
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Trees(com.sun.source.util.Trees) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) JavacTask(com.sun.source.util.JavacTask)

Example 15 with CompilationUnitTree

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

the class StatusPrinterTaskListener method started.

@Override
public void started(TaskEvent e) {
    CompilationUnitTree compilationUnit = e.getCompilationUnit();
    String path = e.getSourceFile().toUri().toString();
    sp.clearLine();
    switch(e.getKind()) {
        case ANALYZE:
            sp.log("Javac [typecheck]: " + path);
            break;
        case ENTER:
            sp.log("Javac [enter]: " + path);
            break;
        case GENERATE:
            sp.log("Javac [generate]: " + path);
            break;
        case PARSE:
            // do not log parsing for Ceylon files
            if (compilationUnit instanceof CeylonCompilationUnit == false)
                sp.log("Javac [parse]: " + path);
            break;
        default:
            break;
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)30 JavacTask (com.sun.source.util.JavacTask)11 JavaCompiler (javax.tools.JavaCompiler)11 ClassTree (com.sun.source.tree.ClassTree)8 Tree (com.sun.source.tree.Tree)8 ArrayList (java.util.ArrayList)8 JavaFileObject (javax.tools.JavaFileObject)8 IOException (java.io.IOException)6 JavacTool (com.sun.tools.javac.api.JavacTool)5 JCTree (com.sun.tools.javac.tree.JCTree)5 Trees (com.sun.source.util.Trees)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 VariableTree (com.sun.source.tree.VariableTree)3 TreeScanner (com.sun.source.util.TreeScanner)3 File (java.io.File)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 StandardJavaFileManager (javax.tools.StandardJavaFileManager)3 ImmutableList (com.google.common.collect.ImmutableList)2 InputFile (com.google.devtools.j2objc.file.InputFile)2 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)2