KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * @(#)TreePathScanner.java 1.2 06/06/08
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
15 /**
16  * A TreeVisitor that visits all the child tree nodes, and provides
17  * support for maintaining a path for the parent nodes.
18  * To visit nodes of a particular type, just override the
19  * corresponding visitorXYZ method.
20  * Inside your method, call super.visitXYZ to visit descendant
21  * nodes.
22  *
23  * @author Jonathan Gibbons
24  * @since 1.6
25  */

26 public class TreePathScanner<R, P> extends TreeScanner<R, P> {
27     
28     /**
29      * Scan a tree from a position identified by a TreePath.
30      */

31     public R scan(TreePath path, P p) {
32     this.path = path;
33     try {
34         return path.getLeaf().accept(this, p);
35     } finally {
36         this.path = null;
37     }
38     }
39     
40     /**
41      * Scan a single node.
42      * The current path is updated for the duration of the scan.
43      */

44     @Override JavaDoc
45     public R scan(Tree tree, P p) {
46     if (tree == null)
47         return null;
48     
49         TreePath prev = path;
50         path = new TreePath(path, tree);
51     try {
52         return tree.accept(this, p);
53     } finally {
54         path = prev;
55     }
56     }
57     
58     /**
59      * Get the current path for the node, as built up by the currently
60      * active set of scan calls.
61      */

62     public TreePath getCurrentPath() {
63     return path;
64     }
65     
66     private TreePath path;
67 }
68
Popular Tags