src/share/classes/com/sun/tools/javac/tree/TreeCopier.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 550
a6f2911a7c55
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.tree;
duke@1 27
duke@1 28 import com.sun.source.tree.*;
duke@1 29 import com.sun.tools.javac.tree.JCTree.*;
duke@1 30 import com.sun.tools.javac.util.List;
duke@1 31 import com.sun.tools.javac.util.ListBuffer;
duke@1 32
duke@1 33 /**
duke@1 34 * Creates a copy of a tree, using a given TreeMaker.
duke@1 35 * Names, literal values, etc are shared with the original.
duke@1 36 *
duke@1 37 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 38 * you write code that depends on this, you do so at your own risk.
duke@1 39 * This code and its internal interfaces are subject to change or
duke@1 40 * deletion without notice.</b>
duke@1 41 */
duke@1 42 public class TreeCopier<P> implements TreeVisitor<JCTree,P> {
duke@1 43 private TreeMaker M;
duke@1 44
duke@1 45 /** Creates a new instance of TreeCopier */
duke@1 46 public TreeCopier(TreeMaker M) {
duke@1 47 this.M = M;
duke@1 48 }
duke@1 49
duke@1 50 public <T extends JCTree> T copy(T tree) {
duke@1 51 return copy(tree, null);
duke@1 52 }
duke@1 53
duke@1 54 @SuppressWarnings("unchecked")
duke@1 55 public <T extends JCTree> T copy(T tree, P p) {
duke@1 56 if (tree == null)
duke@1 57 return null;
duke@1 58 return (T) (tree.accept(this, p));
duke@1 59 }
duke@1 60
duke@1 61 public <T extends JCTree> List<T> copy(List<T> trees) {
duke@1 62 return copy(trees, null);
duke@1 63 }
duke@1 64
duke@1 65 public <T extends JCTree> List<T> copy(List<T> trees, P p) {
duke@1 66 if (trees == null)
duke@1 67 return null;
duke@1 68 ListBuffer<T> lb = new ListBuffer<T>();
duke@1 69 for (T tree: trees)
duke@1 70 lb.append(copy(tree, p));
duke@1 71 return lb.toList();
duke@1 72 }
duke@1 73
jjg@308 74 public JCTree visitAnnotatedType(AnnotatedTypeTree node, P p) {
jjg@308 75 JCAnnotatedType t = (JCAnnotatedType) node;
jjg@308 76 List<JCTypeAnnotation> annotations = copy(t.annotations, p);
jjg@308 77 JCExpression underlyingType = copy(t.underlyingType, p);
jjg@308 78 return M.at(t.pos).AnnotatedType(annotations, underlyingType);
jjg@308 79 }
jjg@308 80
duke@1 81 public JCTree visitAnnotation(AnnotationTree node, P p) {
duke@1 82 JCAnnotation t = (JCAnnotation) node;
duke@1 83 JCTree annotationType = copy(t.annotationType, p);
duke@1 84 List<JCExpression> args = copy(t.args, p);
duke@1 85 return M.at(t.pos).Annotation(annotationType, args);
duke@1 86 }
duke@1 87
duke@1 88 public JCTree visitAssert(AssertTree node, P p) {
duke@1 89 JCAssert t = (JCAssert) node;
duke@1 90 JCExpression cond = copy(t.cond, p);
duke@1 91 JCExpression detail = copy(t.detail, p);
duke@1 92 return M.at(t.pos).Assert(cond, detail);
duke@1 93 }
duke@1 94
duke@1 95 public JCTree visitAssignment(AssignmentTree node, P p) {
duke@1 96 JCAssign t = (JCAssign) node;
duke@1 97 JCExpression lhs = copy(t.lhs, p);
duke@1 98 JCExpression rhs = copy(t.rhs, p);
duke@1 99 return M.at(t.pos).Assign(lhs, rhs);
duke@1 100 }
duke@1 101
duke@1 102 public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
duke@1 103 JCAssignOp t = (JCAssignOp) node;
duke@1 104 JCTree lhs = copy(t.lhs, p);
duke@1 105 JCTree rhs = copy(t.rhs, p);
duke@1 106 return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
duke@1 107 }
duke@1 108
duke@1 109 public JCTree visitBinary(BinaryTree node, P p) {
duke@1 110 JCBinary t = (JCBinary) node;
duke@1 111 JCExpression lhs = copy(t.lhs, p);
duke@1 112 JCExpression rhs = copy(t.rhs, p);
duke@1 113 return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
duke@1 114 }
duke@1 115
duke@1 116 public JCTree visitBlock(BlockTree node, P p) {
duke@1 117 JCBlock t = (JCBlock) node;
duke@1 118 List<JCStatement> stats = copy(t.stats, p);
duke@1 119 return M.at(t.pos).Block(t.flags, stats);
duke@1 120 }
duke@1 121
duke@1 122 public JCTree visitBreak(BreakTree node, P p) {
duke@1 123 JCBreak t = (JCBreak) node;
duke@1 124 return M.at(t.pos).Break(t.label);
duke@1 125 }
duke@1 126
duke@1 127 public JCTree visitCase(CaseTree node, P p) {
duke@1 128 JCCase t = (JCCase) node;
duke@1 129 JCExpression pat = copy(t.pat, p);
duke@1 130 List<JCStatement> stats = copy(t.stats, p);
duke@1 131 return M.at(t.pos).Case(pat, stats);
duke@1 132 }
duke@1 133
duke@1 134 public JCTree visitCatch(CatchTree node, P p) {
duke@1 135 JCCatch t = (JCCatch) node;
duke@1 136 JCVariableDecl param = copy(t.param, p);
duke@1 137 JCBlock body = copy(t.body, p);
duke@1 138 return M.at(t.pos).Catch(param, body);
duke@1 139 }
duke@1 140
duke@1 141 public JCTree visitClass(ClassTree node, P p) {
duke@1 142 JCClassDecl t = (JCClassDecl) node;
duke@1 143 JCModifiers mods = copy(t.mods, p);
duke@1 144 List<JCTypeParameter> typarams = copy(t.typarams, p);
duke@1 145 JCTree extending = copy(t.extending, p);
duke@1 146 List<JCExpression> implementing = copy(t.implementing, p);
duke@1 147 List<JCTree> defs = copy(t.defs, p);
duke@1 148 return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
duke@1 149 }
duke@1 150
duke@1 151 public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
duke@1 152 JCConditional t = (JCConditional) node;
duke@1 153 JCExpression cond = copy(t.cond, p);
duke@1 154 JCExpression truepart = copy(t.truepart, p);
duke@1 155 JCExpression falsepart = copy(t.falsepart, p);
duke@1 156 return M.at(t.pos).Conditional(cond, truepart, falsepart);
duke@1 157 }
duke@1 158
duke@1 159 public JCTree visitContinue(ContinueTree node, P p) {
duke@1 160 JCContinue t = (JCContinue) node;
duke@1 161 return M.at(t.pos).Continue(t.label);
duke@1 162 }
duke@1 163
duke@1 164 public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
duke@1 165 JCDoWhileLoop t = (JCDoWhileLoop) node;
duke@1 166 JCStatement body = copy(t.body, p);
duke@1 167 JCExpression cond = copy(t.cond, p);
duke@1 168 return M.at(t.pos).DoLoop(body, cond);
duke@1 169 }
duke@1 170
duke@1 171 public JCTree visitErroneous(ErroneousTree node, P p) {
duke@1 172 JCErroneous t = (JCErroneous) node;
duke@1 173 List<? extends JCTree> errs = copy(t.errs, p);
duke@1 174 return M.at(t.pos).Erroneous(errs);
duke@1 175 }
duke@1 176
duke@1 177 public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
duke@1 178 JCExpressionStatement t = (JCExpressionStatement) node;
duke@1 179 JCExpression expr = copy(t.expr, p);
duke@1 180 return M.at(t.pos).Exec(expr);
duke@1 181 }
duke@1 182
duke@1 183 public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
duke@1 184 JCEnhancedForLoop t = (JCEnhancedForLoop) node;
duke@1 185 JCVariableDecl var = copy(t.var, p);
duke@1 186 JCExpression expr = copy(t.expr, p);
duke@1 187 JCStatement body = copy(t.body, p);
duke@1 188 return M.at(t.pos).ForeachLoop(var, expr, body);
duke@1 189 }
duke@1 190
duke@1 191 public JCTree visitForLoop(ForLoopTree node, P p) {
duke@1 192 JCForLoop t = (JCForLoop) node;
duke@1 193 List<JCStatement> init = copy(t.init, p);
duke@1 194 JCExpression cond = copy(t.cond, p);
duke@1 195 List<JCExpressionStatement> step = copy(t.step, p);
duke@1 196 JCStatement body = copy(t.body, p);
duke@1 197 return M.at(t.pos).ForLoop(init, cond, step, body);
duke@1 198 }
duke@1 199
duke@1 200 public JCTree visitIdentifier(IdentifierTree node, P p) {
duke@1 201 JCIdent t = (JCIdent) node;
duke@1 202 return M.at(t.pos).Ident(t.name);
duke@1 203 }
duke@1 204
duke@1 205 public JCTree visitIf(IfTree node, P p) {
duke@1 206 JCIf t = (JCIf) node;
duke@1 207 JCExpression cond = copy(t.cond, p);
duke@1 208 JCStatement thenpart = copy(t.thenpart, p);
duke@1 209 JCStatement elsepart = copy(t.elsepart, p);
duke@1 210 return M.at(t.pos).If(cond, thenpart, elsepart);
duke@1 211 }
duke@1 212
duke@1 213 public JCTree visitImport(ImportTree node, P p) {
duke@1 214 JCImport t = (JCImport) node;
duke@1 215 JCTree qualid = copy(t.qualid, p);
duke@1 216 return M.at(t.pos).Import(qualid, t.staticImport);
duke@1 217 }
duke@1 218
duke@1 219 public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
duke@1 220 JCArrayAccess t = (JCArrayAccess) node;
duke@1 221 JCExpression indexed = copy(t.indexed, p);
duke@1 222 JCExpression index = copy(t.index, p);
duke@1 223 return M.at(t.pos).Indexed(indexed, index);
duke@1 224 }
duke@1 225
duke@1 226 public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
duke@1 227 JCLabeledStatement t = (JCLabeledStatement) node;
duke@1 228 JCStatement body = copy(t.body, p);
duke@1 229 return M.at(t.pos).Labelled(t.label, t.body);
duke@1 230 }
duke@1 231
duke@1 232 public JCTree visitLiteral(LiteralTree node, P p) {
duke@1 233 JCLiteral t = (JCLiteral) node;
duke@1 234 return M.at(t.pos).Literal(t.typetag, t.value);
duke@1 235 }
duke@1 236
duke@1 237 public JCTree visitMethod(MethodTree node, P p) {
duke@1 238 JCMethodDecl t = (JCMethodDecl) node;
duke@1 239 JCModifiers mods = copy(t.mods, p);
duke@1 240 JCExpression restype = copy(t.restype, p);
duke@1 241 List<JCTypeParameter> typarams = copy(t.typarams, p);
duke@1 242 List<JCVariableDecl> params = copy(t.params, p);
jjg@308 243 List<JCTypeAnnotation> receiver = copy(t.receiverAnnotations, p);
duke@1 244 List<JCExpression> thrown = copy(t.thrown, p);
duke@1 245 JCBlock body = copy(t.body, p);
duke@1 246 JCExpression defaultValue = copy(t.defaultValue, p);
jjg@308 247 return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, params, receiver, thrown, body, defaultValue);
duke@1 248 }
duke@1 249
duke@1 250 public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
duke@1 251 JCMethodInvocation t = (JCMethodInvocation) node;
duke@1 252 List<JCExpression> typeargs = copy(t.typeargs, p);
duke@1 253 JCExpression meth = copy(t.meth, p);
duke@1 254 List<JCExpression> args = copy(t.args, p);
duke@1 255 return M.at(t.pos).Apply(typeargs, meth, args);
duke@1 256 }
duke@1 257
duke@1 258 public JCTree visitModifiers(ModifiersTree node, P p) {
duke@1 259 JCModifiers t = (JCModifiers) node;
duke@1 260 List<JCAnnotation> annotations = copy(t.annotations, p);
duke@1 261 return M.at(t.pos).Modifiers(t.flags, annotations);
duke@1 262 }
duke@1 263
duke@1 264 public JCTree visitNewArray(NewArrayTree node, P p) {
duke@1 265 JCNewArray t = (JCNewArray) node;
duke@1 266 JCExpression elemtype = copy(t.elemtype, p);
duke@1 267 List<JCExpression> dims = copy(t.dims, p);
duke@1 268 List<JCExpression> elems = copy(t.elems, p);
duke@1 269 return M.at(t.pos).NewArray(elemtype, dims, elems);
duke@1 270 }
duke@1 271
duke@1 272 public JCTree visitNewClass(NewClassTree node, P p) {
duke@1 273 JCNewClass t = (JCNewClass) node;
duke@1 274 JCExpression encl = copy(t.encl, p);
duke@1 275 List<JCExpression> typeargs = copy(t.typeargs, p);
duke@1 276 JCExpression clazz = copy(t.clazz, p);
duke@1 277 List<JCExpression> args = copy(t.args, p);
duke@1 278 JCClassDecl def = copy(t.def, p);
duke@1 279 return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
duke@1 280 }
duke@1 281
duke@1 282 public JCTree visitParenthesized(ParenthesizedTree node, P p) {
duke@1 283 JCParens t = (JCParens) node;
duke@1 284 JCExpression expr = copy(t.expr, p);
duke@1 285 return M.at(t.pos).Parens(expr);
duke@1 286 }
duke@1 287
duke@1 288 public JCTree visitReturn(ReturnTree node, P p) {
duke@1 289 JCReturn t = (JCReturn) node;
duke@1 290 JCExpression expr = copy(t.expr, p);
duke@1 291 return M.at(t.pos).Return(expr);
duke@1 292 }
duke@1 293
duke@1 294 public JCTree visitMemberSelect(MemberSelectTree node, P p) {
duke@1 295 JCFieldAccess t = (JCFieldAccess) node;
duke@1 296 JCExpression selected = copy(t.selected, p);
duke@1 297 return M.at(t.pos).Select(selected, t.name);
duke@1 298 }
duke@1 299
duke@1 300 public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
duke@1 301 JCSkip t = (JCSkip) node;
duke@1 302 return M.at(t.pos).Skip();
duke@1 303 }
duke@1 304
duke@1 305 public JCTree visitSwitch(SwitchTree node, P p) {
duke@1 306 JCSwitch t = (JCSwitch) node;
duke@1 307 JCExpression selector = copy(t.selector, p);
duke@1 308 List<JCCase> cases = copy(t.cases, p);
duke@1 309 return M.at(t.pos).Switch(selector, cases);
duke@1 310 }
duke@1 311
duke@1 312 public JCTree visitSynchronized(SynchronizedTree node, P p) {
duke@1 313 JCSynchronized t = (JCSynchronized) node;
duke@1 314 JCExpression lock = copy(t.lock, p);
duke@1 315 JCBlock body = copy(t.body, p);
duke@1 316 return M.at(t.pos).Synchronized(lock, body);
duke@1 317 }
duke@1 318
duke@1 319 public JCTree visitThrow(ThrowTree node, P p) {
duke@1 320 JCThrow t = (JCThrow) node;
duke@1 321 JCTree expr = copy(t.expr, p);
duke@1 322 return M.at(t.pos).Throw(expr);
duke@1 323 }
duke@1 324
duke@1 325 public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
duke@1 326 JCCompilationUnit t = (JCCompilationUnit) node;
duke@1 327 List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p);
duke@1 328 JCExpression pid = copy(t.pid, p);
duke@1 329 List<JCTree> defs = copy(t.defs, p);
duke@1 330 return M.at(t.pos).TopLevel(packageAnnotations, pid, defs);
duke@1 331 }
duke@1 332
duke@1 333 public JCTree visitTry(TryTree node, P p) {
duke@1 334 JCTry t = (JCTry) node;
duke@1 335 JCBlock body = copy(t.body, p);
duke@1 336 List<JCCatch> catchers = copy(t.catchers, p);
duke@1 337 JCBlock finalizer = copy(t.finalizer, p);
duke@1 338 return M.at(t.pos).Try(body, catchers, finalizer);
duke@1 339 }
duke@1 340
duke@1 341 public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
duke@1 342 JCTypeApply t = (JCTypeApply) node;
duke@1 343 JCExpression clazz = copy(t.clazz, p);
duke@1 344 List<JCExpression> arguments = copy(t.arguments, p);
duke@1 345 return M.at(t.pos).TypeApply(clazz, arguments);
duke@1 346 }
duke@1 347
mcimadamore@550 348 public JCTree visitDisjointType(DisjointTypeTree node, P p) {
mcimadamore@550 349 JCTypeDisjoint t = (JCTypeDisjoint) node;
mcimadamore@550 350 List<JCExpression> components = copy(t.components, p);
mcimadamore@550 351 return M.at(t.pos).TypeDisjoint(components);
mcimadamore@550 352 }
mcimadamore@550 353
duke@1 354 public JCTree visitArrayType(ArrayTypeTree node, P p) {
duke@1 355 JCArrayTypeTree t = (JCArrayTypeTree) node;
duke@1 356 JCExpression elemtype = copy(t.elemtype, p);
duke@1 357 return M.at(t.pos).TypeArray(elemtype);
duke@1 358 }
duke@1 359
duke@1 360 public JCTree visitTypeCast(TypeCastTree node, P p) {
duke@1 361 JCTypeCast t = (JCTypeCast) node;
duke@1 362 JCTree clazz = copy(t.clazz, p);
duke@1 363 JCExpression expr = copy(t.expr, p);
duke@1 364 return M.at(t.pos).TypeCast(clazz, expr);
duke@1 365 }
duke@1 366
duke@1 367 public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
duke@1 368 JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
duke@1 369 return M.at(t.pos).TypeIdent(t.typetag);
duke@1 370 }
duke@1 371
duke@1 372 public JCTree visitTypeParameter(TypeParameterTree node, P p) {
duke@1 373 JCTypeParameter t = (JCTypeParameter) node;
jjg@308 374 List<JCTypeAnnotation> annos = copy(t.annotations, p);
duke@1 375 List<JCExpression> bounds = copy(t.bounds, p);
jjg@308 376 return M.at(t.pos).TypeParameter(t.name, bounds, annos);
duke@1 377 }
duke@1 378
duke@1 379 public JCTree visitInstanceOf(InstanceOfTree node, P p) {
duke@1 380 JCInstanceOf t = (JCInstanceOf) node;
duke@1 381 JCExpression expr = copy(t.expr, p);
duke@1 382 JCTree clazz = copy(t.clazz, p);
duke@1 383 return M.at(t.pos).TypeTest(expr, clazz);
duke@1 384 }
duke@1 385
duke@1 386 public JCTree visitUnary(UnaryTree node, P p) {
duke@1 387 JCUnary t = (JCUnary) node;
duke@1 388 JCExpression arg = copy(t.arg, p);
duke@1 389 return M.at(t.pos).Unary(t.getTag(), arg);
duke@1 390 }
duke@1 391
duke@1 392 public JCTree visitVariable(VariableTree node, P p) {
duke@1 393 JCVariableDecl t = (JCVariableDecl) node;
duke@1 394 JCModifiers mods = copy(t.mods, p);
duke@1 395 JCExpression vartype = copy(t.vartype, p);
duke@1 396 JCExpression init = copy(t.init, p);
duke@1 397 return M.at(t.pos).VarDef(mods, t.name, vartype, init);
duke@1 398 }
duke@1 399
duke@1 400 public JCTree visitWhileLoop(WhileLoopTree node, P p) {
duke@1 401 JCWhileLoop t = (JCWhileLoop) node;
duke@1 402 JCStatement body = copy(t.body, p);
duke@1 403 JCExpression cond = copy(t.cond, p);
duke@1 404 return M.at(t.pos).WhileLoop(cond, body);
duke@1 405 }
duke@1 406
duke@1 407 public JCTree visitWildcard(WildcardTree node, P p) {
duke@1 408 JCWildcard t = (JCWildcard) node;
duke@1 409 TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
duke@1 410 JCTree inner = copy(t.inner, p);
duke@1 411 return M.at(t.pos).Wildcard(kind, inner);
duke@1 412 }
duke@1 413
duke@1 414 public JCTree visitOther(Tree node, P p) {
duke@1 415 JCTree tree = (JCTree) node;
duke@1 416 switch (tree.getTag()) {
duke@1 417 case JCTree.LETEXPR: {
duke@1 418 LetExpr t = (LetExpr) node;
duke@1 419 List<JCVariableDecl> defs = copy(t.defs, p);
duke@1 420 JCTree expr = copy(t.expr, p);
duke@1 421 return M.at(t.pos).LetExpr(defs, expr);
duke@1 422 }
duke@1 423 default:
duke@1 424 throw new AssertionError("unknown tree tag: " + tree.getTag());
duke@1 425 }
duke@1 426 }
duke@1 427
duke@1 428 }

mercurial