Java Code Examples for com.sun.source.util.TreePath#getPath()

The following examples show how to use com.sun.source.util.TreePath#getPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: T6852595.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class BadName { Object o = j; }";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
    CompilationUnitTree cu = compUnits.iterator().next();
    ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
    JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
    TreePath path = TreePath.getPath(cu, vdef.init);
    Trees.instance(ct).getScope(path);
}
 
Example 2
Source File: T6852595.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class BadName { Object o = j; }";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
    CompilationUnitTree cu = compUnits.iterator().next();
    ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
    JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
    TreePath path = TreePath.getPath(cu, vdef.init);
    Trees.instance(ct).getScope(path);
}
 
Example 3
Source File: T6852595.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class BadName { Object o = j; }";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
    CompilationUnitTree cu = compUnits.iterator().next();
    ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
    JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
    TreePath path = TreePath.getPath(cu, vdef.init);
    Trees.instance(ct).getScope(path);
}
 
Example 4
Source File: JavaRefactoringGlobalAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run(CompilationController cc) throws Exception {
    TreePath selectedElement = null;
    cc.toPhase(Phase.RESOLVED);
    selectedElement = cc.getTreeUtilities().pathFor(caret);
    //workaround for issue 89064
    if (selectedElement.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
        List<? extends Tree> decls = cc.getCompilationUnit().getTypeDecls();
        if (!decls.isEmpty()) {
            selectedElement = TreePath.getPath(cc.getCompilationUnit(), decls.get(0));
        }
    }
    ui = delegate.createRefactoringUI(TreePathHandle.create(selectedElement, cc), start, end, cc);
}
 
Example 5
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
    final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
    if (treeTopLevel == null)
        return null;
    return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}
 
Example 6
Source File: TreeInfo.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Return true iff this tree is a child of some annotation. */
public static boolean isInAnnotation(Env<?> env, JCTree tree) {
    TreePath tp = TreePath.getPath(env.toplevel, tree);
    if (tp != null) {
        for (Tree t : tp) {
            if (t.getKind() == Tree.Kind.ANNOTATION)
                return true;
        }
    }
    return false;
}
 
Example 7
Source File: Main.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitAssignment(AssignmentTree node, Void ignored) {
    TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
    if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
        throw new AssertionError(path.getLeaf());
    return null;
}
 
Example 8
Source File: Refactorer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Boolean isRefactorable() {
    prospectives = this.getListRepresentation(loop.getStatement(), true);
    if (prospectives != null && !prospectives.isEmpty()) {
        prospectives.get(prospectives.size() - 1).eagerize();
        if (this.untrasformable) {
            return false;
        }
        for ( int i = 0; i < prospectives.size() - 1; i++) {
            if (!prospectives.get(i).isLazy()) {
                return false;
            }
        }
        hasIterable = false;
        VariableTree var = loop.getVariable();
        TypeElement el = workingCopy.getElements().getTypeElement("java.lang.Iterable"); // NOI18N
        if (el != null) {
            TreePath path = TreePath.getPath(workingCopy.getCompilationUnit(), loop.getExpression());
            TypeMirror m = workingCopy.getTrees().getTypeMirror(path);
            Types types  = workingCopy.getTypes();
            hasIterable = 
                    types.isSubtype(
                        types.erasure(m),
                        types.erasure(el.asType())
                    );
        }
        prospectives = ProspectiveOperation.mergeIntoComposableOperations(prospectives);
        return prospectives != null;

    } else {
        return false;
    }

}
 
