Tue, 28 Dec 2010 15:54:52 -0800
6962318: Update copyright year
Reviewed-by: xdono
1 /*
2 * Copyright (c) 2006, 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 com.sun.source.tree.*;
29 import java.util.Iterator;
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 }
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();
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 }
70 try {
71 new PathFinder().scan(path, target);
72 } catch (Result result) {
73 return result.path;
74 }
75 return null;
76 }
78 /**
79 * Creates a TreePath for a root node.
80 */
81 public TreePath(CompilationUnitTree t) {
82 this(null, t);
83 }
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 }
106 /**
107 * Get the leaf node for this path.
108 */
109 public Tree getLeaf() {
110 return leaf;
111 }
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 }
120 public Iterator<Tree> iterator() {
121 return new Iterator<Tree>() {
122 public boolean hasNext() {
123 return next != null;
124 }
126 public Tree next() {
127 Tree t = next.leaf;
128 next = next.parent;
129 return t;
130 }
132 public void remove() {
133 throw new UnsupportedOperationException();
134 }
136 private TreePath next = TreePath.this;
137 };
138 }
140 private CompilationUnitTree compilationUnit;
141 private Tree leaf;
142 private TreePath parent;
143 }