001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.source.util;
027:
028: import com.sun.source.tree.*;
029:
030: /**
031: * A TreeVisitor that visits all the child tree nodes.
032: * To visit nodes of a particular type, just override the
033: * corresponding visitXYZ method.
034: * Inside your method, call super.visitXYZ to visit descendant
035: * nodes.
036: *
037: * <p>The default implementation of the visitXYZ methods will determine
038: * a result as follows:
039: * <ul>
040: * <li>If the node being visited has no children, the result will be null.
041: * <li>If the node being visited has one child, the result will be the
042: * result of calling {@code scan} on that child. The child may be a simple node
043: * or itself a list of nodes.
044: * <li> If the node being visited has more than one child, the result will
045: * be determined by calling {@code scan} each child in turn, and then combining the
046: * result of each scan after the first with the cumulative result
047: * so far, as determined by the {@link #reduce} method. Each child may be either
048: * a simple node of a list of nodes. The default behavior of the {@code reduce}
049: * method is such that the result of the visitXYZ method will be the result of
050: * the last child scanned.
051: * </ul>
052: *
053: * <p>Here is an example to count the number of identifier nodes in a tree:
054: * <pre>
055: * class CountIdentifiers extends TreeScanner<Integer,Void> {
056: * {@literal @}Override
057: * public Integer visitIdentifier(IdentifierTree node, Void p) {
058: * return 1;
059: * }
060: * {@literal @}Override
061: * public Integer reduce(Integer r1, Integer r2) {
062: * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
063: * }
064: * }
065: * </pre>
066: *
067: * @author Peter von der Ahé
068: * @author Jonathan Gibbons
069: * @since 1.6
070: */
071: public class TreeScanner<R, P> implements TreeVisitor<R, P> {
072:
073: /** Scan a single node.
074: */
075: public R scan(Tree node, P p) {
076: return (node == null) ? null : node.accept(this , p);
077: }
078:
079: private R scanAndReduce(Tree node, P p, R r) {
080: return reduce(scan(node, p), r);
081: }
082:
083: /** Scan a list of nodes.
084: */
085: public R scan(Iterable<? extends Tree> nodes, P p) {
086: R r = null;
087: if (nodes != null) {
088: boolean first = true;
089: for (Tree node : nodes) {
090: r = (first ? scan(node, p) : scanAndReduce(node, p, r));
091: first = false;
092: }
093: }
094: return r;
095: }
096:
097: private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
098: return reduce(scan(nodes, p), r);
099: }
100:
101: /**
102: * Reduces two results into a combined result.
103: * The default implementation is to return the first parameter.
104: * The general contract of the method is that it may take any action whatsoever.
105: */
106: public R reduce(R r1, R r2) {
107: return r1;
108: }
109:
110: /* ***************************************************************************
111: * Visitor methods
112: ****************************************************************************/
113:
114: public R visitCompilationUnit(CompilationUnitTree node, P p) {
115: R r = scan(node.getPackageAnnotations(), p);
116: r = scanAndReduce(node.getPackageName(), p, r);
117: r = scanAndReduce(node.getImports(), p, r);
118: r = scanAndReduce(node.getTypeDecls(), p, r);
119: return r;
120: }
121:
122: public R visitImport(ImportTree node, P p) {
123: return scan(node.getQualifiedIdentifier(), p);
124: }
125:
126: public R visitClass(ClassTree node, P p) {
127: R r = scan(node.getModifiers(), p);
128: r = scanAndReduce(node.getTypeParameters(), p, r);
129: r = scanAndReduce(node.getExtendsClause(), p, r);
130: r = scanAndReduce(node.getImplementsClause(), p, r);
131: r = scanAndReduce(node.getMembers(), p, r);
132: return r;
133: }
134:
135: public R visitMethod(MethodTree node, P p) {
136: R r = scan(node.getModifiers(), p);
137: r = scanAndReduce(node.getReturnType(), p, r);
138: r = scanAndReduce(node.getTypeParameters(), p, r);
139: r = scanAndReduce(node.getParameters(), p, r);
140: r = scanAndReduce(node.getThrows(), p, r);
141: r = scanAndReduce(node.getBody(), p, r);
142: return r;
143: }
144:
145: public R visitVariable(VariableTree node, P p) {
146: R r = scan(node.getModifiers(), p);
147: r = scanAndReduce(node.getType(), p, r);
148: r = scanAndReduce(node.getInitializer(), p, r);
149: return r;
150: }
151:
152: public R visitEmptyStatement(EmptyStatementTree node, P p) {
153: return null;
154: }
155:
156: public R visitBlock(BlockTree node, P p) {
157: return scan(node.getStatements(), p);
158: }
159:
160: public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
161: R r = scan(node.getStatement(), p);
162: r = scanAndReduce(node.getCondition(), p, r);
163: return r;
164: }
165:
166: public R visitWhileLoop(WhileLoopTree node, P p) {
167: R r = scan(node.getCondition(), p);
168: r = scanAndReduce(node.getStatement(), p, r);
169: return r;
170: }
171:
172: public R visitForLoop(ForLoopTree node, P p) {
173: R r = scan(node.getInitializer(), p);
174: r = scanAndReduce(node.getCondition(), p, r);
175: r = scanAndReduce(node.getUpdate(), p, r);
176: r = scanAndReduce(node.getStatement(), p, r);
177: return r;
178: }
179:
180: public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
181: R r = scan(node.getVariable(), p);
182: r = scanAndReduce(node.getExpression(), p, r);
183: r = scanAndReduce(node.getStatement(), p, r);
184: return r;
185: }
186:
187: public R visitLabeledStatement(LabeledStatementTree node, P p) {
188: return scan(node.getStatement(), p);
189: }
190:
191: public R visitSwitch(SwitchTree node, P p) {
192: R r = scan(node.getExpression(), p);
193: r = scanAndReduce(node.getCases(), p, r);
194: return r;
195: }
196:
197: public R visitCase(CaseTree node, P p) {
198: R r = scan(node.getExpression(), p);
199: r = scanAndReduce(node.getStatements(), p, r);
200: return r;
201: }
202:
203: public R visitSynchronized(SynchronizedTree node, P p) {
204: R r = scan(node.getExpression(), p);
205: r = scanAndReduce(node.getBlock(), p, r);
206: return r;
207: }
208:
209: public R visitTry(TryTree node, P p) {
210: R r = scan(node.getBlock(), p);
211: r = scanAndReduce(node.getCatches(), p, r);
212: r = scanAndReduce(node.getFinallyBlock(), p, r);
213: return r;
214: }
215:
216: public R visitCatch(CatchTree node, P p) {
217: R r = scan(node.getParameter(), p);
218: r = scanAndReduce(node.getBlock(), p, r);
219: return r;
220: }
221:
222: public R visitConditionalExpression(ConditionalExpressionTree node,
223: P p) {
224: R r = scan(node.getCondition(), p);
225: r = scanAndReduce(node.getTrueExpression(), p, r);
226: r = scanAndReduce(node.getFalseExpression(), p, r);
227: return r;
228: }
229:
230: public R visitIf(IfTree node, P p) {
231: R r = scan(node.getCondition(), p);
232: r = scanAndReduce(node.getThenStatement(), p, r);
233: r = scanAndReduce(node.getElseStatement(), p, r);
234: return r;
235: }
236:
237: public R visitExpressionStatement(ExpressionStatementTree node, P p) {
238: return scan(node.getExpression(), p);
239: }
240:
241: public R visitBreak(BreakTree node, P p) {
242: return null;
243: }
244:
245: public R visitContinue(ContinueTree node, P p) {
246: return null;
247: }
248:
249: public R visitReturn(ReturnTree node, P p) {
250: return scan(node.getExpression(), p);
251: }
252:
253: public R visitThrow(ThrowTree node, P p) {
254: return scan(node.getExpression(), p);
255: }
256:
257: public R visitAssert(AssertTree node, P p) {
258: R r = scan(node.getCondition(), p);
259: r = scanAndReduce(node.getDetail(), p, r);
260: return r;
261: }
262:
263: public R visitMethodInvocation(MethodInvocationTree node, P p) {
264: R r = scan(node.getTypeArguments(), p);
265: r = scanAndReduce(node.getMethodSelect(), p, r);
266: r = scanAndReduce(node.getArguments(), p, r);
267: return r;
268: }
269:
270: public R visitNewClass(NewClassTree node, P p) {
271: R r = scan(node.getEnclosingExpression(), p);
272: r = scanAndReduce(node.getIdentifier(), p, r);
273: r = scanAndReduce(node.getTypeArguments(), p, r);
274: r = scanAndReduce(node.getArguments(), p, r);
275: r = scanAndReduce(node.getClassBody(), p, r);
276: return r;
277: }
278:
279: public R visitNewArray(NewArrayTree node, P p) {
280: R r = scan(node.getType(), p);
281: r = scanAndReduce(node.getDimensions(), p, r);
282: r = scanAndReduce(node.getInitializers(), p, r);
283: return r;
284: }
285:
286: public R visitParenthesized(ParenthesizedTree node, P p) {
287: return scan(node.getExpression(), p);
288: }
289:
290: public R visitAssignment(AssignmentTree node, P p) {
291: R r = scan(node.getVariable(), p);
292: r = scanAndReduce(node.getExpression(), p, r);
293: return r;
294: }
295:
296: public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
297: R r = scan(node.getVariable(), p);
298: r = scanAndReduce(node.getExpression(), p, r);
299: return r;
300: }
301:
302: public R visitUnary(UnaryTree node, P p) {
303: return scan(node.getExpression(), p);
304: }
305:
306: public R visitBinary(BinaryTree node, P p) {
307: R r = scan(node.getLeftOperand(), p);
308: r = scanAndReduce(node.getRightOperand(), p, r);
309: return r;
310: }
311:
312: public R visitTypeCast(TypeCastTree node, P p) {
313: R r = scan(node.getType(), p);
314: r = scanAndReduce(node.getExpression(), p, r);
315: return r;
316: }
317:
318: public R visitInstanceOf(InstanceOfTree node, P p) {
319: R r = scan(node.getExpression(), p);
320: r = scanAndReduce(node.getType(), p, r);
321: return r;
322: }
323:
324: public R visitArrayAccess(ArrayAccessTree node, P p) {
325: R r = scan(node.getExpression(), p);
326: r = scanAndReduce(node.getIndex(), p, r);
327: return r;
328: }
329:
330: public R visitMemberSelect(MemberSelectTree node, P p) {
331: return scan(node.getExpression(), p);
332: }
333:
334: public R visitIdentifier(IdentifierTree node, P p) {
335: return null;
336: }
337:
338: public R visitLiteral(LiteralTree node, P p) {
339: return null;
340: }
341:
342: public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
343: return null;
344: }
345:
346: public R visitArrayType(ArrayTypeTree node, P p) {
347: return scan(node.getType(), p);
348: }
349:
350: public R visitParameterizedType(ParameterizedTypeTree node, P p) {
351: R r = scan(node.getType(), p);
352: r = scanAndReduce(node.getTypeArguments(), p, r);
353: return r;
354: }
355:
356: public R visitTypeParameter(TypeParameterTree node, P p) {
357: return scan(node.getBounds(), p);
358: }
359:
360: public R visitWildcard(WildcardTree node, P p) {
361: return scan(node.getBound(), p);
362: }
363:
364: public R visitModifiers(ModifiersTree node, P p) {
365: return scan(node.getAnnotations(), p);
366: }
367:
368: public R visitAnnotation(AnnotationTree node, P p) {
369: R r = scan(node.getAnnotationType(), p);
370: r = scanAndReduce(node.getArguments(), p, r);
371: return r;
372: }
373:
374: public R visitOther(Tree node, P p) {
375: return null;
376: }
377:
378: public R visitErroneous(ErroneousTree node, P p) {
379: return null;
380: }
381: }
|