Example 9
Source File: JDIWrappersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Element getElement(Tree tree) {
    TreePath expPath = TreePath.getPath(cut, tree);
    Element e = trees.getElement(expPath);
    if (e == null) {
        if (tree instanceof ParenthesizedTree) {
            e = getElement(((ParenthesizedTree) tree).getExpression());
            //if (e == null) {
            //    System.err.println("Have null element for "+tree);
            //}
            //System.err.println("\nHAVE "+e.asType().toString()+" for ParenthesizedTree "+tree);
        }
        else if (tree instanceof TypeCastTree) {
            e = getElement(((TypeCastTree) tree).getType());
            //if (e == null) {
            //    System.err.println("Have null element for "+tree);
            //}
            //System.err.println("\nHAVE "+e.asType().toString()+" for TypeCastTree "+tree);
        }
        else if (tree instanceof AssignmentTree) {
            e = getElement(((AssignmentTree) tree).getVariable());
        }
        else if (tree instanceof ArrayAccessTree) {
            e = getElement(((ArrayAccessTree) tree).getExpression());
            if (e != null) {
                TypeMirror tm = e.asType();
                if (tm.getKind() == TypeKind.ARRAY) {
                    tm = ((ArrayType) tm).getComponentType();
                    e = types.asElement(tm);
                }
            }
            //System.err.println("ArrayAccessTree = "+((ArrayAccessTree) tree).getExpression()+", element = "+getElement(((ArrayAccessTree) tree).getExpression())+", type = "+getElement(((ArrayAccessTree) tree).getExpression()).asType());
        }
    }
    return e;
}
 
Example 10
Source File: Main.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitAssignment(AssignmentTree node, Void ignored) {
    TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
    if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
        throw new AssertionError(path.getLeaf());
    return null;
}
 
Example 11
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitAssignment(AssignmentTree node, Void ignored) {
    TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
    if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
        throw new AssertionError(path.getLeaf());
    return null;
}
 
Example 12
Source File: TreeInfo.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Return true iff this tree is a child of some annotation. */
public static boolean isInAnnotation(Env<?> env, JCTree tree) {
    TreePath tp = TreePath.getPath(env.toplevel, tree);
    if (tp != null) {
        for (Tree t : tp) {
            if (t.getKind() == Tree.Kind.ANNOTATION)
                return true;
        }
    }
    return false;
}
 
Example 13
Source File: PullUpRefactoringUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static TreePath findSelectedClassMemberDeclaration(final TreePath path, final CompilationInfo javac) {
    TreePath currentPath = path;
    TreePath selection = null;
    while (currentPath != null && selection == null) {
        switch (currentPath.getLeaf().getKind()) {
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
            case NEW_CLASS:
            case METHOD:
                selection = currentPath;
                break;
            case VARIABLE:
                Element elm = javac.getTrees().getElement(currentPath);
                if (elm != null && elm.getKind().isField()) {
                    selection = currentPath;
                }
                break;
        }
        if (selection != null && javac.getTreeUtilities().isSynthetic(selection)) {
            selection = null;
        }
        if (selection == null) {
            currentPath = currentPath.getParentPath();
        }
    }
    
    if (selection == null && path != null) {
        List<? extends Tree> typeDecls = path.getCompilationUnit().getTypeDecls();
        if (!typeDecls.isEmpty() && typeDecls.get(0).getKind().asInterface() == ClassTree.class) {
            selection = TreePath.getPath(path.getCompilationUnit(), typeDecls.get(0));
        }
    }
    return selection;
}
 
Example 14
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
    final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
    if (treeTopLevel == null)
        return null;
    return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}
 
Example 15
Source File: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(CompilationUnitTree unit, Tree node) {
    return TreePath.getPath(unit, node);
}
 
Example 16
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(CompilationUnitTree unit, Tree node) {
    return TreePath.getPath(unit, node);
}
 
Example 17
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
    final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
    if (treeTopLevel == null)
        return null;
    return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}
 
Example 18
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(CompilationUnitTree unit, Tree node) {
    return TreePath.getPath(unit, node);
}
 
Example 19
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
    final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
    if (treeTopLevel == null)
        return null;
    return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
}
 
Example 20
Source File: JavacTrees.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public TreePath getPath(CompilationUnitTree unit, Tree node) {
    return TreePath.getPath(unit, node);
}