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