KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * @(#)TreeScanner.java 1.5 06/06/22
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.
17  * To visit nodes of a particular type, just override the
18  * corresponding visitXYZ method.
19  * Inside your method, call super.visitXYZ to visit descendant
20  * nodes.
21  *
22  * <p>The default implementation of the visitXYZ methods will determine
23  * a result as follows:
24  * <ul>
25  * <li>If the node being visited has no children, the result will be null.
26  * <li>If the node being visited has one child, the result will be the
27  * result of calling {@code scan} on that child. The child may be a simple node
28  * or itself a list of nodes.
29  * <li> If the node being visited has more than one child, the result will
30  * be determined by calling {@code scan} each child in turn, and then combining the
31  * result of each scan after the first with the cumulative result
32  * so far, as determined by the {@link #reduce} method. Each child may be either
33  * a simple node of a list of nodes. The default behavior of the {@code reduce}
34  * method is such that the result of the visitXYZ method will be the result of
35  * the last child scanned.
36  * </ul>
37  *
38  * <p>Here is an example to count the number of identifier nodes in a tree:
39  * <pre>
40  * class CountIdentifiers extends TreeScanner<Integer,Void> {
41  * {@literal @}Override
42  * public Integer visitIdentifier(IdentifierTree node, Void p) {
43  * return 1;
44  * }
45  * {@literal @}Override
46  * public Integer reduce(Integer r1, Integer r2) {
47  * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
48  * }
49  * }
50  * </pre>
51  *
52  * @author Peter von der Ah&eacute;
53  * @author Jonathan Gibbons
54  * @since 1.6
55  */

56 public class TreeScanner<R,P> implements TreeVisitor<R,P> {
57
58     /** Scan a single node.
59      */

60     public R scan(Tree node, P p) {
61     return (node == null) ? null : node.accept(this, p);
62     }
63     
64     private R scanAndReduce(Tree node, P p, R r) {
65         return reduce(scan(node, p), r);
66     }
67
68     /** Scan a list of nodes.
69      */

70     public R scan(Iterable JavaDoc<? extends Tree> nodes, P p) {
71     R r = null;
72     if (nodes != null) {
73             boolean first = true;
74             for (Tree node : nodes) {
75                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
76                 first = false;
77             }
78         }
79         return r;
80     }
81     
82     private R scanAndReduce(Iterable JavaDoc<? extends Tree> nodes, P p, R r) {
83         return reduce(scan(nodes, p), r);
84     }
85     
86     /**
87      * Reduces two results into a combined result.
88      * The default implementation is to return the first parameter.
89      * The general contract of the method is that it may take any action whatsoever.
90      */

91     public R reduce(R r1, R r2) {
92         return r1;
93     }
94
95
96 /* ***************************************************************************
97  * Visitor methods
98  ****************************************************************************/

99
100     public R visitCompilationUnit(CompilationUnitTree node, P p) {
101         R r = scan(node.getPackageAnnotations(), p);
102     r = scanAndReduce(node.getPackageName(), p, r);
103         r = scanAndReduce(node.getImports(), p, r);
104     r = scanAndReduce(node.getTypeDecls(), p, r);
105         return r;
106     }
107
108     public R visitImport(ImportTree node, P p) {
109     return scan(node.getQualifiedIdentifier(), p);
110     }
111
112     public R visitClass(ClassTree node, P p) {
113     R r = scan(node.getModifiers(), p);
114     r = scanAndReduce(node.getTypeParameters(), p, r);
115     r = scanAndReduce(node.getExtendsClause(), p, r);
116     r = scanAndReduce(node.getImplementsClause(), p, r);
117     r = scanAndReduce(node.getMembers(), p, r);
118         return r;
119     }
120
121     public R visitMethod(MethodTree node, P p) {
122     R r = scan(node.getModifiers(), p);
123     r = scanAndReduce(node.getReturnType(), p, r);
124     r = scanAndReduce(node.getTypeParameters(), p, r);
125     r = scanAndReduce(node.getParameters(), p, r);
126     r = scanAndReduce(node.getThrows(), p, r);
127     r = scanAndReduce(node.getBody(), p, r);
128         return r;
129     }
130     
131     public R visitVariable(VariableTree node, P p) {
132     R r = scan(node.getModifiers(), p);
133     r = scanAndReduce(node.getType(), p, r);
134     r = scanAndReduce(node.getInitializer(), p, r);
135         return r;
136     }
137     
138     public R visitEmptyStatement(EmptyStatementTree node, P p) {
139         return null;
140     }
141
142     public R visitBlock(BlockTree node, P p) {
143     return scan(node.getStatements(), p);
144     }
145
146     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
147     R r = scan(node.getStatement(), p);
148     r = scanAndReduce(node.getCondition(), p, r);
149         return r;
150     }
151
152     public R visitWhileLoop(WhileLoopTree node, P p) {
153     R r = scan(node.getCondition(), p);
154     r = scanAndReduce(node.getStatement(), p, r);
155         return r;
156     }
157
158     public R visitForLoop(ForLoopTree node, P p) {
159     R r = scan(node.getInitializer(), p);
160     r = scanAndReduce(node.getCondition(), p, r);
161     r = scanAndReduce(node.getUpdate(), p, r);
162     r = scanAndReduce(node.getStatement(), p, r);
163         return r;
164     }
165
166     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
167     R r = scan(node.getVariable(), p);
168     r = scanAndReduce(node.getExpression(), p, r);
169     r = scanAndReduce(node.getStatement(), p, r);
170         return r;
171     }
172
173     public R visitLabeledStatement(LabeledStatementTree node, P p) {
174     return scan(node.getStatement(), p);
175     }
176
177     public R visitSwitch(SwitchTree node, P p) {
178     R r = scan(node.getExpression(), p);
179     r = scanAndReduce(node.getCases(), p, r);
180         return r;
181     }
182
183     public R visitCase(CaseTree node, P p) {
184     R r = scan(node.getExpression(), p);
185     r = scanAndReduce(node.getStatements(), p, r);
186         return r;
187     }
188
189     public R visitSynchronized(SynchronizedTree node, P p) {
190     R r = scan(node.getExpression(), p);
191     r = scanAndReduce(node.getBlock(), p, r);
192         return r;
193     }
194
195     public R visitTry(TryTree node, P p) {
196     R r = scan(node.getBlock(), p);
197     r = scanAndReduce(node.getCatches(), p, r);
198     r = scanAndReduce(node.getFinallyBlock(), p, r);
199         return r;
200     }
201
202     public R visitCatch(CatchTree node, P p) {
203     R r = scan(node.getParameter(), p);
204     r = scanAndReduce(node.getBlock(), p, r);
205         return r;
206     }
207
208     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
209     R r = scan(node.getCondition(), p);
210     r = scanAndReduce(node.getTrueExpression(), p, r);
211     r = scanAndReduce(node.getFalseExpression(), p, r);
212         return r;
213     }
214
215     public R visitIf(IfTree node, P p) {
216     R r = scan(node.getCondition(), p);
217     r = scanAndReduce(node.getThenStatement(), p, r);
218     r = scanAndReduce(node.getElseStatement(), p, r);
219         return r;
220     }
221
222     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
223     return scan(node.getExpression(), p);
224     }
225
226     public R visitBreak(BreakTree node, P p) {
227         return null;
228     }
229
230     public R visitContinue(ContinueTree node, P p) {
231         return null;
232     }
233
234     public R visitReturn(ReturnTree node, P p) {
235     return scan(node.getExpression(), p);
236     }
237
238     public R visitThrow(ThrowTree node, P p) {
239     return scan(node.getExpression(), p);
240     }
241
242     public R visitAssert(AssertTree node, P p) {
243     R r = scan(node.getCondition(), p);
244     r = scanAndReduce(node.getDetail(), p, r);
245         return r;
246     }
247
248     public R visitMethodInvocation(MethodInvocationTree node, P p) {
249     R r = scan(node.getTypeArguments(), p);
250     r = scanAndReduce(node.getMethodSelect(), p, r);
251     r = scanAndReduce(node.getArguments(), p, r);
252         return r;
253     }
254
255     public R visitNewClass(NewClassTree node, P p) {
256     R r = scan(node.getEnclosingExpression(), p);
257     r = scanAndReduce(node.getIdentifier(), p, r);
258     r = scanAndReduce(node.getTypeArguments(), p, r);
259         r = scanAndReduce(node.getArguments(), p, r);
260     r = scanAndReduce(node.getClassBody(), p, r);
261         return r;
262     }
263
264     public R visitNewArray(NewArrayTree node, P p) {
265     R r = scan(node.getType(), p);
266     r = scanAndReduce(node.getDimensions(), p, r);
267     r = scanAndReduce(node.getInitializers(), p, r);
268         return r;
269     }
270
271     public R visitParenthesized(ParenthesizedTree node, P p) {
272     return scan(node.getExpression(), p);
273     }
274
275     public R visitAssignment(AssignmentTree node, P p) {
276     R r = scan(node.getVariable(), p);
277     r = scanAndReduce(node.getExpression(), p, r);
278         return r;
279     }
280
281     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
282     R r = scan(node.getVariable(), p);
283     r = scanAndReduce(node.getExpression(), p, r);
284         return r;
285     }
286
287     public R visitUnary(UnaryTree node, P p) {
288     return scan(node.getExpression(), p);
289     }
290
291     public R visitBinary(BinaryTree node, P p) {
292     R r = scan(node.getLeftOperand(), p);
293     r = scanAndReduce(node.getRightOperand(), p, r);
294         return r;
295     }
296
297     public R visitTypeCast(TypeCastTree node, P p) {
298     R r = scan(node.getType(), p);
299     r = scanAndReduce(node.getExpression(), p, r);
300         return r;
301     }
302
303     public R visitInstanceOf(InstanceOfTree node, P p) {
304     R r = scan(node.getExpression(), p);
305     r = scanAndReduce(node.getType(), p, r);
306         return r;
307     }
308
309     public R visitArrayAccess(ArrayAccessTree node, P p) {
310     R r = scan(node.getExpression(), p);
311     r = scanAndReduce(node.getIndex(), p, r);
312         return r;
313     }
314
315     public R visitMemberSelect(MemberSelectTree node, P p) {
316     return scan(node.getExpression(), p);
317     }
318
319     public R visitIdentifier(IdentifierTree node, P p) {
320         return null;
321     }
322
323     public R visitLiteral(LiteralTree node, P p) {
324         return null;
325     }
326
327     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
328         return null;
329     }
330
331     public R visitArrayType(ArrayTypeTree node, P p) {
332         return scan(node.getType(), p);
333     }
334
335     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
336     R r = scan(node.getType(), p);
337     r = scanAndReduce(node.getTypeArguments(), p, r);
338         return r;
339     }
340
341     public R visitTypeParameter(TypeParameterTree node, P p) {
342     return scan(node.getBounds(), p);
343     }
344
345     public R visitWildcard(WildcardTree node, P p) {
346         return scan(node.getBound(), p);
347     }
348
349     public R visitModifiers(ModifiersTree node, P p) {
350     return scan(node.getAnnotations(), p);
351     }
352
353     public R visitAnnotation(AnnotationTree node, P p) {
354     R r = scan(node.getAnnotationType(), p);
355     r = scanAndReduce(node.getArguments(), p, r);
356         return r;
357     }
358
359     public R visitOther(Tree node, P p) {
360     return null;
361     }
362
363     public R visitErroneous(ErroneousTree node, P p) {
364         return null;
365     }
366 }
367
Popular Tags