| 1 | /* |
| 2 | * Copyright 2001-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 API supported by Sun Microsystems. If |
| 40 | * 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.packageAnnotations); |
| 67 | scan(tree.pid); |
| 68 | scan(tree.defs); |
| 69 | } |
| 70 | |
| 71 | public void visitImport(JCImport tree) { |
| 72 | scan(tree.qualid); |
| 73 | } |
| 74 | |
| 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 | |
| 83 | public void visitMethodDef(JCMethodDecl tree) { |
| 84 | scan(tree.mods); |
| 85 | scan(tree.restype); |
| 86 | scan(tree.typarams); |
| 87 | scan(tree.params); |
| 88 | scan(tree.thrown); |
| 89 | scan(tree.body); |
| 90 | } |
| 91 | |
| 92 | public void visitVarDef(JCVariableDecl tree) { |
| 93 | scan(tree.mods); |
| 94 | scan(tree.vartype); |
| 95 | scan(tree.init); |
| 96 | } |
| 97 | |
| 98 | public void visitSkip(JCSkip tree) { |
| 99 | } |
| 100 | |
| 101 | public void visitBlock(JCBlock tree) { |
| 102 | scan(tree.stats); |
| 103 | } |
| 104 | |
| 105 | public void visitDoLoop(JCDoWhileLoop tree) { |
| 106 | scan(tree.body); |
| 107 | scan(tree.cond); |
| 108 | } |
| 109 | |
| 110 | public void visitWhileLoop(JCWhileLoop tree) { |
| 111 | scan(tree.cond); |
| 112 | scan(tree.body); |
| 113 | } |
| 114 | |
| 115 | public void visitForLoop(JCForLoop tree) { |
| 116 | scan(tree.init); |
| 117 | scan(tree.cond); |
| 118 | scan(tree.step); |
| 119 | scan(tree.body); |
| 120 | } |
| 121 | |
| 122 | public void visitForeachLoop(JCEnhancedForLoop tree) { |
| 123 | scan(tree.var); |
| 124 | scan(tree.expr); |
| 125 | scan(tree.body); |
| 126 | } |
| 127 | |
| 128 | public void visitLabelled(JCLabeledStatement tree) { |
| 129 | scan(tree.body); |
| 130 | } |
| 131 | |
| 132 | public void visitSwitch(JCSwitch tree) { |
| 133 | scan(tree.selector); |
| 134 | scan(tree.cases); |
| 135 | } |
| 136 | |
| 137 | public void visitCase(JCCase tree) { |
| 138 | scan(tree.pat); |
| 139 | scan(tree.stats); |
| 140 | } |
| 141 | |
| 142 | public void visitSynchronized(JCSynchronized tree) { |
| 143 | scan(tree.lock); |
| 144 | scan(tree.body); |
| 145 | } |
| 146 | |
| 147 | public void visitTry(JCTry tree) { |
| 148 | scan(tree.body); |
| 149 | scan(tree.catchers); |
| 150 | scan(tree.finalizer); |
| 151 | } |
| 152 | |
| 153 | public void visitCatch(JCCatch tree) { |
| 154 | scan(tree.param); |
| 155 | scan(tree.body); |
| 156 | } |
| 157 | |
| 158 | public void visitConditional(JCConditional tree) { |
| 159 | scan(tree.cond); |
| 160 | scan(tree.truepart); |
| 161 | scan(tree.falsepart); |
| 162 | } |
| 163 | |
| 164 | public void visitIf(JCIf tree) { |
| 165 | scan(tree.cond); |
| 166 | scan(tree.thenpart); |
| 167 | scan(tree.elsepart); |
| 168 | } |
| 169 | |
| 170 | public void visitExec(JCExpressionStatement tree) { |
| 171 | scan(tree.expr); |
| 172 | } |
| 173 | |
| 174 | public void visitBreak(JCBreak tree) { |
| 175 | } |
| 176 | |
| 177 | public void visitContinue(JCContinue tree) { |
| 178 | } |
| 179 | |
| 180 | public void visitReturn(JCReturn tree) { |
| 181 | scan(tree.expr); |
| 182 | } |
| 183 | |
| 184 | public void visitThrow(JCThrow tree) { |
| 185 | scan(tree.expr); |
| 186 | } |
| 187 | |
| 188 | public void visitAssert(JCAssert tree) { |
| 189 | scan(tree.cond); |
| 190 | scan(tree.detail); |
| 191 | } |
| 192 | |
| 193 | public void visitApply(JCMethodInvocation tree) { |
| 194 | scan(tree.meth); |
| 195 | scan(tree.args); |
| 196 | } |
| 197 | |
| 198 | public void visitNewClass(JCNewClass tree) { |
| 199 | scan(tree.encl); |
| 200 | scan(tree.clazz); |
| 201 | scan(tree.args); |
| 202 | scan(tree.def); |
| 203 | } |
| 204 | |
| 205 | public void visitNewArray(JCNewArray tree) { |
| 206 | scan(tree.elemtype); |
| 207 | scan(tree.dims); |
| 208 | scan(tree.elems); |
| 209 | } |
| 210 | |
| 211 | public void visitParens(JCParens tree) { |
| 212 | scan(tree.expr); |
| 213 | } |
| 214 | |
| 215 | public void visitAssign(JCAssign tree) { |
| 216 | scan(tree.lhs); |
| 217 | scan(tree.rhs); |
| 218 | } |
| 219 | |
| 220 | public void visitAssignop(JCAssignOp tree) { |
| 221 | scan(tree.lhs); |
| 222 | scan(tree.rhs); |
| 223 | } |
| 224 | |
| 225 | public void visitUnary(JCUnary tree) { |
| 226 | scan(tree.arg); |
| 227 | } |
| 228 | |
| 229 | public void visitBinary(JCBinary tree) { |
| 230 | scan(tree.lhs); |
| 231 | scan(tree.rhs); |
| 232 | } |
| 233 | |
| 234 | public void visitTypeCast(JCTypeCast tree) { |
| 235 | scan(tree.clazz); |
| 236 | scan(tree.expr); |
| 237 | } |
| 238 | |
| 239 | public void visitTypeTest(JCInstanceOf tree) { |
| 240 | scan(tree.expr); |
| 241 | scan(tree.clazz); |
| 242 | } |
| 243 | |
| 244 | public void visitIndexed(JCArrayAccess tree) { |
| 245 | scan(tree.indexed); |
| 246 | scan(tree.index); |
| 247 | } |
| 248 | |
| 249 | public void visitSelect(JCFieldAccess tree) { |
| 250 | scan(tree.selected); |
| 251 | } |
| 252 | |
| 253 | public void visitIdent(JCIdent tree) { |
| 254 | } |
| 255 | |
| 256 | public void visitLiteral(JCLiteral tree) { |
| 257 | } |
| 258 | |
| 259 | public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
| 260 | } |
| 261 | |
| 262 | public void visitTypeArray(JCArrayTypeTree tree) { |
| 263 | scan(tree.elemtype); |
| 264 | } |
| 265 | |
| 266 | public void visitTypeApply(JCTypeApply tree) { |
| 267 | scan(tree.clazz); |
| 268 | scan(tree.arguments); |
| 269 | } |
| 270 | |
| 271 | public void visitTypeParameter(JCTypeParameter tree) { |
| 272 | scan(tree.bounds); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void visitWildcard(JCWildcard tree) { |
| 277 | scan(tree.kind); |
| 278 | if (tree.inner != null) |
| 279 | scan(tree.inner); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void visitTypeBoundKind(TypeBoundKind that) { |
| 284 | } |
| 285 | |
| 286 | public void visitModifiers(JCModifiers tree) { |
| 287 | scan(tree.annotations); |
| 288 | } |
| 289 | |
| 290 | public void visitAnnotation(JCAnnotation tree) { |
| 291 | scan(tree.annotationType); |
| 292 | scan(tree.args); |
| 293 | } |
| 294 | |
| 295 | public void visitErroneous(JCErroneous tree) { |
| 296 | } |
| 297 | |
| 298 | public void visitLetExpr(LetExpr tree) { |
| 299 | scan(tree.defs); |
| 300 | scan(tree.expr); |
| 301 | } |
| 302 | |
| 303 | public void visitTree(JCTree tree) { |
| 304 | assert false; |
| 305 | } |
| 306 | } |