001: /*
002: * Copyright 1999-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.tools.javac.util.*;
029: import com.sun.tools.javac.tree.JCTree.*;
030:
031: /** A subclass of Tree.Visitor, this class defines
032: * a general tree translator pattern. Translation proceeds recursively in
033: * left-to-right order down a tree, constructing translated nodes by
034: * overwriting existing ones. There is one visitor method in this class
035: * for every possible kind of tree node. To obtain a specific
036: * translator, it suffices to override those visitor methods which
037: * do some interesting work. The translator class itself takes care of all
038: * navigational aspects.
039: *
040: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
041: * you write code that depends on this, you do so at your own risk.
042: * This code and its internal interfaces are subject to change or
043: * deletion without notice.</b>
044: */
045: @Version("@(#)TreeTranslator.java 1.44 07/05/05")
046: public class TreeTranslator extends JCTree.Visitor {
047:
048: /** Visitor result field: a tree
049: */
050: protected JCTree result;
051:
052: /** Visitor method: Translate a single node.
053: */
054: @SuppressWarnings("unchecked")
055: public <T extends JCTree> T translate(T tree) {
056: if (tree == null) {
057: return null;
058: } else {
059: tree.accept(this );
060: JCTree result = this .result;
061: this .result = null;
062: return (T) result; // XXX cast
063: }
064: }
065:
066: /** Visitor method: translate a list of nodes.
067: */
068: public <T extends JCTree> List<T> translate(List<T> trees) {
069: if (trees == null)
070: return null;
071: for (List<T> l = trees; l.nonEmpty(); l = l.tail)
072: l.head = translate(l.head);
073: return trees;
074: }
075:
076: /** Visitor method: translate a list of variable definitions.
077: */
078: public List<JCVariableDecl> translateVarDefs(
079: List<JCVariableDecl> trees) {
080: for (List<JCVariableDecl> l = trees; l.nonEmpty(); l = l.tail)
081: l.head = translate(l.head);
082: return trees;
083: }
084:
085: /** Visitor method: translate a list of type parameters.
086: */
087: public List<JCTypeParameter> translateTypeParams(
088: List<JCTypeParameter> trees) {
089: for (List<JCTypeParameter> l = trees; l.nonEmpty(); l = l.tail)
090: l.head = translate(l.head);
091: return trees;
092: }
093:
094: /** Visitor method: translate a list of case parts of switch statements.
095: */
096: public List<JCCase> translateCases(List<JCCase> trees) {
097: for (List<JCCase> l = trees; l.nonEmpty(); l = l.tail)
098: l.head = translate(l.head);
099: return trees;
100: }
101:
102: /** Visitor method: translate a list of catch clauses in try statements.
103: */
104: public List<JCCatch> translateCatchers(List<JCCatch> trees) {
105: for (List<JCCatch> l = trees; l.nonEmpty(); l = l.tail)
106: l.head = translate(l.head);
107: return trees;
108: }
109:
110: /** Visitor method: translate a list of catch clauses in try statements.
111: */
112: public List<JCAnnotation> translateAnnotations(
113: List<JCAnnotation> trees) {
114: for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail)
115: l.head = translate(l.head);
116: return trees;
117: }
118:
119: /* ***************************************************************************
120: * Visitor methods
121: ****************************************************************************/
122:
123: public void visitTopLevel(JCCompilationUnit tree) {
124: tree.pid = translate(tree.pid);
125: tree.defs = translate(tree.defs);
126: result = tree;
127: }
128:
129: public void visitImport(JCImport tree) {
130: tree.qualid = translate(tree.qualid);
131: result = tree;
132: }
133:
134: public void visitClassDef(JCClassDecl tree) {
135: tree.mods = translate(tree.mods);
136: tree.typarams = translateTypeParams(tree.typarams);
137: tree.extending = translate(tree.extending);
138: tree.implementing = translate(tree.implementing);
139: tree.defs = translate(tree.defs);
140: result = tree;
141: }
142:
143: public void visitMethodDef(JCMethodDecl tree) {
144: tree.mods = translate(tree.mods);
145: tree.restype = translate(tree.restype);
146: tree.typarams = translateTypeParams(tree.typarams);
147: tree.params = translateVarDefs(tree.params);
148: tree.thrown = translate(tree.thrown);
149: tree.body = translate(tree.body);
150: result = tree;
151: }
152:
153: public void visitVarDef(JCVariableDecl tree) {
154: tree.mods = translate(tree.mods);
155: tree.vartype = translate(tree.vartype);
156: tree.init = translate(tree.init);
157: result = tree;
158: }
159:
160: public void visitSkip(JCSkip tree) {
161: result = tree;
162: }
163:
164: public void visitBlock(JCBlock tree) {
165: tree.stats = translate(tree.stats);
166: result = tree;
167: }
168:
169: public void visitDoLoop(JCDoWhileLoop tree) {
170: tree.body = translate(tree.body);
171: tree.cond = translate(tree.cond);
172: result = tree;
173: }
174:
175: public void visitWhileLoop(JCWhileLoop tree) {
176: tree.cond = translate(tree.cond);
177: tree.body = translate(tree.body);
178: result = tree;
179: }
180:
181: public void visitForLoop(JCForLoop tree) {
182: tree.init = translate(tree.init);
183: tree.cond = translate(tree.cond);
184: tree.step = translate(tree.step);
185: tree.body = translate(tree.body);
186: result = tree;
187: }
188:
189: public void visitForeachLoop(JCEnhancedForLoop tree) {
190: tree.var = translate(tree.var);
191: tree.expr = translate(tree.expr);
192: tree.body = translate(tree.body);
193: result = tree;
194: }
195:
196: public void visitLabelled(JCLabeledStatement tree) {
197: tree.body = translate(tree.body);
198: result = tree;
199: }
200:
201: public void visitSwitch(JCSwitch tree) {
202: tree.selector = translate(tree.selector);
203: tree.cases = translateCases(tree.cases);
204: result = tree;
205: }
206:
207: public void visitCase(JCCase tree) {
208: tree.pat = translate(tree.pat);
209: tree.stats = translate(tree.stats);
210: result = tree;
211: }
212:
213: public void visitSynchronized(JCSynchronized tree) {
214: tree.lock = translate(tree.lock);
215: tree.body = translate(tree.body);
216: result = tree;
217: }
218:
219: public void visitTry(JCTry tree) {
220: tree.body = translate(tree.body);
221: tree.catchers = translateCatchers(tree.catchers);
222: tree.finalizer = translate(tree.finalizer);
223: result = tree;
224: }
225:
226: public void visitCatch(JCCatch tree) {
227: tree.param = translate(tree.param);
228: tree.body = translate(tree.body);
229: result = tree;
230: }
231:
232: public void visitConditional(JCConditional tree) {
233: tree.cond = translate(tree.cond);
234: tree.truepart = translate(tree.truepart);
235: tree.falsepart = translate(tree.falsepart);
236: result = tree;
237: }
238:
239: public void visitIf(JCIf tree) {
240: tree.cond = translate(tree.cond);
241: tree.thenpart = translate(tree.thenpart);
242: tree.elsepart = translate(tree.elsepart);
243: result = tree;
244: }
245:
246: public void visitExec(JCExpressionStatement tree) {
247: tree.expr = translate(tree.expr);
248: result = tree;
249: }
250:
251: public void visitBreak(JCBreak tree) {
252: result = tree;
253: }
254:
255: public void visitContinue(JCContinue tree) {
256: result = tree;
257: }
258:
259: public void visitReturn(JCReturn tree) {
260: tree.expr = translate(tree.expr);
261: result = tree;
262: }
263:
264: public void visitThrow(JCThrow tree) {
265: tree.expr = translate(tree.expr);
266: result = tree;
267: }
268:
269: public void visitAssert(JCAssert tree) {
270: tree.cond = translate(tree.cond);
271: tree.detail = translate(tree.detail);
272: result = tree;
273: }
274:
275: public void visitApply(JCMethodInvocation tree) {
276: tree.meth = translate(tree.meth);
277: tree.args = translate(tree.args);
278: result = tree;
279: }
280:
281: public void visitNewClass(JCNewClass tree) {
282: tree.encl = translate(tree.encl);
283: tree.clazz = translate(tree.clazz);
284: tree.args = translate(tree.args);
285: tree.def = translate(tree.def);
286: result = tree;
287: }
288:
289: public void visitNewArray(JCNewArray tree) {
290: tree.elemtype = translate(tree.elemtype);
291: tree.dims = translate(tree.dims);
292: tree.elems = translate(tree.elems);
293: result = tree;
294: }
295:
296: public void visitParens(JCParens tree) {
297: tree.expr = translate(tree.expr);
298: result = tree;
299: }
300:
301: public void visitAssign(JCAssign tree) {
302: tree.lhs = translate(tree.lhs);
303: tree.rhs = translate(tree.rhs);
304: result = tree;
305: }
306:
307: public void visitAssignop(JCAssignOp tree) {
308: tree.lhs = translate(tree.lhs);
309: tree.rhs = translate(tree.rhs);
310: result = tree;
311: }
312:
313: public void visitUnary(JCUnary tree) {
314: tree.arg = translate(tree.arg);
315: result = tree;
316: }
317:
318: public void visitBinary(JCBinary tree) {
319: tree.lhs = translate(tree.lhs);
320: tree.rhs = translate(tree.rhs);
321: result = tree;
322: }
323:
324: public void visitTypeCast(JCTypeCast tree) {
325: tree.clazz = translate(tree.clazz);
326: tree.expr = translate(tree.expr);
327: result = tree;
328: }
329:
330: public void visitTypeTest(JCInstanceOf tree) {
331: tree.expr = translate(tree.expr);
332: tree.clazz = translate(tree.clazz);
333: result = tree;
334: }
335:
336: public void visitIndexed(JCArrayAccess tree) {
337: tree.indexed = translate(tree.indexed);
338: tree.index = translate(tree.index);
339: result = tree;
340: }
341:
342: public void visitSelect(JCFieldAccess tree) {
343: tree.selected = translate(tree.selected);
344: result = tree;
345: }
346:
347: public void visitIdent(JCIdent tree) {
348: result = tree;
349: }
350:
351: public void visitLiteral(JCLiteral tree) {
352: result = tree;
353: }
354:
355: public void visitTypeIdent(JCPrimitiveTypeTree tree) {
356: result = tree;
357: }
358:
359: public void visitTypeArray(JCArrayTypeTree tree) {
360: tree.elemtype = translate(tree.elemtype);
361: result = tree;
362: }
363:
364: public void visitTypeApply(JCTypeApply tree) {
365: tree.clazz = translate(tree.clazz);
366: tree.arguments = translate(tree.arguments);
367: result = tree;
368: }
369:
370: public void visitTypeParameter(JCTypeParameter tree) {
371: tree.bounds = translate(tree.bounds);
372: result = tree;
373: }
374:
375: @Override
376: public void visitWildcard(JCWildcard tree) {
377: tree.kind = translate(tree.kind);
378: tree.inner = translate(tree.inner);
379: result = tree;
380: }
381:
382: @Override
383: public void visitTypeBoundKind(TypeBoundKind tree) {
384: result = tree;
385: }
386:
387: public void visitErroneous(JCErroneous tree) {
388: result = tree;
389: }
390:
391: public void visitLetExpr(LetExpr tree) {
392: tree.defs = translateVarDefs(tree.defs);
393: tree.expr = translate(tree.expr);
394: result = tree;
395: }
396:
397: public void visitModifiers(JCModifiers tree) {
398: tree.annotations = translateAnnotations(tree.annotations);
399: result = tree;
400: }
401:
402: public void visitAnnotation(JCAnnotation tree) {
403: tree.annotationType = translate(tree.annotationType);
404: tree.args = translate(tree.args);
405: result = tree;
406: }
407:
408: public void visitTree(JCTree tree) {
409: throw new AssertionError(tree);
410: }
411: }
|