1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,415 @@ 1.4 +/* 1.5 + * Copyright 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.tools.javac.tree; 1.30 + 1.31 +import com.sun.source.tree.Tree; 1.32 +import com.sun.source.tree.*; 1.33 +import com.sun.tools.javac.tree.JCTree.*; 1.34 +import com.sun.tools.javac.util.List; 1.35 +import com.sun.tools.javac.util.ListBuffer; 1.36 +import java.util.Map; 1.37 + 1.38 +/** 1.39 + * Creates a copy of a tree, using a given TreeMaker. 1.40 + * Names, literal values, etc are shared with the original. 1.41 + * 1.42 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.43 + * you write code that depends on this, you do so at your own risk. 1.44 + * This code and its internal interfaces are subject to change or 1.45 + * deletion without notice.</b> 1.46 + */ 1.47 +public class TreeCopier<P> implements TreeVisitor<JCTree,P> { 1.48 + private TreeMaker M; 1.49 + 1.50 + /** Creates a new instance of TreeCopier */ 1.51 + public TreeCopier(TreeMaker M) { 1.52 + this.M = M; 1.53 + } 1.54 + 1.55 + public <T extends JCTree> T copy(T tree) { 1.56 + return copy(tree, null); 1.57 + } 1.58 + 1.59 + @SuppressWarnings("unchecked") 1.60 + public <T extends JCTree> T copy(T tree, P p) { 1.61 + if (tree == null) 1.62 + return null; 1.63 + return (T) (tree.accept(this, p)); 1.64 + } 1.65 + 1.66 + public <T extends JCTree> List<T> copy(List<T> trees) { 1.67 + return copy(trees, null); 1.68 + } 1.69 + 1.70 + public <T extends JCTree> List<T> copy(List<T> trees, P p) { 1.71 + if (trees == null) 1.72 + return null; 1.73 + ListBuffer<T> lb = new ListBuffer<T>(); 1.74 + for (T tree: trees) 1.75 + lb.append(copy(tree, p)); 1.76 + return lb.toList(); 1.77 + } 1.78 + 1.79 + public JCTree visitAnnotation(AnnotationTree node, P p) { 1.80 + JCAnnotation t = (JCAnnotation) node; 1.81 + JCTree annotationType = copy(t.annotationType, p); 1.82 + List<JCExpression> args = copy(t.args, p); 1.83 + return M.at(t.pos).Annotation(annotationType, args); 1.84 + } 1.85 + 1.86 + public JCTree visitAssert(AssertTree node, P p) { 1.87 + JCAssert t = (JCAssert) node; 1.88 + JCExpression cond = copy(t.cond, p); 1.89 + JCExpression detail = copy(t.detail, p); 1.90 + return M.at(t.pos).Assert(cond, detail); 1.91 + } 1.92 + 1.93 + public JCTree visitAssignment(AssignmentTree node, P p) { 1.94 + JCAssign t = (JCAssign) node; 1.95 + JCExpression lhs = copy(t.lhs, p); 1.96 + JCExpression rhs = copy(t.rhs, p); 1.97 + return M.at(t.pos).Assign(lhs, rhs); 1.98 + } 1.99 + 1.100 + public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) { 1.101 + JCAssignOp t = (JCAssignOp) node; 1.102 + JCTree lhs = copy(t.lhs, p); 1.103 + JCTree rhs = copy(t.rhs, p); 1.104 + return M.at(t.pos).Assignop(t.getTag(), lhs, rhs); 1.105 + } 1.106 + 1.107 + public JCTree visitBinary(BinaryTree node, P p) { 1.108 + JCBinary t = (JCBinary) node; 1.109 + JCExpression lhs = copy(t.lhs, p); 1.110 + JCExpression rhs = copy(t.rhs, p); 1.111 + return M.at(t.pos).Binary(t.getTag(), lhs, rhs); 1.112 + } 1.113 + 1.114 + public JCTree visitBlock(BlockTree node, P p) { 1.115 + JCBlock t = (JCBlock) node; 1.116 + List<JCStatement> stats = copy(t.stats, p); 1.117 + return M.at(t.pos).Block(t.flags, stats); 1.118 + } 1.119 + 1.120 + public JCTree visitBreak(BreakTree node, P p) { 1.121 + JCBreak t = (JCBreak) node; 1.122 + return M.at(t.pos).Break(t.label); 1.123 + } 1.124 + 1.125 + public JCTree visitCase(CaseTree node, P p) { 1.126 + JCCase t = (JCCase) node; 1.127 + JCExpression pat = copy(t.pat, p); 1.128 + List<JCStatement> stats = copy(t.stats, p); 1.129 + return M.at(t.pos).Case(pat, stats); 1.130 + } 1.131 + 1.132 + public JCTree visitCatch(CatchTree node, P p) { 1.133 + JCCatch t = (JCCatch) node; 1.134 + JCVariableDecl param = copy(t.param, p); 1.135 + JCBlock body = copy(t.body, p); 1.136 + return M.at(t.pos).Catch(param, body); 1.137 + } 1.138 + 1.139 + public JCTree visitClass(ClassTree node, P p) { 1.140 + JCClassDecl t = (JCClassDecl) node; 1.141 + JCModifiers mods = copy(t.mods, p); 1.142 + List<JCTypeParameter> typarams = copy(t.typarams, p); 1.143 + JCTree extending = copy(t.extending, p); 1.144 + List<JCExpression> implementing = copy(t.implementing, p); 1.145 + List<JCTree> defs = copy(t.defs, p); 1.146 + return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs); 1.147 + } 1.148 + 1.149 + public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) { 1.150 + JCConditional t = (JCConditional) node; 1.151 + JCExpression cond = copy(t.cond, p); 1.152 + JCExpression truepart = copy(t.truepart, p); 1.153 + JCExpression falsepart = copy(t.falsepart, p); 1.154 + return M.at(t.pos).Conditional(cond, truepart, falsepart); 1.155 + } 1.156 + 1.157 + public JCTree visitContinue(ContinueTree node, P p) { 1.158 + JCContinue t = (JCContinue) node; 1.159 + return M.at(t.pos).Continue(t.label); 1.160 + } 1.161 + 1.162 + public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) { 1.163 + JCDoWhileLoop t = (JCDoWhileLoop) node; 1.164 + JCStatement body = copy(t.body, p); 1.165 + JCExpression cond = copy(t.cond, p); 1.166 + return M.at(t.pos).DoLoop(body, cond); 1.167 + } 1.168 + 1.169 + public JCTree visitErroneous(ErroneousTree node, P p) { 1.170 + JCErroneous t = (JCErroneous) node; 1.171 + List<? extends JCTree> errs = copy(t.errs, p); 1.172 + return M.at(t.pos).Erroneous(errs); 1.173 + } 1.174 + 1.175 + public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) { 1.176 + JCExpressionStatement t = (JCExpressionStatement) node; 1.177 + JCExpression expr = copy(t.expr, p); 1.178 + return M.at(t.pos).Exec(expr); 1.179 + } 1.180 + 1.181 + public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) { 1.182 + JCEnhancedForLoop t = (JCEnhancedForLoop) node; 1.183 + JCVariableDecl var = copy(t.var, p); 1.184 + JCExpression expr = copy(t.expr, p); 1.185 + JCStatement body = copy(t.body, p); 1.186 + return M.at(t.pos).ForeachLoop(var, expr, body); 1.187 + } 1.188 + 1.189 + public JCTree visitForLoop(ForLoopTree node, P p) { 1.190 + JCForLoop t = (JCForLoop) node; 1.191 + List<JCStatement> init = copy(t.init, p); 1.192 + JCExpression cond = copy(t.cond, p); 1.193 + List<JCExpressionStatement> step = copy(t.step, p); 1.194 + JCStatement body = copy(t.body, p); 1.195 + return M.at(t.pos).ForLoop(init, cond, step, body); 1.196 + } 1.197 + 1.198 + public JCTree visitIdentifier(IdentifierTree node, P p) { 1.199 + JCIdent t = (JCIdent) node; 1.200 + return M.at(t.pos).Ident(t.name); 1.201 + } 1.202 + 1.203 + public JCTree visitIf(IfTree node, P p) { 1.204 + JCIf t = (JCIf) node; 1.205 + JCExpression cond = copy(t.cond, p); 1.206 + JCStatement thenpart = copy(t.thenpart, p); 1.207 + JCStatement elsepart = copy(t.elsepart, p); 1.208 + return M.at(t.pos).If(cond, thenpart, elsepart); 1.209 + } 1.210 + 1.211 + public JCTree visitImport(ImportTree node, P p) { 1.212 + JCImport t = (JCImport) node; 1.213 + JCTree qualid = copy(t.qualid, p); 1.214 + return M.at(t.pos).Import(qualid, t.staticImport); 1.215 + } 1.216 + 1.217 + public JCTree visitArrayAccess(ArrayAccessTree node, P p) { 1.218 + JCArrayAccess t = (JCArrayAccess) node; 1.219 + JCExpression indexed = copy(t.indexed, p); 1.220 + JCExpression index = copy(t.index, p); 1.221 + return M.at(t.pos).Indexed(indexed, index); 1.222 + } 1.223 + 1.224 + public JCTree visitLabeledStatement(LabeledStatementTree node, P p) { 1.225 + JCLabeledStatement t = (JCLabeledStatement) node; 1.226 + JCStatement body = copy(t.body, p); 1.227 + return M.at(t.pos).Labelled(t.label, t.body); 1.228 + } 1.229 + 1.230 + public JCTree visitLiteral(LiteralTree node, P p) { 1.231 + JCLiteral t = (JCLiteral) node; 1.232 + return M.at(t.pos).Literal(t.typetag, t.value); 1.233 + } 1.234 + 1.235 + public JCTree visitMethod(MethodTree node, P p) { 1.236 + JCMethodDecl t = (JCMethodDecl) node; 1.237 + JCModifiers mods = copy(t.mods, p); 1.238 + JCExpression restype = copy(t.restype, p); 1.239 + List<JCTypeParameter> typarams = copy(t.typarams, p); 1.240 + List<JCVariableDecl> params = copy(t.params, p); 1.241 + List<JCExpression> thrown = copy(t.thrown, p); 1.242 + JCBlock body = copy(t.body, p); 1.243 + JCExpression defaultValue = copy(t.defaultValue, p); 1.244 + return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, params, thrown, body, defaultValue); 1.245 + } 1.246 + 1.247 + public JCTree visitMethodInvocation(MethodInvocationTree node, P p) { 1.248 + JCMethodInvocation t = (JCMethodInvocation) node; 1.249 + List<JCExpression> typeargs = copy(t.typeargs, p); 1.250 + JCExpression meth = copy(t.meth, p); 1.251 + List<JCExpression> args = copy(t.args, p); 1.252 + return M.at(t.pos).Apply(typeargs, meth, args); 1.253 + } 1.254 + 1.255 + public JCTree visitModifiers(ModifiersTree node, P p) { 1.256 + JCModifiers t = (JCModifiers) node; 1.257 + List<JCAnnotation> annotations = copy(t.annotations, p); 1.258 + return M.at(t.pos).Modifiers(t.flags, annotations); 1.259 + } 1.260 + 1.261 + public JCTree visitNewArray(NewArrayTree node, P p) { 1.262 + JCNewArray t = (JCNewArray) node; 1.263 + JCExpression elemtype = copy(t.elemtype, p); 1.264 + List<JCExpression> dims = copy(t.dims, p); 1.265 + List<JCExpression> elems = copy(t.elems, p); 1.266 + return M.at(t.pos).NewArray(elemtype, dims, elems); 1.267 + } 1.268 + 1.269 + public JCTree visitNewClass(NewClassTree node, P p) { 1.270 + JCNewClass t = (JCNewClass) node; 1.271 + JCExpression encl = copy(t.encl, p); 1.272 + List<JCExpression> typeargs = copy(t.typeargs, p); 1.273 + JCExpression clazz = copy(t.clazz, p); 1.274 + List<JCExpression> args = copy(t.args, p); 1.275 + JCClassDecl def = copy(t.def, p); 1.276 + return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def); 1.277 + } 1.278 + 1.279 + public JCTree visitParenthesized(ParenthesizedTree node, P p) { 1.280 + JCParens t = (JCParens) node; 1.281 + JCExpression expr = copy(t.expr, p); 1.282 + return M.at(t.pos).Parens(expr); 1.283 + } 1.284 + 1.285 + public JCTree visitReturn(ReturnTree node, P p) { 1.286 + JCReturn t = (JCReturn) node; 1.287 + JCExpression expr = copy(t.expr, p); 1.288 + return M.at(t.pos).Return(expr); 1.289 + } 1.290 + 1.291 + public JCTree visitMemberSelect(MemberSelectTree node, P p) { 1.292 + JCFieldAccess t = (JCFieldAccess) node; 1.293 + JCExpression selected = copy(t.selected, p); 1.294 + return M.at(t.pos).Select(selected, t.name); 1.295 + } 1.296 + 1.297 + public JCTree visitEmptyStatement(EmptyStatementTree node, P p) { 1.298 + JCSkip t = (JCSkip) node; 1.299 + return M.at(t.pos).Skip(); 1.300 + } 1.301 + 1.302 + public JCTree visitSwitch(SwitchTree node, P p) { 1.303 + JCSwitch t = (JCSwitch) node; 1.304 + JCExpression selector = copy(t.selector, p); 1.305 + List<JCCase> cases = copy(t.cases, p); 1.306 + return M.at(t.pos).Switch(selector, cases); 1.307 + } 1.308 + 1.309 + public JCTree visitSynchronized(SynchronizedTree node, P p) { 1.310 + JCSynchronized t = (JCSynchronized) node; 1.311 + JCExpression lock = copy(t.lock, p); 1.312 + JCBlock body = copy(t.body, p); 1.313 + return M.at(t.pos).Synchronized(lock, body); 1.314 + } 1.315 + 1.316 + public JCTree visitThrow(ThrowTree node, P p) { 1.317 + JCThrow t = (JCThrow) node; 1.318 + JCTree expr = copy(t.expr, p); 1.319 + return M.at(t.pos).Throw(expr); 1.320 + } 1.321 + 1.322 + public JCTree visitCompilationUnit(CompilationUnitTree node, P p) { 1.323 + JCCompilationUnit t = (JCCompilationUnit) node; 1.324 + List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p); 1.325 + JCExpression pid = copy(t.pid, p); 1.326 + List<JCTree> defs = copy(t.defs, p); 1.327 + return M.at(t.pos).TopLevel(packageAnnotations, pid, defs); 1.328 + } 1.329 + 1.330 + public JCTree visitTry(TryTree node, P p) { 1.331 + JCTry t = (JCTry) node; 1.332 + JCBlock body = copy(t.body, p); 1.333 + List<JCCatch> catchers = copy(t.catchers, p); 1.334 + JCBlock finalizer = copy(t.finalizer, p); 1.335 + return M.at(t.pos).Try(body, catchers, finalizer); 1.336 + } 1.337 + 1.338 + public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) { 1.339 + JCTypeApply t = (JCTypeApply) node; 1.340 + JCExpression clazz = copy(t.clazz, p); 1.341 + List<JCExpression> arguments = copy(t.arguments, p); 1.342 + return M.at(t.pos).TypeApply(clazz, arguments); 1.343 + } 1.344 + 1.345 + public JCTree visitArrayType(ArrayTypeTree node, P p) { 1.346 + JCArrayTypeTree t = (JCArrayTypeTree) node; 1.347 + JCExpression elemtype = copy(t.elemtype, p); 1.348 + return M.at(t.pos).TypeArray(elemtype); 1.349 + } 1.350 + 1.351 + public JCTree visitTypeCast(TypeCastTree node, P p) { 1.352 + JCTypeCast t = (JCTypeCast) node; 1.353 + JCTree clazz = copy(t.clazz, p); 1.354 + JCExpression expr = copy(t.expr, p); 1.355 + return M.at(t.pos).TypeCast(clazz, expr); 1.356 + } 1.357 + 1.358 + public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) { 1.359 + JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node; 1.360 + return M.at(t.pos).TypeIdent(t.typetag); 1.361 + } 1.362 + 1.363 + public JCTree visitTypeParameter(TypeParameterTree node, P p) { 1.364 + JCTypeParameter t = (JCTypeParameter) node; 1.365 + List<JCExpression> bounds = copy(t.bounds, p); 1.366 + return M.at(t.pos).TypeParameter(t.name, t.bounds); 1.367 + } 1.368 + 1.369 + public JCTree visitInstanceOf(InstanceOfTree node, P p) { 1.370 + JCInstanceOf t = (JCInstanceOf) node; 1.371 + JCExpression expr = copy(t.expr, p); 1.372 + JCTree clazz = copy(t.clazz, p); 1.373 + return M.at(t.pos).TypeTest(expr, clazz); 1.374 + } 1.375 + 1.376 + public JCTree visitUnary(UnaryTree node, P p) { 1.377 + JCUnary t = (JCUnary) node; 1.378 + JCExpression arg = copy(t.arg, p); 1.379 + return M.at(t.pos).Unary(t.getTag(), arg); 1.380 + } 1.381 + 1.382 + public JCTree visitVariable(VariableTree node, P p) { 1.383 + JCVariableDecl t = (JCVariableDecl) node; 1.384 + JCModifiers mods = copy(t.mods, p); 1.385 + JCExpression vartype = copy(t.vartype, p); 1.386 + JCExpression init = copy(t.init, p); 1.387 + return M.at(t.pos).VarDef(mods, t.name, vartype, init); 1.388 + } 1.389 + 1.390 + public JCTree visitWhileLoop(WhileLoopTree node, P p) { 1.391 + JCWhileLoop t = (JCWhileLoop) node; 1.392 + JCStatement body = copy(t.body, p); 1.393 + JCExpression cond = copy(t.cond, p); 1.394 + return M.at(t.pos).WhileLoop(cond, body); 1.395 + } 1.396 + 1.397 + public JCTree visitWildcard(WildcardTree node, P p) { 1.398 + JCWildcard t = (JCWildcard) node; 1.399 + TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind); 1.400 + JCTree inner = copy(t.inner, p); 1.401 + return M.at(t.pos).Wildcard(kind, inner); 1.402 + } 1.403 + 1.404 + public JCTree visitOther(Tree node, P p) { 1.405 + JCTree tree = (JCTree) node; 1.406 + switch (tree.getTag()) { 1.407 + case JCTree.LETEXPR: { 1.408 + LetExpr t = (LetExpr) node; 1.409 + List<JCVariableDecl> defs = copy(t.defs, p); 1.410 + JCTree expr = copy(t.expr, p); 1.411 + return M.at(t.pos).LetExpr(defs, expr); 1.412 + } 1.413 + default: 1.414 + throw new AssertionError("unknown tree tag: " + tree.getTag()); 1.415 + } 1.416 + } 1.417 + 1.418 +}