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