1 /*
2  * Copyright (c) 2001, 2013, 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  */
25 
26 package com.sun.tools.javac.tree;
27 
28 import com.sun.tools.javac.util.*;
29 import com.sun.tools.javac.tree.JCTree.*;
30 
31 /** A subclass of Tree.Visitor, this class defines
32  *  a general tree scanner pattern. Translation proceeds recursively in
33  *  left-to-right order down a tree. There is one visitor method in this class
34  *  for every possible kind of tree node.  To obtain a specific
35  *  scanner, it suffices to override those visitor methods which
36  *  do some interesting work. The scanner class itself takes care of all
37  *  navigational aspects.
38  *
39  *  <p><b>This is NOT part of any supported API.
40  *  If you write code that depends on this, you do so at your own risk.
41  *  This code and its internal interfaces are subject to change or
42  *  deletion without notice.</b>
43  */
44 public class TreeScanner extends Visitor {
45 
46     /** Visitor method: Scan a single node.
47      */
scan(JCTree tree)48     public void scan(JCTree tree) {
49         if(tree!=null) tree.accept(this);
50     }
51 
52     /** Visitor method: scan a list of nodes.
53      */
scan(List<? extends JCTree> trees)54     public void scan(List<? extends JCTree> trees) {
55         if (trees != null)
56         for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
57             scan(l.head);
58     }
59 
60 
61 /* ***************************************************************************
62  * Visitor methods
63  ****************************************************************************/
64 
visitTopLevel(JCCompilationUnit tree)65     public void visitTopLevel(JCCompilationUnit tree) {
66         scan(tree.packageAnnotations);
67         scan(tree.pid);
68         scan(tree.defs);
69     }
70 
visitImport(JCImport tree)71     public void visitImport(JCImport tree) {
72         scan(tree.qualid);
73     }
74 
visitClassDef(JCClassDecl tree)75     public void visitClassDef(JCClassDecl tree) {
76         scan(tree.mods);
77         scan(tree.typarams);
78         scan(tree.extending);
79         scan(tree.implementing);
80         scan(tree.defs);
81     }
82 
visitMethodDef(JCMethodDecl tree)83     public void visitMethodDef(JCMethodDecl tree) {
84         scan(tree.mods);
85         scan(tree.restype);
86         scan(tree.typarams);
87         scan(tree.recvparam);
88         scan(tree.params);
89         scan(tree.thrown);
90         scan(tree.defaultValue);
91         scan(tree.body);
92     }
93 
visitVarDef(JCVariableDecl tree)94     public void visitVarDef(JCVariableDecl tree) {
95         scan(tree.mods);
96         scan(tree.vartype);
97         scan(tree.nameexpr);
98         scan(tree.init);
99     }
100 
visitSkip(JCSkip tree)101     public void visitSkip(JCSkip tree) {
102     }
103 
visitBlock(JCBlock tree)104     public void visitBlock(JCBlock tree) {
105         scan(tree.stats);
106     }
107 
visitDoLoop(JCDoWhileLoop tree)108     public void visitDoLoop(JCDoWhileLoop tree) {
109         scan(tree.body);
110         scan(tree.cond);
111     }
112 
visitWhileLoop(JCWhileLoop tree)113     public void visitWhileLoop(JCWhileLoop tree) {
114         scan(tree.cond);
115         scan(tree.body);
116     }
117 
visitForLoop(JCForLoop tree)118     public void visitForLoop(JCForLoop tree) {
119         scan(tree.init);
120         scan(tree.cond);
121         scan(tree.step);
122         scan(tree.body);
123     }
124 
visitForeachLoop(JCEnhancedForLoop tree)125     public void visitForeachLoop(JCEnhancedForLoop tree) {
126         scan(tree.var);
127         scan(tree.expr);
128         scan(tree.body);
129     }
130 
visitLabelled(JCLabeledStatement tree)131     public void visitLabelled(JCLabeledStatement tree) {
132         scan(tree.body);
133     }
134 
visitSwitch(JCSwitch tree)135     public void visitSwitch(JCSwitch tree) {
136         scan(tree.selector);
137         scan(tree.cases);
138     }
139 
visitCase(JCCase tree)140     public void visitCase(JCCase tree) {
141         scan(tree.pat);
142         scan(tree.stats);
143     }
144 
visitSynchronized(JCSynchronized tree)145     public void visitSynchronized(JCSynchronized tree) {
146         scan(tree.lock);
147         scan(tree.body);
148     }
149 
visitTry(JCTry tree)150     public void visitTry(JCTry tree) {
151         scan(tree.resources);
152         scan(tree.body);
153         scan(tree.catchers);
154         scan(tree.finalizer);
155     }
156 
visitCatch(JCCatch tree)157     public void visitCatch(JCCatch tree) {
158         scan(tree.param);
159         scan(tree.body);
160     }
161 
visitConditional(JCConditional tree)162     public void visitConditional(JCConditional tree) {
163         scan(tree.cond);
164         scan(tree.truepart);
165         scan(tree.falsepart);
166     }
167 
visitIf(JCIf tree)168     public void visitIf(JCIf tree) {
169         scan(tree.cond);
170         scan(tree.thenpart);
171         scan(tree.elsepart);
172     }
173 
visitExec(JCExpressionStatement tree)174     public void visitExec(JCExpressionStatement tree) {
175         scan(tree.expr);
176     }
177 
visitBreak(JCBreak tree)178     public void visitBreak(JCBreak tree) {
179     }
180 
visitContinue(JCContinue tree)181     public void visitContinue(JCContinue tree) {
182     }
183 
visitReturn(JCReturn tree)184     public void visitReturn(JCReturn tree) {
185         scan(tree.expr);
186     }
187 
visitThrow(JCThrow tree)188     public void visitThrow(JCThrow tree) {
189         scan(tree.expr);
190     }
191 
visitAssert(JCAssert tree)192     public void visitAssert(JCAssert tree) {
193         scan(tree.cond);
194         scan(tree.detail);
195     }
196 
visitApply(JCMethodInvocation tree)197     public void visitApply(JCMethodInvocation tree) {
198         scan(tree.typeargs);
199         scan(tree.meth);
200         scan(tree.args);
201     }
202 
visitNewClass(JCNewClass tree)203     public void visitNewClass(JCNewClass tree) {
204         scan(tree.encl);
205         scan(tree.typeargs);
206         scan(tree.clazz);
207         scan(tree.args);
208         scan(tree.def);
209     }
210 
visitNewArray(JCNewArray tree)211     public void visitNewArray(JCNewArray tree) {
212         scan(tree.annotations);
213         scan(tree.elemtype);
214         scan(tree.dims);
215         for (List<JCAnnotation> annos : tree.dimAnnotations)
216             scan(annos);
217         scan(tree.elems);
218     }
219 
visitLambda(JCLambda tree)220     public void visitLambda(JCLambda tree) {
221         scan(tree.body);
222         scan(tree.params);
223     }
224 
visitParens(JCParens tree)225     public void visitParens(JCParens tree) {
226         scan(tree.expr);
227     }
228 
visitAssign(JCAssign tree)229     public void visitAssign(JCAssign tree) {
230         scan(tree.lhs);
231         scan(tree.rhs);
232     }
233 
visitAssignop(JCAssignOp tree)234     public void visitAssignop(JCAssignOp tree) {
235         scan(tree.lhs);
236         scan(tree.rhs);
237     }
238 
visitUnary(JCUnary tree)239     public void visitUnary(JCUnary tree) {
240         scan(tree.arg);
241     }
242 
visitBinary(JCBinary tree)243     public void visitBinary(JCBinary tree) {
244         scan(tree.lhs);
245         scan(tree.rhs);
246     }
247 
visitTypeCast(JCTypeCast tree)248     public void visitTypeCast(JCTypeCast tree) {
249         scan(tree.clazz);
250         scan(tree.expr);
251     }
252 
visitTypeTest(JCInstanceOf tree)253     public void visitTypeTest(JCInstanceOf tree) {
254         scan(tree.expr);
255         scan(tree.clazz);
256     }
257 
visitIndexed(JCArrayAccess tree)258     public void visitIndexed(JCArrayAccess tree) {
259         scan(tree.indexed);
260         scan(tree.index);
261     }
262 
visitSelect(JCFieldAccess tree)263     public void visitSelect(JCFieldAccess tree) {
264         scan(tree.selected);
265     }
266 
visitReference(JCMemberReference tree)267     public void visitReference(JCMemberReference tree) {
268         scan(tree.expr);
269         scan(tree.typeargs);
270     }
271 
visitIdent(JCIdent tree)272     public void visitIdent(JCIdent tree) {
273     }
274 
visitLiteral(JCLiteral tree)275     public void visitLiteral(JCLiteral tree) {
276     }
277 
visitTypeIdent(JCPrimitiveTypeTree tree)278     public void visitTypeIdent(JCPrimitiveTypeTree tree) {
279     }
280 
visitTypeArray(JCArrayTypeTree tree)281     public void visitTypeArray(JCArrayTypeTree tree) {
282         scan(tree.elemtype);
283     }
284 
visitTypeApply(JCTypeApply tree)285     public void visitTypeApply(JCTypeApply tree) {
286         scan(tree.clazz);
287         scan(tree.arguments);
288     }
289 
visitTypeUnion(JCTypeUnion tree)290     public void visitTypeUnion(JCTypeUnion tree) {
291         scan(tree.alternatives);
292     }
293 
visitTypeIntersection(JCTypeIntersection tree)294     public void visitTypeIntersection(JCTypeIntersection tree) {
295         scan(tree.bounds);
296     }
297 
visitTypeParameter(JCTypeParameter tree)298     public void visitTypeParameter(JCTypeParameter tree) {
299         scan(tree.annotations);
300         scan(tree.bounds);
301     }
302 
303     @Override
visitWildcard(JCWildcard tree)304     public void visitWildcard(JCWildcard tree) {
305         scan(tree.kind);
306         if (tree.inner != null)
307             scan(tree.inner);
308     }
309 
310     @Override
visitTypeBoundKind(TypeBoundKind that)311     public void visitTypeBoundKind(TypeBoundKind that) {
312     }
313 
visitModifiers(JCModifiers tree)314     public void visitModifiers(JCModifiers tree) {
315         scan(tree.annotations);
316     }
317 
visitAnnotation(JCAnnotation tree)318     public void visitAnnotation(JCAnnotation tree) {
319         scan(tree.annotationType);
320         scan(tree.args);
321     }
322 
visitAnnotatedType(JCAnnotatedType tree)323     public void visitAnnotatedType(JCAnnotatedType tree) {
324         scan(tree.annotations);
325         scan(tree.underlyingType);
326     }
327 
visitErroneous(JCErroneous tree)328     public void visitErroneous(JCErroneous tree) {
329     }
330 
visitLetExpr(LetExpr tree)331     public void visitLetExpr(LetExpr tree) {
332         scan(tree.defs);
333         scan(tree.expr);
334     }
335 
visitTree(JCTree tree)336     public void visitTree(JCTree tree) {
337         Assert.error();
338     }
339 }
340