Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.source.util;
28 import java.util.Iterator;
30 import com.sun.source.tree.*;
32 /**
33 * A path of tree nodes, typically used to represent the sequence of ancestor
34 * nodes of a tree node up to the top level CompilationUnitTree node.
35 *
36 * @author Jonathan Gibbons
37 * @since 1.6
38 */
39 public class TreePath implements Iterable<Tree> {
40 /**
41 * Gets a tree path for a tree node within a compilation unit.
42 * @return null if the node is not found
43 */
44 public static TreePath getPath(CompilationUnitTree unit, Tree target) {
45 return getPath(new TreePath(unit), target);
46 }
48 /**
49 * Gets a tree path for a tree node within a subtree identified by a TreePath object.
50 * @return null if the node is not found
51 */
52 public static TreePath getPath(TreePath path, Tree target) {
53 path.getClass();
54 target.getClass();
56 class Result extends Error {
57 static final long serialVersionUID = -5942088234594905625L;
58 TreePath path;
59 Result(TreePath path) {
60 this.path = path;
61 }
62 }
63 class PathFinder extends TreePathScanner<TreePath,Tree> {
64 public TreePath scan(Tree tree, Tree target) {
65 if (tree == target)
66 throw new Result(new TreePath(getCurrentPath(), target));
67 return super.scan(tree, target);
68 }
69 }
71 try {
72 new PathFinder().scan(path, target);
73 } catch (Result result) {
74 return result.path;
75 }
76 return null;
77 }
79 /**
80 * Creates a TreePath for a root node.
81 */
82 public TreePath(CompilationUnitTree t) {
83 this(null, t);
84 }
86 /**
87 * Creates a TreePath for a child node.
88 */
89 public TreePath(TreePath p, Tree t) {
90 if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {
91 compilationUnit = (CompilationUnitTree) t;
92 parent = null;
93 }
94 else {
95 compilationUnit = p.compilationUnit;
96 parent = p;
97 }
98 leaf = t;
99 }
100 /**
101 * Get the compilation unit associated with this path.
102 */
103 public CompilationUnitTree getCompilationUnit() {
104 return compilationUnit;
105 }
107 /**
108 * Get the leaf node for this path.
109 */
110 public Tree getLeaf() {
111 return leaf;
112 }
114 /**
115 * Get the path for the enclosing node, or null if there is no enclosing node.
116 */
117 public TreePath getParentPath() {
118 return parent;
119 }
121 public Iterator<Tree> iterator() {
122 return new Iterator<Tree>() {
123 public boolean hasNext() {
124 return next != null;
125 }
127 public Tree next() {
128 Tree t = next.leaf;
129 next = next.parent;
130 return t;
131 }
133 public void remove() {
134 throw new UnsupportedOperationException();
135 }
137 private TreePath next = TreePath.this;
138 };
139 }
141 private CompilationUnitTree compilationUnit;
142 private Tree leaf;
143 private TreePath parent;
144 }