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