KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > source > util > TreePath


1 /**
2  * @(#)TreePath.java 1.3 06/06/12
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  *
7  * Use and Distribution is subject to the Java Research License available
8  * at <http://wwws.sun.com/software/communitysource/jrl.html>.
9  */

10
11 package com.sun.source.util;
12
13 import com.sun.source.tree.*;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * A path of tree nodes, typically used to represent the sequence of ancestor
18  * nodes of a tree node up to the top level CompilationUnitTree node.
19  *
20  * @author Jonathan Gibbons
21  * @since 1.6
22  */

23 public class TreePath implements Iterable JavaDoc<Tree> {
24     /**
25      * Gets a tree path for a tree node within a compilation unit.
26      * @return null if the node is not found
27      */

28     public static TreePath getPath(CompilationUnitTree unit, Tree target) {
29     return getPath(new TreePath(unit), target);
30     }
31     
32     /**
33      * Gets a tree path for a tree node within a subtree identified by a TreePath object.
34      * @return null if the node is not found
35      */

36     public static TreePath getPath(TreePath path, Tree target) {
37     path.getClass();
38     target.getClass();
39     
40     class Result extends Error JavaDoc {
41         static final long serialVersionUID = -5942088234594905625L;
42         TreePath path;
43         Result(TreePath path) {
44         this.path = path;
45         }
46     }
47     class PathFinder extends TreePathScanner<TreePath,Tree> {
48         public TreePath scan(Tree tree, Tree target) {
49         if (tree == target)
50             throw new Result(new TreePath(getCurrentPath(), target));
51         return super.scan(tree, target);
52         }
53     }
54     
55     try {
56         new PathFinder().scan(path, target);
57     } catch (Result result) {
58         return result.path;
59     }
60         return null;
61     }
62     
63     /**
64      * Creates a TreePath for a root node.
65      */

66     public TreePath(CompilationUnitTree t) {
67         this(null, t);
68     }
69     
70     /**
71      * Creates a TreePath for a child node.
72      */

73     public TreePath(TreePath p, Tree t) {
74         if (t.getKind() == Tree.Kind.COMPILATION_UNIT) {
75             compilationUnit = (CompilationUnitTree) t;
76             parent = null;
77         }
78         else {
79             compilationUnit = p.compilationUnit;
80             parent = p;
81         }
82         leaf = t;
83     }
84     /**
85      * Get the compilation unit associated with this path.
86      */

87     public CompilationUnitTree getCompilationUnit() {
88     return compilationUnit;
89     }
90     
91     /**
92      * Get the leaf node for this path.
93      */

94     public Tree getLeaf() {
95     return leaf;
96     }
97     
98     /**
99      * Get the path for the enclosing node, or null if there is no enclosing node.
100      */

101     public TreePath getParentPath() {
102     return parent;
103     }
104
105     public Iterator JavaDoc<Tree> iterator() {
106     return new Iterator JavaDoc<Tree>() {
107         public boolean hasNext() {
108         return curr.parent != null;
109         }
110         
111         public Tree next() {
112         curr = curr.parent;
113         return curr.leaf;
114         }
115         
116         public void remove() {
117         throw new UnsupportedOperationException JavaDoc();
118         }
119         
120         private TreePath curr;
121     };
122     }
123     
124     private CompilationUnitTree compilationUnit;
125     private Tree leaf;
126     private TreePath parent;
127 }
128
Popular Tags