author | jjg |
Tue, 19 Mar 2013 15:13:44 -0700 | |
changeset 16559 | e927a4e65b0c |
parent 16303 | b5dca0b42963 |
child 18664 | 5c35de99f6df |
permissions | -rw-r--r-- |
10 | 1 |
/* |
16303
b5dca0b42963
8008267: Add @Supported annotation to com.sun.source types
darcy
parents:
14952
diff
changeset
|
2 |
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.source.util; |
|
27 |
||
14258 | 28 |
import java.util.Iterator; |
29 |
||
10 | 30 |
import com.sun.source.tree.*; |
31 |
||
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 |
*/ |
|
16303
b5dca0b42963
8008267: Add @Supported annotation to com.sun.source types
darcy
parents:
14952
diff
changeset
|
39 |
@jdk.Supported |
10 | 40 |
public class TreePath implements Iterable<Tree> { |
41 |
/** |
|
42 |
* Gets a tree path for a tree node within a compilation unit. |
|
43 |
* @return null if the node is not found |
|
44 |
*/ |
|
45 |
public static TreePath getPath(CompilationUnitTree unit, Tree target) { |
|
46 |
return getPath(new TreePath(unit), target); |
|
47 |
} |
|
48 |
||
49 |
/** |
|
50 |
* Gets a tree path for a tree node within a subtree identified by a TreePath object. |
|
51 |
* @return null if the node is not found |
|
52 |
*/ |
|
53 |
public static TreePath getPath(TreePath path, Tree target) { |
|
54 |
path.getClass(); |
|
55 |
target.getClass(); |
|
56 |
||
57 |
class Result extends Error { |
|
58 |
static final long serialVersionUID = -5942088234594905625L; |
|
59 |
TreePath path; |
|
60 |
Result(TreePath path) { |
|
61 |
this.path = path; |
|
62 |
} |
|
63 |
} |
|
14952 | 64 |
|
10 | 65 |
class PathFinder extends TreePathScanner<TreePath,Tree> { |
66 |
public TreePath scan(Tree tree, Tree target) { |
|
14952 | 67 |
if (tree == target) { |
10 | 68 |
throw new Result(new TreePath(getCurrentPath(), target)); |
14952 | 69 |
} |
10 | 70 |
return super.scan(tree, target); |
71 |
} |
|
72 |
} |
|
73 |
||
14952 | 74 |
if (path.getLeaf() == target) { |
75 |
return path; |
|
76 |
} |
|
77 |
||
10 | 78 |
try { |
79 |
new PathFinder().scan(path, target); |
|
80 |
} catch (Result result) { |
|
81 |
return result.path; |
|
82 |
} |
|
83 |
return null; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Creates a TreePath for a root node. |
|
88 |
*/ |
|
89 |
public TreePath(CompilationUnitTree t) { |
|
90 |
this(null, t); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Creates a TreePath for a child node. |
|
95 |
*/ |
|
96 |
public TreePath(TreePath p, Tree t) { |
|
97 |
if (t.getKind() == Tree.Kind.COMPILATION_UNIT) { |
|
98 |
compilationUnit = (CompilationUnitTree) t; |
|
99 |
parent = null; |
|
100 |
} |
|
101 |
else { |
|
102 |
compilationUnit = p.compilationUnit; |
|
103 |
parent = p; |
|
104 |
} |
|
105 |
leaf = t; |
|
106 |
} |
|
107 |
/** |
|
108 |
* Get the compilation unit associated with this path. |
|
109 |
*/ |
|
110 |
public CompilationUnitTree getCompilationUnit() { |
|
111 |
return compilationUnit; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Get the leaf node for this path. |
|
116 |
*/ |
|
117 |
public Tree getLeaf() { |
|
118 |
return leaf; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Get the path for the enclosing node, or null if there is no enclosing node. |
|
123 |
*/ |
|
124 |
public TreePath getParentPath() { |
|
125 |
return parent; |
|
126 |
} |
|
127 |
||
128 |
public Iterator<Tree> iterator() { |
|
129 |
return new Iterator<Tree>() { |
|
130 |
public boolean hasNext() { |
|
3149 | 131 |
return next != null; |
10 | 132 |
} |
133 |
||
134 |
public Tree next() { |
|
3149 | 135 |
Tree t = next.leaf; |
136 |
next = next.parent; |
|
137 |
return t; |
|
10 | 138 |
} |
139 |
||
140 |
public void remove() { |
|
141 |
throw new UnsupportedOperationException(); |
|
142 |
} |
|
143 |
||
3149 | 144 |
private TreePath next = TreePath.this; |
10 | 145 |
}; |
146 |
} |
|
147 |
||
148 |
private CompilationUnitTree compilationUnit; |
|
149 |
private Tree leaf; |
|
150 |
private TreePath parent; |
|
151 |
} |