1 /*
2  * Copyright (c) 2001, 2016, 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.defs);
67     }
68 
visitPackageDef(JCPackageDecl tree)69     public void visitPackageDef(JCPackageDecl tree) {
70         scan(tree.annotations);
71         scan(tree.pid);
72     }
73 
74     @Override
visitModuleDef(JCModuleDecl tree)75     public void visitModuleDef(JCModuleDecl tree) {
76         scan(tree.mods);
77         scan(tree.qualId);
78         scan(tree.directives);
79     }
80 
81     @Override
visitExports(JCExports tree)82     public void visitExports(JCExports tree) {
83         scan(tree.qualid);
84         scan(tree.moduleNames);
85     }
86 
87     @Override
visitOpens(JCOpens tree)88     public void visitOpens(JCOpens tree) {
89         scan(tree.qualid);
90         scan(tree.moduleNames);
91     }
92 
93     @Override
visitProvides(JCProvides tree)94     public void visitProvides(JCProvides tree) {
95         scan(tree.serviceName);
96         scan(tree.implNames);
97     }
98 
99     @Override
visitRequires(JCRequires tree)100     public void visitRequires(JCRequires tree) {
101         scan(tree.moduleName);
102     }
103 
104     @Override
visitUses(JCUses tree)105     public void visitUses(JCUses tree) {
106         scan(tree.qualid);
107     }
108 
visitImport(JCImport tree)109     public void visitImport(JCImport tree) {
110         scan(tree.qualid);
111     }
112 
visitClassDef(JCClassDecl tree)113     public void visitClassDef(JCClassDecl tree) {
114         scan(tree.mods);
115         scan(tree.typarams);
116         scan(tree.extending);
117         scan(tree.implementing);
118         scan(tree.defs);
119     }
120 
visitMethodDef(JCMethodDecl tree)121     public void visitMethodDef(JCMethodDecl tree) {
122         scan(tree.mods);
123         scan(tree.restype);
124         scan(tree.typarams);
125         scan(tree.recvparam);
126         scan(tree.params);
127         scan(tree.thrown);
128         scan(tree.defaultValue);
129         scan(tree.body);
130     }
131 
visitVarDef(JCVariableDecl tree)132     public void visitVarDef(JCVariableDecl tree) {
133         scan(tree.mods);
134         scan(tree.vartype);
135         scan(tree.nameexpr);
136         scan(tree.init);
137     }
138 
visitSkip(JCSkip tree)139     public void visitSkip(JCSkip tree) {
140     }
141 
visitBlock(JCBlock tree)142     public void visitBlock(JCBlock tree) {
143         scan(tree.stats);
144     }
145 
visitDoLoop(JCDoWhileLoop tree)146     public void visitDoLoop(JCDoWhileLoop tree) {
147         scan(tree.body);
148         scan(tree.cond);
149     }
150 
visitWhileLoop(JCWhileLoop tree)151     public void visitWhileLoop(JCWhileLoop tree) {
152         scan(tree.cond);
153         scan(tree.body);
154     }
155 
visitForLoop(JCForLoop tree)156     public void visitForLoop(JCForLoop tree) {
157         scan(tree.init);
158         scan(tree.cond);
159         scan(tree.step);
160         scan(tree.body);
161     }
162 
visitForeachLoop(JCEnhancedForLoop tree)163     public void visitForeachLoop(JCEnhancedForLoop tree) {
164         scan(tree.var);
165         scan(tree.expr);
166         scan(tree.body);
167     }
168 
visitLabelled(JCLabeledStatement tree)169     public void visitLabelled(JCLabeledStatement tree) {
170         scan(tree.body);
171     }
172 
visitSwitch(JCSwitch tree)173     public void visitSwitch(JCSwitch tree) {
174         scan(tree.selector);
175         scan(tree.cases);
176     }
177 
visitCase(JCCase tree)178     public void visitCase(JCCase tree) {
179         scan(tree.pats);
180         scan(tree.stats);
181     }
182 
visitSwitchExpression(JCSwitchExpression tree)183     public void visitSwitchExpression(JCSwitchExpression tree) {
184         scan(tree.selector);
185         scan(tree.cases);
186     }
187 
visitSynchronized(JCSynchronized tree)188     public void visitSynchronized(JCSynchronized tree) {
189         scan(tree.lock);
190         scan(tree.body);
191     }
192 
visitTry(JCTry tree)193     public void visitTry(JCTry tree) {
194         scan(tree.resources);
195         scan(tree.body);
196         scan(tree.catchers);
197         scan(tree.finalizer);
198     }
199 
visitCatch(JCCatch tree)200     public void visitCatch(JCCatch tree) {
201         scan(tree.param);
202         scan(tree.body);
203     }
204 
visitConditional(JCConditional tree)205     public void visitConditional(JCConditional tree) {
206         scan(tree.cond);
207         scan(tree.truepart);
208         scan(tree.falsepart);
209     }
210 
visitIf(JCIf tree)211     public void visitIf(JCIf tree) {
212         scan(tree.cond);
213         scan(tree.thenpart);
214         scan(tree.elsepart);
215     }
216 
visitExec(JCExpressionStatement tree)217     public void visitExec(JCExpressionStatement tree) {
218         scan(tree.expr);
219     }
220 
visitBreak(JCBreak tree)221     public void visitBreak(JCBreak tree) {
222         scan(tree.value);
223     }
224 
visitContinue(JCContinue tree)225     public void visitContinue(JCContinue tree) {
226     }
227 
visitReturn(JCReturn tree)228     public void visitReturn(JCReturn tree) {
229         scan(tree.expr);
230     }
231 
visitThrow(JCThrow tree)232     public void visitThrow(JCThrow tree) {
233         scan(tree.expr);
234     }
235 
visitAssert(JCAssert tree)236     public void visitAssert(JCAssert tree) {
237         scan(tree.cond);
238         scan(tree.detail);
239     }
240 
visitApply(JCMethodInvocation tree)241     public void visitApply(JCMethodInvocation tree) {
242         scan(tree.typeargs);
243         scan(tree.meth);
244         scan(tree.args);
245     }
246 
visitNewClass(JCNewClass tree)247     public void visitNewClass(JCNewClass tree) {
248         scan(tree.encl);
249         scan(tree.typeargs);
250         scan(tree.clazz);
251         scan(tree.args);
252         scan(tree.def);
253     }
254 
visitNewArray(JCNewArray tree)255     public void visitNewArray(JCNewArray tree) {
256         scan(tree.annotations);
257         scan(tree.elemtype);
258         scan(tree.dims);
259         for (List<JCAnnotation> annos : tree.dimAnnotations)
260             scan(annos);
261         scan(tree.elems);
262     }
263 
visitLambda(JCLambda tree)264     public void visitLambda(JCLambda tree) {
265         scan(tree.body);
266         scan(tree.params);
267     }
268 
visitParens(JCParens tree)269     public void visitParens(JCParens tree) {
270         scan(tree.expr);
271     }
272 
visitAssign(JCAssign tree)273     public void visitAssign(JCAssign tree) {
274         scan(tree.lhs);
275         scan(tree.rhs);
276     }
277 
visitAssignop(JCAssignOp tree)278     public void visitAssignop(JCAssignOp tree) {
279         scan(tree.lhs);
280         scan(tree.rhs);
281     }
282 
visitUnary(JCUnary tree)283     public void visitUnary(JCUnary tree) {
284         scan(tree.arg);
285     }
286 
visitBinary(JCBinary tree)287     public void visitBinary(JCBinary tree) {
288         scan(tree.lhs);
289         scan(tree.rhs);
290     }
291 
visitTypeCast(JCTypeCast tree)292     public void visitTypeCast(JCTypeCast tree) {
293         scan(tree.clazz);
294         scan(tree.expr);
295     }
296 
visitTypeTest(JCInstanceOf tree)297     public void visitTypeTest(JCInstanceOf tree) {
298         scan(tree.expr);
299         scan(tree.clazz);
300     }
301 
visitIndexed(JCArrayAccess tree)302     public void visitIndexed(JCArrayAccess tree) {
303         scan(tree.indexed);
304         scan(tree.index);
305     }
306 
visitSelect(JCFieldAccess tree)307     public void visitSelect(JCFieldAccess tree) {
308         scan(tree.selected);
309     }
310 
visitReference(JCMemberReference tree)311     public void visitReference(JCMemberReference tree) {
312         scan(tree.expr);
313         scan(tree.typeargs);
314     }
315 
visitIdent(JCIdent tree)316     public void visitIdent(JCIdent tree) {
317     }
318 
visitLiteral(JCLiteral tree)319     public void visitLiteral(JCLiteral tree) {
320     }
321 
visitTypeIdent(JCPrimitiveTypeTree tree)322     public void visitTypeIdent(JCPrimitiveTypeTree tree) {
323     }
324 
visitTypeArray(JCArrayTypeTree tree)325     public void visitTypeArray(JCArrayTypeTree tree) {
326         scan(tree.elemtype);
327     }
328 
visitTypeApply(JCTypeApply tree)329     public void visitTypeApply(JCTypeApply tree) {
330         scan(tree.clazz);
331         scan(tree.arguments);
332     }
333 
visitTypeUnion(JCTypeUnion tree)334     public void visitTypeUnion(JCTypeUnion tree) {
335         scan(tree.alternatives);
336     }
337 
visitTypeIntersection(JCTypeIntersection tree)338     public void visitTypeIntersection(JCTypeIntersection tree) {
339         scan(tree.bounds);
340     }
341 
visitTypeParameter(JCTypeParameter tree)342     public void visitTypeParameter(JCTypeParameter tree) {
343         scan(tree.annotations);
344         scan(tree.bounds);
345     }
346 
347     @Override
visitWildcard(JCWildcard tree)348     public void visitWildcard(JCWildcard tree) {
349         scan(tree.kind);
350         if (tree.inner != null)
351             scan(tree.inner);
352     }
353 
354     @Override
visitTypeBoundKind(TypeBoundKind that)355     public void visitTypeBoundKind(TypeBoundKind that) {
356     }
357 
visitModifiers(JCModifiers tree)358     public void visitModifiers(JCModifiers tree) {
359         scan(tree.annotations);
360     }
361 
visitAnnotation(JCAnnotation tree)362     public void visitAnnotation(JCAnnotation tree) {
363         scan(tree.annotationType);
364         scan(tree.args);
365     }
366 
visitAnnotatedType(JCAnnotatedType tree)367     public void visitAnnotatedType(JCAnnotatedType tree) {
368         scan(tree.annotations);
369         scan(tree.underlyingType);
370     }
371 
visitErroneous(JCErroneous tree)372     public void visitErroneous(JCErroneous tree) {
373     }
374 
visitLetExpr(LetExpr tree)375     public void visitLetExpr(LetExpr tree) {
376         scan(tree.defs);
377         scan(tree.expr);
378     }
379 
visitTree(JCTree tree)380     public void visitTree(JCTree tree) {
381         Assert.error();
382     }
383 }
384