1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/source/util/TreeScanner.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,381 @@ 1.4 +/* 1.5 + * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.source.util; 1.30 + 1.31 +import com.sun.source.tree.*; 1.32 + 1.33 +/** 1.34 + * A TreeVisitor that visits all the child tree nodes. 1.35 + * To visit nodes of a particular type, just override the 1.36 + * corresponding visitXYZ method. 1.37 + * Inside your method, call super.visitXYZ to visit descendant 1.38 + * nodes. 1.39 + * 1.40 + * <p>The default implementation of the visitXYZ methods will determine 1.41 + * a result as follows: 1.42 + * <ul> 1.43 + * <li>If the node being visited has no children, the result will be null. 1.44 + * <li>If the node being visited has one child, the result will be the 1.45 + * result of calling {@code scan} on that child. The child may be a simple node 1.46 + * or itself a list of nodes. 1.47 + * <li> If the node being visited has more than one child, the result will 1.48 + * be determined by calling {@code scan} each child in turn, and then combining the 1.49 + * result of each scan after the first with the cumulative result 1.50 + * so far, as determined by the {@link #reduce} method. Each child may be either 1.51 + * a simple node of a list of nodes. The default behavior of the {@code reduce} 1.52 + * method is such that the result of the visitXYZ method will be the result of 1.53 + * the last child scanned. 1.54 + * </ul> 1.55 + * 1.56 + * <p>Here is an example to count the number of identifier nodes in a tree: 1.57 + * <pre> 1.58 + * class CountIdentifiers extends TreeScanner<Integer,Void> { 1.59 + * {@literal @}Override 1.60 + * public Integer visitIdentifier(IdentifierTree node, Void p) { 1.61 + * return 1; 1.62 + * } 1.63 + * {@literal @}Override 1.64 + * public Integer reduce(Integer r1, Integer r2) { 1.65 + * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 1.66 + * } 1.67 + * } 1.68 + * </pre> 1.69 + * 1.70 + * @author Peter von der Ahé 1.71 + * @author Jonathan Gibbons 1.72 + * @since 1.6 1.73 + */ 1.74 +public class TreeScanner<R,P> implements TreeVisitor<R,P> { 1.75 + 1.76 + /** Scan a single node. 1.77 + */ 1.78 + public R scan(Tree node, P p) { 1.79 + return (node == null) ? null : node.accept(this, p); 1.80 + } 1.81 + 1.82 + private R scanAndReduce(Tree node, P p, R r) { 1.83 + return reduce(scan(node, p), r); 1.84 + } 1.85 + 1.86 + /** Scan a list of nodes. 1.87 + */ 1.88 + public R scan(Iterable<? extends Tree> nodes, P p) { 1.89 + R r = null; 1.90 + if (nodes != null) { 1.91 + boolean first = true; 1.92 + for (Tree node : nodes) { 1.93 + r = (first ? scan(node, p) : scanAndReduce(node, p, r)); 1.94 + first = false; 1.95 + } 1.96 + } 1.97 + return r; 1.98 + } 1.99 + 1.100 + private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) { 1.101 + return reduce(scan(nodes, p), r); 1.102 + } 1.103 + 1.104 + /** 1.105 + * Reduces two results into a combined result. 1.106 + * The default implementation is to return the first parameter. 1.107 + * The general contract of the method is that it may take any action whatsoever. 1.108 + */ 1.109 + public R reduce(R r1, R r2) { 1.110 + return r1; 1.111 + } 1.112 + 1.113 + 1.114 +/* *************************************************************************** 1.115 + * Visitor methods 1.116 + ****************************************************************************/ 1.117 + 1.118 + public R visitCompilationUnit(CompilationUnitTree node, P p) { 1.119 + R r = scan(node.getPackageAnnotations(), p); 1.120 + r = scanAndReduce(node.getPackageName(), p, r); 1.121 + r = scanAndReduce(node.getImports(), p, r); 1.122 + r = scanAndReduce(node.getTypeDecls(), p, r); 1.123 + return r; 1.124 + } 1.125 + 1.126 + public R visitImport(ImportTree node, P p) { 1.127 + return scan(node.getQualifiedIdentifier(), p); 1.128 + } 1.129 + 1.130 + public R visitClass(ClassTree node, P p) { 1.131 + R r = scan(node.getModifiers(), p); 1.132 + r = scanAndReduce(node.getTypeParameters(), p, r); 1.133 + r = scanAndReduce(node.getExtendsClause(), p, r); 1.134 + r = scanAndReduce(node.getImplementsClause(), p, r); 1.135 + r = scanAndReduce(node.getMembers(), p, r); 1.136 + return r; 1.137 + } 1.138 + 1.139 + public R visitMethod(MethodTree node, P p) { 1.140 + R r = scan(node.getModifiers(), p); 1.141 + r = scanAndReduce(node.getReturnType(), p, r); 1.142 + r = scanAndReduce(node.getTypeParameters(), p, r); 1.143 + r = scanAndReduce(node.getParameters(), p, r); 1.144 + r = scanAndReduce(node.getThrows(), p, r); 1.145 + r = scanAndReduce(node.getBody(), p, r); 1.146 + return r; 1.147 + } 1.148 + 1.149 + public R visitVariable(VariableTree node, P p) { 1.150 + R r = scan(node.getModifiers(), p); 1.151 + r = scanAndReduce(node.getType(), p, r); 1.152 + r = scanAndReduce(node.getInitializer(), p, r); 1.153 + return r; 1.154 + } 1.155 + 1.156 + public R visitEmptyStatement(EmptyStatementTree node, P p) { 1.157 + return null; 1.158 + } 1.159 + 1.160 + public R visitBlock(BlockTree node, P p) { 1.161 + return scan(node.getStatements(), p); 1.162 + } 1.163 + 1.164 + public R visitDoWhileLoop(DoWhileLoopTree node, P p) { 1.165 + R r = scan(node.getStatement(), p); 1.166 + r = scanAndReduce(node.getCondition(), p, r); 1.167 + return r; 1.168 + } 1.169 + 1.170 + public R visitWhileLoop(WhileLoopTree node, P p) { 1.171 + R r = scan(node.getCondition(), p); 1.172 + r = scanAndReduce(node.getStatement(), p, r); 1.173 + return r; 1.174 + } 1.175 + 1.176 + public R visitForLoop(ForLoopTree node, P p) { 1.177 + R r = scan(node.getInitializer(), p); 1.178 + r = scanAndReduce(node.getCondition(), p, r); 1.179 + r = scanAndReduce(node.getUpdate(), p, r); 1.180 + r = scanAndReduce(node.getStatement(), p, r); 1.181 + return r; 1.182 + } 1.183 + 1.184 + public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) { 1.185 + R r = scan(node.getVariable(), p); 1.186 + r = scanAndReduce(node.getExpression(), p, r); 1.187 + r = scanAndReduce(node.getStatement(), p, r); 1.188 + return r; 1.189 + } 1.190 + 1.191 + public R visitLabeledStatement(LabeledStatementTree node, P p) { 1.192 + return scan(node.getStatement(), p); 1.193 + } 1.194 + 1.195 + public R visitSwitch(SwitchTree node, P p) { 1.196 + R r = scan(node.getExpression(), p); 1.197 + r = scanAndReduce(node.getCases(), p, r); 1.198 + return r; 1.199 + } 1.200 + 1.201 + public R visitCase(CaseTree node, P p) { 1.202 + R r = scan(node.getExpression(), p); 1.203 + r = scanAndReduce(node.getStatements(), p, r); 1.204 + return r; 1.205 + } 1.206 + 1.207 + public R visitSynchronized(SynchronizedTree node, P p) { 1.208 + R r = scan(node.getExpression(), p); 1.209 + r = scanAndReduce(node.getBlock(), p, r); 1.210 + return r; 1.211 + } 1.212 + 1.213 + public R visitTry(TryTree node, P p) { 1.214 + R r = scan(node.getBlock(), p); 1.215 + r = scanAndReduce(node.getCatches(), p, r); 1.216 + r = scanAndReduce(node.getFinallyBlock(), p, r); 1.217 + return r; 1.218 + } 1.219 + 1.220 + public R visitCatch(CatchTree node, P p) { 1.221 + R r = scan(node.getParameter(), p); 1.222 + r = scanAndReduce(node.getBlock(), p, r); 1.223 + return r; 1.224 + } 1.225 + 1.226 + public R visitConditionalExpression(ConditionalExpressionTree node, P p) { 1.227 + R r = scan(node.getCondition(), p); 1.228 + r = scanAndReduce(node.getTrueExpression(), p, r); 1.229 + r = scanAndReduce(node.getFalseExpression(), p, r); 1.230 + return r; 1.231 + } 1.232 + 1.233 + public R visitIf(IfTree node, P p) { 1.234 + R r = scan(node.getCondition(), p); 1.235 + r = scanAndReduce(node.getThenStatement(), p, r); 1.236 + r = scanAndReduce(node.getElseStatement(), p, r); 1.237 + return r; 1.238 + } 1.239 + 1.240 + public R visitExpressionStatement(ExpressionStatementTree node, P p) { 1.241 + return scan(node.getExpression(), p); 1.242 + } 1.243 + 1.244 + public R visitBreak(BreakTree node, P p) { 1.245 + return null; 1.246 + } 1.247 + 1.248 + public R visitContinue(ContinueTree node, P p) { 1.249 + return null; 1.250 + } 1.251 + 1.252 + public R visitReturn(ReturnTree node, P p) { 1.253 + return scan(node.getExpression(), p); 1.254 + } 1.255 + 1.256 + public R visitThrow(ThrowTree node, P p) { 1.257 + return scan(node.getExpression(), p); 1.258 + } 1.259 + 1.260 + public R visitAssert(AssertTree node, P p) { 1.261 + R r = scan(node.getCondition(), p); 1.262 + r = scanAndReduce(node.getDetail(), p, r); 1.263 + return r; 1.264 + } 1.265 + 1.266 + public R visitMethodInvocation(MethodInvocationTree node, P p) { 1.267 + R r = scan(node.getTypeArguments(), p); 1.268 + r = scanAndReduce(node.getMethodSelect(), p, r); 1.269 + r = scanAndReduce(node.getArguments(), p, r); 1.270 + return r; 1.271 + } 1.272 + 1.273 + public R visitNewClass(NewClassTree node, P p) { 1.274 + R r = scan(node.getEnclosingExpression(), p); 1.275 + r = scanAndReduce(node.getIdentifier(), p, r); 1.276 + r = scanAndReduce(node.getTypeArguments(), p, r); 1.277 + r = scanAndReduce(node.getArguments(), p, r); 1.278 + r = scanAndReduce(node.getClassBody(), p, r); 1.279 + return r; 1.280 + } 1.281 + 1.282 + public R visitNewArray(NewArrayTree node, P p) { 1.283 + R r = scan(node.getType(), p); 1.284 + r = scanAndReduce(node.getDimensions(), p, r); 1.285 + r = scanAndReduce(node.getInitializers(), p, r); 1.286 + return r; 1.287 + } 1.288 + 1.289 + public R visitParenthesized(ParenthesizedTree node, P p) { 1.290 + return scan(node.getExpression(), p); 1.291 + } 1.292 + 1.293 + public R visitAssignment(AssignmentTree node, P p) { 1.294 + R r = scan(node.getVariable(), p); 1.295 + r = scanAndReduce(node.getExpression(), p, r); 1.296 + return r; 1.297 + } 1.298 + 1.299 + public R visitCompoundAssignment(CompoundAssignmentTree node, P p) { 1.300 + R r = scan(node.getVariable(), p); 1.301 + r = scanAndReduce(node.getExpression(), p, r); 1.302 + return r; 1.303 + } 1.304 + 1.305 + public R visitUnary(UnaryTree node, P p) { 1.306 + return scan(node.getExpression(), p); 1.307 + } 1.308 + 1.309 + public R visitBinary(BinaryTree node, P p) { 1.310 + R r = scan(node.getLeftOperand(), p); 1.311 + r = scanAndReduce(node.getRightOperand(), p, r); 1.312 + return r; 1.313 + } 1.314 + 1.315 + public R visitTypeCast(TypeCastTree node, P p) { 1.316 + R r = scan(node.getType(), p); 1.317 + r = scanAndReduce(node.getExpression(), p, r); 1.318 + return r; 1.319 + } 1.320 + 1.321 + public R visitInstanceOf(InstanceOfTree node, P p) { 1.322 + R r = scan(node.getExpression(), p); 1.323 + r = scanAndReduce(node.getType(), p, r); 1.324 + return r; 1.325 + } 1.326 + 1.327 + public R visitArrayAccess(ArrayAccessTree node, P p) { 1.328 + R r = scan(node.getExpression(), p); 1.329 + r = scanAndReduce(node.getIndex(), p, r); 1.330 + return r; 1.331 + } 1.332 + 1.333 + public R visitMemberSelect(MemberSelectTree node, P p) { 1.334 + return scan(node.getExpression(), p); 1.335 + } 1.336 + 1.337 + public R visitIdentifier(IdentifierTree node, P p) { 1.338 + return null; 1.339 + } 1.340 + 1.341 + public R visitLiteral(LiteralTree node, P p) { 1.342 + return null; 1.343 + } 1.344 + 1.345 + public R visitPrimitiveType(PrimitiveTypeTree node, P p) { 1.346 + return null; 1.347 + } 1.348 + 1.349 + public R visitArrayType(ArrayTypeTree node, P p) { 1.350 + return scan(node.getType(), p); 1.351 + } 1.352 + 1.353 + public R visitParameterizedType(ParameterizedTypeTree node, P p) { 1.354 + R r = scan(node.getType(), p); 1.355 + r = scanAndReduce(node.getTypeArguments(), p, r); 1.356 + return r; 1.357 + } 1.358 + 1.359 + public R visitTypeParameter(TypeParameterTree node, P p) { 1.360 + return scan(node.getBounds(), p); 1.361 + } 1.362 + 1.363 + public R visitWildcard(WildcardTree node, P p) { 1.364 + return scan(node.getBound(), p); 1.365 + } 1.366 + 1.367 + public R visitModifiers(ModifiersTree node, P p) { 1.368 + return scan(node.getAnnotations(), p); 1.369 + } 1.370 + 1.371 + public R visitAnnotation(AnnotationTree node, P p) { 1.372 + R r = scan(node.getAnnotationType(), p); 1.373 + r = scanAndReduce(node.getArguments(), p, r); 1.374 + return r; 1.375 + } 1.376 + 1.377 + public R visitOther(Tree node, P p) { 1.378 + return null; 1.379 + } 1.380 + 1.381 + public R visitErroneous(ErroneousTree node, P p) { 1.382 + return null; 1.383 + } 1.384 +}