001 /*
002 * Copyright 2006-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javac.tree;
027
028 import com.sun.source.tree.*;
029 import com.sun.tools.javac.tree.JCTree.*;
030 import com.sun.tools.javac.util.List;
031 import com.sun.tools.javac.util.ListBuffer;
032
033 /**
034 * Creates a copy of a tree, using a given TreeMaker.
035 * Names, literal values, etc are shared with the original.
036 *
037 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
038 * you write code that depends on this, you do so at your own risk.
039 * This code and its internal interfaces are subject to change or
040 * deletion without notice.</b>
041 */
042 public class TreeCopier<P> implements TreeVisitor<JCTree,P> {
043 private TreeMaker M;
044
045 /** Creates a new instance of TreeCopier */
046 public TreeCopier(TreeMaker M) {
047 this.M = M;
048 }
049
050 public <T extends JCTree> T copy(T tree) {
051 return copy(tree, null);
052 }
053
054 @SuppressWarnings("unchecked")
055 public <T extends JCTree> T copy(T tree, P p) {
056 if (tree == null)
057 return null;
058 return (T) (tree.accept(this, p));
059 }
060
061 public <T extends JCTree> List<T> copy(List<T> trees) {
062 return copy(trees, null);
063 }
064
065 public <T extends JCTree> List<T> copy(List<T> trees, P p) {
066 if (trees == null)
067 return null;
068 ListBuffer<T> lb = new ListBuffer<T>();
069 for (T tree: trees)
070 lb.append(copy(tree, p));
071 return lb.toList();
072 }
073
074 public JCTree visitAnnotation(AnnotationTree node, P p) {
075 JCAnnotation t = (JCAnnotation) node;
076 JCTree annotationType = copy(t.annotationType, p);
077 List<JCExpression> args = copy(t.args, p);
078 return M.at(t.pos).Annotation(annotationType, args);
079 }
080
081 public JCTree visitAssert(AssertTree node, P p) {
082 JCAssert t = (JCAssert) node;
083 JCExpression cond = copy(t.cond, p);
084 JCExpression detail = copy(t.detail, p);
085 return M.at(t.pos).Assert(cond, detail);
086 }
087
088 public JCTree visitAssignment(AssignmentTree node, P p) {
089 JCAssign t = (JCAssign) node;
090 JCExpression lhs = copy(t.lhs, p);
091 JCExpression rhs = copy(t.rhs, p);
092 return M.at(t.pos).Assign(lhs, rhs);
093 }
094
095 public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
096 JCAssignOp t = (JCAssignOp) node;
097 JCTree lhs = copy(t.lhs, p);
098 JCTree rhs = copy(t.rhs, p);
099 return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
100 }
101
102 public JCTree visitBinary(BinaryTree node, P p) {
103 JCBinary t = (JCBinary) node;
104 JCExpression lhs = copy(t.lhs, p);
105 JCExpression rhs = copy(t.rhs, p);
106 return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
107 }
108
109 public JCTree visitBlock(BlockTree node, P p) {
110 JCBlock t = (JCBlock) node;
111 List<JCStatement> stats = copy(t.stats, p);
112 return M.at(t.pos).Block(t.flags, stats);
113 }
114
115 /* emw4: staging stuff */
116 public JCTree visitBracketExpr(BracketExprTree node, P p) {
117 throw new UnsupportedOperationException ("visitBracketExpr in TreeCopier");
118 }
119
120 public JCTree visitBracketStat(BracketStatTree node, P p) {
121 throw new UnsupportedOperationException ("visitBracketStat in TreeCopier");
122 }
123
124 public JCTree visitEscapeExpr(EscapeExprTree node, P p) {
125 throw new UnsupportedOperationException ("visitEscapeExpr in TreeCopier");
126 }
127
128 public JCTree visitEscapeStat(EscapeStatTree node, P p) {
129 throw new UnsupportedOperationException ("visitEscapeStat in TreeCopier");
130 }
131
132
133 public JCTree visitBreak(BreakTree node, P p) {
134 JCBreak t = (JCBreak) node;
135 return M.at(t.pos).Break(t.label);
136 }
137
138 public JCTree visitCase(CaseTree node, P p) {
139 JCCase t = (JCCase) node;
140 JCExpression pat = copy(t.pat, p);
141 List<JCStatement> stats = copy(t.stats, p);
142 return M.at(t.pos).Case(pat, stats);
143 }
144
145 public JCTree visitCatch(CatchTree node, P p) {
146 JCCatch t = (JCCatch) node;
147 JCVariableDecl param = copy(t.param, p);
148 JCBlock body = copy(t.body, p);
149 return M.at(t.pos).Catch(param, body);
150 }
151
152 public JCTree visitClass(ClassTree node, P p) {
153 JCClassDecl t = (JCClassDecl) node;
154 JCModifiers mods = copy(t.mods, p);
155 List<JCTypeParameter> typarams = copy(t.typarams, p);
156 JCTree extending = copy(t.extending, p);
157 List<JCExpression> implementing = copy(t.implementing, p);
158 List<JCTree> defs = copy(t.defs, p);
159 return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
160 }
161
162 public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
163 JCConditional t = (JCConditional) node;
164 JCExpression cond = copy(t.cond, p);
165 JCExpression truepart = copy(t.truepart, p);
166 JCExpression falsepart = copy(t.falsepart, p);
167 return M.at(t.pos).Conditional(cond, truepart, falsepart);
168 }
169
170 public JCTree visitContinue(ContinueTree node, P p) {
171 JCContinue t = (JCContinue) node;
172 return M.at(t.pos).Continue(t.label);
173 }
174
175 public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
176 JCDoWhileLoop t = (JCDoWhileLoop) node;
177 JCStatement body = copy(t.body, p);
178 JCExpression cond = copy(t.cond, p);
179 return M.at(t.pos).DoLoop(body, cond);
180 }
181
182 public JCTree visitErroneous(ErroneousTree node, P p) {
183 JCErroneous t = (JCErroneous) node;
184 List<? extends JCTree> errs = copy(t.errs, p);
185 return M.at(t.pos).Erroneous(errs);
186 }
187
188 public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
189 JCExpressionStatement t = (JCExpressionStatement) node;
190 JCExpression expr = copy(t.expr, p);
191 return M.at(t.pos).Exec(expr);
192 }
193
194 public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
195 JCEnhancedForLoop t = (JCEnhancedForLoop) node;
196 JCVariableDecl var = copy(t.var, p);
197 JCExpression expr = copy(t.expr, p);
198 JCStatement body = copy(t.body, p);
199 return M.at(t.pos).ForeachLoop(var, expr, body);
200 }
201
202 public JCTree visitForLoop(ForLoopTree node, P p) {
203 JCForLoop t = (JCForLoop) node;
204 List<JCStatement> init = copy(t.init, p);
205 JCExpression cond = copy(t.cond, p);
206 List<JCExpressionStatement> step = copy(t.step, p);
207 JCStatement body = copy(t.body, p);
208 return M.at(t.pos).ForLoop(init, cond, step, body);
209 }
210
211 public JCTree visitIdentifier(IdentifierTree node, P p) {
212 JCIdent t = (JCIdent) node;
213 return M.at(t.pos).Ident(t.name);
214 }
215
216 public JCTree visitIf(IfTree node, P p) {
217 JCIf t = (JCIf) node;
218 JCExpression cond = copy(t.cond, p);
219 JCStatement thenpart = copy(t.thenpart, p);
220 JCStatement elsepart = copy(t.elsepart, p);
221 return M.at(t.pos).If(cond, thenpart, elsepart);
222 }
223
224 public JCTree visitImport(ImportTree node, P p) {
225 JCImport t = (JCImport) node;
226 JCTree qualid = copy(t.qualid, p);
227 return M.at(t.pos).Import(qualid, t.staticImport);
228 }
229
230 public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
231 JCArrayAccess t = (JCArrayAccess) node;
232 JCExpression indexed = copy(t.indexed, p);
233 JCExpression index = copy(t.index, p);
234 return M.at(t.pos).Indexed(indexed, index);
235 }
236
237 public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
238 JCLabeledStatement t = (JCLabeledStatement) node;
239 JCStatement body = copy(t.body, p);
240 return M.at(t.pos).Labelled(t.label, t.body);
241 }
242
243 public JCTree visitLiteral(LiteralTree node, P p) {
244 JCLiteral t = (JCLiteral) node;
245 return M.at(t.pos).Literal(t.typetag, t.value);
246 }
247
248 public JCTree visitMethod(MethodTree node, P p) {
249 JCMethodDecl t = (JCMethodDecl) node;
250 JCModifiers mods = copy(t.mods, p);
251 JCExpression restype = copy(t.restype, p);
252 List<JCTypeParameter> typarams = copy(t.typarams, p);
253 List<JCVariableDecl> params = copy(t.params, p);
254 List<JCExpression> thrown = copy(t.thrown, p);
255 JCBlock body = copy(t.body, p);
256 JCExpression defaultValue = copy(t.defaultValue, p);
257 return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, params, thrown, body, defaultValue);
258 }
259
260 public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
261 JCMethodInvocation t = (JCMethodInvocation) node;
262 List<JCExpression> typeargs = copy(t.typeargs, p);
263 JCExpression meth = copy(t.meth, p);
264 List<JCExpression> args = copy(t.args, p);
265 return M.at(t.pos).Apply(typeargs, meth, args);
266 }
267
268 public JCTree visitModifiers(ModifiersTree node, P p) {
269 JCModifiers t = (JCModifiers) node;
270 List<JCAnnotation> annotations = copy(t.annotations, p);
271 return M.at(t.pos).Modifiers(t.flags, annotations);
272 }
273
274 public JCTree visitNewArray(NewArrayTree node, P p) {
275 JCNewArray t = (JCNewArray) node;
276 JCExpression elemtype = copy(t.elemtype, p);
277 List<JCExpression> dims = copy(t.dims, p);
278 List<JCExpression> elems = copy(t.elems, p);
279 return M.at(t.pos).NewArray(elemtype, dims, elems);
280 }
281
282 public JCTree visitNewClass(NewClassTree node, P p) {
283 JCNewClass t = (JCNewClass) node;
284 JCExpression encl = copy(t.encl, p);
285 List<JCExpression> typeargs = copy(t.typeargs, p);
286 JCExpression clazz = copy(t.clazz, p);
287 List<JCExpression> args = copy(t.args, p);
288 JCClassDecl def = copy(t.def, p);
289 return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
290 }
291
292 public JCTree visitParenthesized(ParenthesizedTree node, P p) {
293 JCParens t = (JCParens) node;
294 JCExpression expr = copy(t.expr, p);
295 return M.at(t.pos).Parens(expr);
296 }
297
298 public JCTree visitReturn(ReturnTree node, P p) {
299 JCReturn t = (JCReturn) node;
300 JCExpression expr = copy(t.expr, p);
301 return M.at(t.pos).Return(expr);
302 }
303
304 public JCTree visitMemberSelect(MemberSelectTree node, P p) {
305 JCFieldAccess t = (JCFieldAccess) node;
306 JCExpression selected = copy(t.selected, p);
307 return M.at(t.pos).Select(selected, t.name);
308 }
309
310 public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
311 JCSkip t = (JCSkip) node;
312 return M.at(t.pos).Skip();
313 }
314
315 public JCTree visitSwitch(SwitchTree node, P p) {
316 JCSwitch t = (JCSwitch) node;
317 JCExpression selector = copy(t.selector, p);
318 List<JCCase> cases = copy(t.cases, p);
319 return M.at(t.pos).Switch(selector, cases);
320 }
321
322 public JCTree visitSynchronized(SynchronizedTree node, P p) {
323 JCSynchronized t = (JCSynchronized) node;
324 JCExpression lock = copy(t.lock, p);
325 JCBlock body = copy(t.body, p);
326 return M.at(t.pos).Synchronized(lock, body);
327 }
328
329 public JCTree visitThrow(ThrowTree node, P p) {
330 JCThrow t = (JCThrow) node;
331 JCTree expr = copy(t.expr, p);
332 return M.at(t.pos).Throw(expr);
333 }
334
335 public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
336 JCCompilationUnit t = (JCCompilationUnit) node;
337 List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p);
338 JCExpression pid = copy(t.pid, p);
339 List<JCTree> defs = copy(t.defs, p);
340 return M.at(t.pos).TopLevel(packageAnnotations, pid, defs);
341 }
342
343 public JCTree visitTry(TryTree node, P p) {
344 JCTry t = (JCTry) node;
345 JCBlock body = copy(t.body, p);
346 List<JCCatch> catchers = copy(t.catchers, p);
347 JCBlock finalizer = copy(t.finalizer, p);
348 return M.at(t.pos).Try(body, catchers, finalizer);
349 }
350
351 public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
352 JCTypeApply t = (JCTypeApply) node;
353 JCExpression clazz = copy(t.clazz, p);
354 List<JCExpression> arguments = copy(t.arguments, p);
355 return M.at(t.pos).TypeApply(clazz, arguments);
356 }
357
358 public JCTree visitArrayType(ArrayTypeTree node, P p) {
359 JCArrayTypeTree t = (JCArrayTypeTree) node;
360 JCExpression elemtype = copy(t.elemtype, p);
361 return M.at(t.pos).TypeArray(elemtype);
362 }
363
364 public JCTree visitTypeCast(TypeCastTree node, P p) {
365 JCTypeCast t = (JCTypeCast) node;
366 JCTree clazz = copy(t.clazz, p);
367 JCExpression expr = copy(t.expr, p);
368 return M.at(t.pos).TypeCast(clazz, expr);
369 }
370
371 public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
372 JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
373 return M.at(t.pos).TypeIdent(t.typetag);
374 }
375
376 public JCTree visitTypeParameter(TypeParameterTree node, P p) {
377 JCTypeParameter t = (JCTypeParameter) node;
378 List<JCExpression> bounds = copy(t.bounds, p);
379 return M.at(t.pos).TypeParameter(t.name, t.bounds);
380 }
381
382 public JCTree visitInstanceOf(InstanceOfTree node, P p) {
383 JCInstanceOf t = (JCInstanceOf) node;
384 JCExpression expr = copy(t.expr, p);
385 JCTree clazz = copy(t.clazz, p);
386 return M.at(t.pos).TypeTest(expr, clazz);
387 }
388
389 public JCTree visitUnary(UnaryTree node, P p) {
390 JCUnary t = (JCUnary) node;
391 JCExpression arg = copy(t.arg, p);
392 return M.at(t.pos).Unary(t.getTag(), arg);
393 }
394
395 public JCTree visitVariable(VariableTree node, P p) {
396 JCVariableDecl t = (JCVariableDecl) node;
397 JCModifiers mods = copy(t.mods, p);
398 JCExpression vartype = copy(t.vartype, p);
399 JCExpression init = copy(t.init, p);
400 return M.at(t.pos).VarDef(mods, t.name, vartype, init);
401 }
402
403 public JCTree visitWhileLoop(WhileLoopTree node, P p) {
404 JCWhileLoop t = (JCWhileLoop) node;
405 JCStatement body = copy(t.body, p);
406 JCExpression cond = copy(t.cond, p);
407 return M.at(t.pos).WhileLoop(cond, body);
408 }
409
410 public JCTree visitWildcard(WildcardTree node, P p) {
411 JCWildcard t = (JCWildcard) node;
412 TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
413 JCTree inner = copy(t.inner, p);
414 return M.at(t.pos).Wildcard(kind, inner);
415 }
416
417 public JCTree visitOther(Tree node, P p) {
418 JCTree tree = (JCTree) node;
419 switch (tree.getTag()) {
420 case JCTree.LETEXPR: {
421 LetExpr t = (LetExpr) node;
422 List<JCVariableDecl> defs = copy(t.defs, p);
423 JCTree expr = copy(t.expr, p);
424 return M.at(t.pos).LetExpr(defs, expr);
425 }
426 default:
427 throw new AssertionError("unknown tree tag: " + tree.getTag());
428 }
429 }
430
431 }