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