Search in sources :

Example 31 with CompilationUnitTree

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

use of com.sun.source.tree.CompilationUnitTree in project vertx-docgen by vert-x3.

the class JavaDocGenerator method renderSource.

public String renderSource(TreePath path, List<? extends Tree> trees, String source) {
    CompilationUnitTree unit = path.getCompilationUnit();
    int from = (int) docTrees.getSourcePositions().getStartPosition(unit, trees.get(0));
    int to = (int) docTrees.getSourcePositions().getEndPosition(unit, trees.get(trees.size() - 1));
    // Correct boundaries
    while (from > 1 && source.charAt(from - 1) != '\n') {
        from--;
    }
    while (to < source.length() && source.charAt(to) != '\n') {
        to++;
    }
    String block = source.substring(from, to);
    // Determine margin
    int blockMargin = Integer.MAX_VALUE;
    LineMap lineMap = unit.getLineMap();
    for (Tree statement : trees) {
        int statementStart = (int) docTrees.getSourcePositions().getStartPosition(unit, statement);
        int lineStart = statementStart;
        while (lineMap.getLineNumber(statementStart) == lineMap.getLineNumber(lineStart - 1)) {
            lineStart--;
        }
        blockMargin = Math.min(blockMargin, statementStart - lineStart);
    }
    // Crop the fragment
    StringBuilder fragment = new StringBuilder();
    for (Iterator<String> sc = new Scanner(block).useDelimiter("\n"); sc.hasNext(); ) {
        String line = sc.next();
        int margin = Math.min(blockMargin, line.length());
        line = line.substring(margin);
        fragment.append(line);
        if (sc.hasNext()) {
            fragment.append('\n');
        }
    }
    return fragment.toString();
}
Also used : Scanner(java.util.Scanner) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) LineMap(com.sun.source.tree.LineMap)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)32 JavacTask (com.sun.source.util.JavacTask)11 JavaCompiler (javax.tools.JavaCompiler)11 ClassTree (com.sun.source.tree.ClassTree)9 Tree (com.sun.source.tree.Tree)9 ArrayList (java.util.ArrayList)8 JavaFileObject (javax.tools.JavaFileObject)8 IOException (java.io.IOException)7 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 MethodTree (com.sun.source.tree.MethodTree)3 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