| 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.source.util; |
| 27 | |
| 28 | import com.sun.source.tree.*; |
| 29 | import java.util.Iterator; |
| 30 | |
| 31 | /** |
| 32 | * A path of tree nodes, typically used to represent the sequence of ancestor |
| 33 | * nodes of a tree node up to the top level CompilationUnitTree node. |
| 34 | * |
| 35 | * @author Jonathan Gibbons |
| 36 | * @since 1.6 |
| 37 | */ |
| 38 | public class TreePath implements Iterable<Tree> { |
| 39 | /** |
| 40 | * Gets a tree path for a tree node within a compilation unit. |
| 41 | * @return null if the node is not found |
| 42 | */ |
| 43 | public static TreePath getPath(CompilationUnitTree unit, Tree target) { |
| 44 | return getPath(new TreePath(unit), target); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Gets a tree path for a tree node within a subtree identified by a TreePath object. |
| 49 | * @return null if the node is not found |
| 50 | */ |
| 51 | public static TreePath getPath(TreePath path, Tree target) { |
| 52 | path.getClass(); |
| 53 | target.getClass(); |
| 54 | |
| 55 | class Result extends Error { |
| 56 | static final long serialVersionUID = -5942088234594905625L; |
| 57 | TreePath path; |
| 58 | Result(TreePath path) { |
| 59 | this.path = path; |
| 60 | } |
| 61 | } |
| 62 | class PathFinder extends TreePathScanner<TreePath,Tree> { |
| 63 | public TreePath scan(Tree tree, Tree target) { |
| 64 | if (tree == target) |
| 65 | throw new Result(new TreePath(getCurrentPath(), target)); |
| 66 | return super.scan(tree, target); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | new PathFinder().scan(path, target); |
| 72 | } catch (Result result) { |
| 73 | return result.path; |
| 74 | } |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a TreePath for a root node. |
| 80 | */ |
| 81 | public TreePath(CompilationUnitTree t) { |
| 82 | this(null, t); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates a TreePath for a child node. |
| 87 | */ |
| 88 | public TreePath(TreePath p, Tree t) { |
| 89 | if (t.getKind() == Tree.Kind.COMPILATION_UNIT) { |
| 90 | compilationUnit = (CompilationUnitTree) t; |
| 91 | parent = null; |
| 92 | } |
| 93 | else { |
| 94 | compilationUnit = p.compilationUnit; |
| 95 | parent = p; |
| 96 | } |
| 97 | leaf = t; |
| 98 | } |
| 99 | /** |
| 100 | * Get the compilation unit associated with this path. |
| 101 | */ |
| 102 | public CompilationUnitTree getCompilationUnit() { |
| 103 | return compilationUnit; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the leaf node for this path. |
| 108 | */ |
| 109 | public Tree getLeaf() { |
| 110 | return leaf; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the path for the enclosing node, or null if there is no enclosing node. |
| 115 | */ |
| 116 | public TreePath getParentPath() { |
| 117 | return parent; |
| 118 | } |
| 119 | |
| 120 | public Iterator<Tree> iterator() { |
| 121 | return new Iterator<Tree>() { |
| 122 | public boolean hasNext() { |
| 123 | return curr.parent != null; |
| 124 | } |
| 125 | |
| 126 | public Tree next() { |
| 127 | curr = curr.parent; |
| 128 | return curr.leaf; |
| 129 | } |
| 130 | |
| 131 | public void remove() { |
| 132 | throw new UnsupportedOperationException(); |
| 133 | } |
| 134 | |
| 135 | private TreePath curr; |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | private CompilationUnitTree compilationUnit; |
| 140 | private Tree leaf; |
| 141 | private TreePath parent; |
| 142 | } |