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