1 /*
  2  * Copyright (c) 2006, 2019, 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.DefinedBy;
 31 import com.sun.tools.javac.util.DefinedBy.Api;
 32 import com.sun.tools.javac.util.List;
 33 import com.sun.tools.javac.util.ListBuffer;
 34 
 35 /**
 36  * Creates a copy of a tree, using a given TreeMaker.
 37  * Names, literal values, etc are shared with the original.
 38  *
 39  *  <p><b>This is NOT part of any supported API.
 40  *  If you write code that depends on this, you do so at your own risk.
 41  *  This code and its internal interfaces are subject to change or
 42  *  deletion without notice.</b>
 43  */
 44 public class TreeCopier<P> implements TreeVisitor<JCTree,P> {
 45     private TreeMaker M;
 46 
 47     /** Creates a new instance of TreeCopier */
 48     public TreeCopier(TreeMaker M) {
 49         this.M = M;
 50     }
 51 
 52     public <T extends JCTree> T copy(T tree) {
 53         return copy(tree, null);
 54     }
 55 
 56     @SuppressWarnings("unchecked")
 57     public <T extends JCTree> T copy(T tree, P p) {
 58         if (tree == null)
 59             return null;
 60         return (T) (tree.accept(this, p));
 61     }
 62 
 63     public <T extends JCTree> List<T> copy(List<T> trees) {
 64         return copy(trees, null);
 65     }
 66 
 67     public <T extends JCTree> List<T> copy(List<T> trees, P p) {
 68         if (trees == null)
 69             return null;
 70         ListBuffer<T> lb = new ListBuffer<>();
 71         for (T tree: trees)
 72             lb.append(copy(tree, p));
 73         return lb.toList();
 74     }
 75 
 76     @DefinedBy(Api.COMPILER_TREE)
 77     public JCTree visitAnnotatedType(AnnotatedTypeTree node, P p) {
 78         JCAnnotatedType t = (JCAnnotatedType) node;
 79         List<JCAnnotation> annotations = copy(t.annotations, p);
 80         JCExpression underlyingType = copy(t.underlyingType, p);
 81         return M.at(t.pos).AnnotatedType(annotations, underlyingType);
 82     }
 83 
 84     @DefinedBy(Api.COMPILER_TREE)
 85     public JCTree visitAnnotation(AnnotationTree node, P p) {
 86         JCAnnotation t = (JCAnnotation) node;
 87         JCTree annotationType = copy(t.annotationType, p);
 88         List<JCExpression> args = copy(t.args, p);
 89         if (t.getKind() == Tree.Kind.TYPE_ANNOTATION) {
 90             JCAnnotation newTA = M.at(t.pos).TypeAnnotation(annotationType, args);
 91             newTA.attribute = t.attribute;
 92             return newTA;
 93         } else {
 94             JCAnnotation newT = M.at(t.pos).Annotation(annotationType, args);
 95             newT.attribute = t.attribute;
 96             return newT;
 97         }
 98     }
 99 
100     @DefinedBy(Api.COMPILER_TREE)
101     public JCTree visitAssert(AssertTree node, P p) {
102         JCAssert t = (JCAssert) node;
103         JCExpression cond = copy(t.cond, p);
104         JCExpression detail = copy(t.detail, p);
105         return M.at(t.pos).Assert(cond, detail);
106     }
107 
108     @DefinedBy(Api.COMPILER_TREE)
109     public JCTree visitAssignment(AssignmentTree node, P p) {
110         JCAssign t = (JCAssign) node;
111         JCExpression lhs = copy(t.lhs, p);
112         JCExpression rhs = copy(t.rhs, p);
113         return M.at(t.pos).Assign(lhs, rhs);
114     }
115 
116     @DefinedBy(Api.COMPILER_TREE)
117     public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
118         JCAssignOp t = (JCAssignOp) node;
119         JCTree lhs = copy(t.lhs, p);
120         JCTree rhs = copy(t.rhs, p);
121         return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
122     }
123 
124     @DefinedBy(Api.COMPILER_TREE)
125     public JCTree visitBinary(BinaryTree node, P p) {
126         JCBinary t = (JCBinary) node;
127         JCExpression lhs = copy(t.lhs, p);
128         JCExpression rhs = copy(t.rhs, p);
129         return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
130     }
131 
132     @DefinedBy(Api.COMPILER_TREE)
133     public JCTree visitBlock(BlockTree node, P p) {
134         JCBlock t = (JCBlock) node;
135         List<JCStatement> stats = copy(t.stats, p);
136         return M.at(t.pos).Block(t.flags, stats);
137     }
138 
139     @DefinedBy(Api.COMPILER_TREE)
140     public JCTree visitBreak(BreakTree node, P p) {
141         JCBreak t = (JCBreak) node;
142         return M.at(t.pos).Break(t.label);
143     }
144 
145     @DefinedBy(Api.COMPILER_TREE)
146     public JCTree visitYield(YieldTree node, P p) {
147         JCYield t = (JCYield) node;
148         JCExpression value = copy(t.value, p);
149         return M.at(t.pos).Yield(value);
150     }
151 
152     @DefinedBy(Api.COMPILER_TREE)
153     public JCTree visitCase(CaseTree node, P p) {
154         JCCase t = (JCCase) node;
155         List<JCCaseLabel> labels = copy(t.labels, p);
156         List<JCStatement> stats = copy(t.stats, p);
157         JCTree body;
158         if (node.getCaseKind() == CaseTree.CaseKind.RULE) {
159             body = t.body instanceof JCExpression && t.stats.head.hasTag(Tag.YIELD)
160                     ? ((JCYield) t.stats.head).value : t.stats.head;
161         } else {
162             body = null;
163         }
164         return M.at(t.pos).Case(t.caseKind, labels, stats, body);
165     }
166 
167     @DefinedBy(Api.COMPILER_TREE)
168     public JCTree visitCatch(CatchTree node, P p) {
169         JCCatch t = (JCCatch) node;
170         JCVariableDecl param = copy(t.param, p);
171         JCBlock body = copy(t.body, p);
172         return M.at(t.pos).Catch(param, body);
173     }
174 
175     @DefinedBy(Api.COMPILER_TREE)
176     public JCTree visitClass(ClassTree node, P p) {
177         JCClassDecl t = (JCClassDecl) node;
178         JCModifiers mods = copy(t.mods, p);
179         List<JCTypeParameter> typarams = copy(t.typarams, p);
180         JCExpression extending = copy(t.extending, p);
181         List<JCExpression> implementing = copy(t.implementing, p);
182         List<JCTree> defs = copy(t.defs, p);
183         return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
184     }
185 
186     @DefinedBy(Api.COMPILER_TREE)
187     public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
188         JCConditional t = (JCConditional) node;
189         JCExpression cond = copy(t.cond, p);
190         JCExpression truepart = copy(t.truepart, p);
191         JCExpression falsepart = copy(t.falsepart, p);
192         return M.at(t.pos).Conditional(cond, truepart, falsepart);
193     }
194 
195     @DefinedBy(Api.COMPILER_TREE)
196     public JCTree visitContinue(ContinueTree node, P p) {
197         JCContinue t = (JCContinue) node;
198         return M.at(t.pos).Continue(t.label);
199     }
200 
201     @DefinedBy(Api.COMPILER_TREE)
202     public JCTree visitDefaultValue(DefaultValueTree node, P p) {
203         JCDefaultValue t = (JCDefaultValue) node;
204         JCExpression clazz = copy(t.clazz, p);
205         return M.at(t.pos).DefaultValue(clazz);
206     }
207 
208     @DefinedBy(Api.COMPILER_TREE)
209     public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
210         JCDoWhileLoop t = (JCDoWhileLoop) node;
211         JCStatement body = copy(t.body, p);
212         JCExpression cond = copy(t.cond, p);
213         return M.at(t.pos).DoLoop(body, cond);
214     }
215 
216     @DefinedBy(Api.COMPILER_TREE)
217     public JCTree visitErroneous(ErroneousTree node, P p) {
218         JCErroneous t = (JCErroneous) node;
219         List<? extends JCTree> errs = copy(t.errs, p);
220         return M.at(t.pos).Erroneous(errs);
221     }
222 
223     @DefinedBy(Api.COMPILER_TREE)
224     public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
225         JCExpressionStatement t = (JCExpressionStatement) node;
226         JCExpression expr = copy(t.expr, p);
227         return M.at(t.pos).Exec(expr);
228     }
229 
230     @DefinedBy(Api.COMPILER_TREE)
231     public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
232         JCEnhancedForLoop t = (JCEnhancedForLoop) node;
233         JCVariableDecl var = copy(t.var, p);
234         JCExpression expr = copy(t.expr, p);
235         JCStatement body = copy(t.body, p);
236         return M.at(t.pos).ForeachLoop(var, expr, body);
237     }
238 
239     @DefinedBy(Api.COMPILER_TREE)
240     public JCTree visitForLoop(ForLoopTree node, P p) {
241         JCForLoop t = (JCForLoop) node;
242         List<JCStatement> init = copy(t.init, p);
243         JCExpression cond = copy(t.cond, p);
244         List<JCExpressionStatement> step = copy(t.step, p);
245         JCStatement body = copy(t.body, p);
246         return M.at(t.pos).ForLoop(init, cond, step, body);
247     }
248 
249     @DefinedBy(Api.COMPILER_TREE)
250     public JCTree visitIdentifier(IdentifierTree node, P p) {
251         JCIdent t = (JCIdent) node;
252         JCIdent ident = M.at(t.pos).Ident(t.name);
253         return ident;
254     }
255 
256     @DefinedBy(Api.COMPILER_TREE)
257     public JCTree visitIf(IfTree node, P p) {
258         JCIf t = (JCIf) node;
259         JCExpression cond = copy(t.cond, p);
260         JCStatement thenpart = copy(t.thenpart, p);
261         JCStatement elsepart = copy(t.elsepart, p);
262         return M.at(t.pos).If(cond, thenpart, elsepart);
263     }
264 
265     @DefinedBy(Api.COMPILER_TREE)
266     public JCTree visitImport(ImportTree node, P p) {
267         JCImport t = (JCImport) node;
268         JCTree qualid = copy(t.qualid, p);
269         return M.at(t.pos).Import(qualid, t.staticImport);
270     }
271 
272     @DefinedBy(Api.COMPILER_TREE)
273     public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
274         JCArrayAccess t = (JCArrayAccess) node;
275         JCExpression indexed = copy(t.indexed, p);
276         JCExpression index = copy(t.index, p);
277         return M.at(t.pos).Indexed(indexed, index);
278     }
279 
280     @DefinedBy(Api.COMPILER_TREE)
281     public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
282         JCLabeledStatement t = (JCLabeledStatement) node;
283         JCStatement body = copy(t.body, p);
284         return M.at(t.pos).Labelled(t.label, body);
285     }
286 
287     @DefinedBy(Api.COMPILER_TREE)
288     public JCTree visitLiteral(LiteralTree node, P p) {
289         JCLiteral t = (JCLiteral) node;
290         return M.at(t.pos).Literal(t.typetag, t.value);
291     }
292 
293     @DefinedBy(Api.COMPILER_TREE)
294     public JCTree visitMethod(MethodTree node, P p) {
295         JCMethodDecl t  = (JCMethodDecl) node;
296         JCModifiers mods = copy(t.mods, p);
297         JCExpression restype = copy(t.restype, p);
298         List<JCTypeParameter> typarams = copy(t.typarams, p);
299         List<JCVariableDecl> params = copy(t.params, p);
300         JCVariableDecl recvparam = copy(t.recvparam, p);
301         List<JCExpression> thrown = copy(t.thrown, p);
302         JCBlock body = copy(t.body, p);
303         JCExpression defaultValue = copy(t.defaultValue, p);
304         return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, recvparam, params, thrown, body, defaultValue);
305     }
306 
307     @DefinedBy(Api.COMPILER_TREE)
308     public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
309         JCMethodInvocation t = (JCMethodInvocation) node;
310         List<JCExpression> typeargs = copy(t.typeargs, p);
311         JCExpression meth = copy(t.meth, p);
312         List<JCExpression> args = copy(t.args, p);
313         return M.at(t.pos).Apply(typeargs, meth, args);
314     }
315 
316     @DefinedBy(Api.COMPILER_TREE)
317     public JCTree visitModifiers(ModifiersTree node, P p) {
318         JCModifiers t = (JCModifiers) node;
319         List<JCAnnotation> annotations = copy(t.annotations, p);
320         return M.at(t.pos).Modifiers(t.flags, annotations);
321     }
322 
323     @DefinedBy(Api.COMPILER_TREE)
324     public JCTree visitNewArray(NewArrayTree node, P p) {
325         JCNewArray t = (JCNewArray) node;
326         JCExpression elemtype = copy(t.elemtype, p);
327         List<JCExpression> dims = copy(t.dims, p);
328         List<JCExpression> elems = copy(t.elems, p);
329         return M.at(t.pos).NewArray(elemtype, dims, elems);
330     }
331 
332     @DefinedBy(Api.COMPILER_TREE)
333     public JCTree visitNewClass(NewClassTree node, P p) {
334         JCNewClass t = (JCNewClass) node;
335         JCExpression encl = copy(t.encl, p);
336         List<JCExpression> typeargs = copy(t.typeargs, p);
337         JCExpression clazz = copy(t.clazz, p);
338         List<JCExpression> args = copy(t.args, p);
339         JCClassDecl def = copy(t.def, p);
340         return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
341     }
342 
343     @DefinedBy(Api.COMPILER_TREE)
344     public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
345         JCLambda t = (JCLambda) node;
346         List<JCVariableDecl> params = copy(t.params, p);
347         JCTree body = copy(t.body, p);
348         return M.at(t.pos).Lambda(params, body);
349     }
350 
351     @DefinedBy(Api.COMPILER_TREE)
352     public JCTree visitParenthesized(ParenthesizedTree node, P p) {
353         JCParens t = (JCParens) node;
354         JCExpression expr = copy(t.expr, p);
355         return M.at(t.pos).Parens(expr);
356     }
357 
358     @DefinedBy(Api.COMPILER_TREE)
359     public JCTree visitReturn(ReturnTree node, P p) {
360         JCReturn t = (JCReturn) node;
361         JCExpression expr = copy(t.expr, p);
362         return M.at(t.pos).Return(expr);
363     }
364 
365     @DefinedBy(Api.COMPILER_TREE)
366     public JCTree visitMemberSelect(MemberSelectTree node, P p) {
367         JCFieldAccess t = (JCFieldAccess) node;
368         JCExpression selected = copy(t.selected, p);
369         JCFieldAccess select = M.at(t.pos).Select(selected, t.name);
370         return select;
371     }
372 
373     @DefinedBy(Api.COMPILER_TREE)
374     public JCTree visitMemberReference(MemberReferenceTree node, P p) {
375         JCMemberReference t = (JCMemberReference) node;
376         JCExpression expr = copy(t.expr, p);
377         List<JCExpression> typeargs = copy(t.typeargs, p);
378         return M.at(t.pos).Reference(t.mode, t.name, expr, typeargs);
379     }
380 
381     @DefinedBy(Api.COMPILER_TREE)
382     public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
383         JCSkip t = (JCSkip) node;
384         return M.at(t.pos).Skip();
385     }
386 
387     @DefinedBy(Api.COMPILER_TREE)
388     public JCTree visitSwitch(SwitchTree node, P p) {
389         JCSwitch t = (JCSwitch) node;
390         JCExpression selector = copy(t.selector, p);
391         List<JCCase> cases = copy(t.cases, p);
392         return M.at(t.pos).Switch(selector, cases);
393     }
394 
395     @DefinedBy(Api.COMPILER_TREE)
396     public JCTree visitSwitchExpression(SwitchExpressionTree node, P p) {
397         JCSwitchExpression t = (JCSwitchExpression) node;
398         JCExpression selector = copy(t.selector, p);
399         List<JCCase> cases = copy(t.cases, p);
400         return M.at(t.pos).SwitchExpression(selector, cases);
401     }
402 
403     @DefinedBy(Api.COMPILER_TREE)
404     public JCTree visitSynchronized(SynchronizedTree node, P p) {
405         JCSynchronized t = (JCSynchronized) node;
406         JCExpression lock = copy(t.lock, p);
407         JCBlock body = copy(t.body, p);
408         return M.at(t.pos).Synchronized(lock, body);
409     }
410 
411     @DefinedBy(Api.COMPILER_TREE)
412     public JCTree visitThrow(ThrowTree node, P p) {
413         JCThrow t = (JCThrow) node;
414         JCExpression expr = copy(t.expr, p);
415         return M.at(t.pos).Throw(expr);
416     }
417 
418     @DefinedBy(Api.COMPILER_TREE)
419     public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
420         JCCompilationUnit t = (JCCompilationUnit) node;
421         List<JCTree> defs = copy(t.defs, p);
422         return M.at(t.pos).TopLevel(defs);
423     }
424 
425     @DefinedBy(Api.COMPILER_TREE)
426     public JCTree visitPackage(PackageTree node, P p) {
427         JCPackageDecl t = (JCPackageDecl) node;
428         List<JCAnnotation> annotations = copy(t.annotations, p);
429         JCExpression pid = copy(t.pid, p);
430         return M.at(t.pos).PackageDecl(annotations, pid);
431     }
432 
433     @DefinedBy(Api.COMPILER_TREE)
434     public JCTree visitTry(TryTree node, P p) {
435         JCTry t = (JCTry) node;
436         List<JCTree> resources = copy(t.resources, p);
437         JCBlock body = copy(t.body, p);
438         List<JCCatch> catchers = copy(t.catchers, p);
439         JCBlock finalizer = copy(t.finalizer, p);
440         return M.at(t.pos).Try(resources, body, catchers, finalizer);
441     }
442 
443     @DefinedBy(Api.COMPILER_TREE)
444     public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
445         JCTypeApply t = (JCTypeApply) node;
446         JCExpression clazz = copy(t.clazz, p);
447         List<JCExpression> arguments = copy(t.arguments, p);
448         return M.at(t.pos).TypeApply(clazz, arguments);
449     }
450 
451     @DefinedBy(Api.COMPILER_TREE)
452     public JCTree visitUnionType(UnionTypeTree node, P p) {
453         JCTypeUnion t = (JCTypeUnion) node;
454         List<JCExpression> components = copy(t.alternatives, p);
455         return M.at(t.pos).TypeUnion(components);
456     }
457 
458     @DefinedBy(Api.COMPILER_TREE)
459     public JCTree visitIntersectionType(IntersectionTypeTree node, P p) {
460         JCTypeIntersection t = (JCTypeIntersection) node;
461         List<JCExpression> bounds = copy(t.bounds, p);
462         return M.at(t.pos).TypeIntersection(bounds);
463     }
464 
465     @DefinedBy(Api.COMPILER_TREE)
466     public JCTree visitArrayType(ArrayTypeTree node, P p) {
467         JCArrayTypeTree t = (JCArrayTypeTree) node;
468         JCExpression elemtype = copy(t.elemtype, p);
469         return M.at(t.pos).TypeArray(elemtype);
470     }
471 
472     @DefinedBy(Api.COMPILER_TREE)
473     public JCTree visitTypeCast(TypeCastTree node, P p) {
474         JCTypeCast t = (JCTypeCast) node;
475         JCTree clazz = copy(t.clazz, p);
476         JCExpression expr = copy(t.expr, p);
477         return M.at(t.pos).TypeCast(clazz, expr);
478     }
479 
480     @DefinedBy(Api.COMPILER_TREE)
481     public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
482         JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
483         return M.at(t.pos).TypeIdent(t.typetag);
484     }
485 
486     @DefinedBy(Api.COMPILER_TREE)
487     public JCTree visitTypeParameter(TypeParameterTree node, P p) {
488         JCTypeParameter t = (JCTypeParameter) node;
489         List<JCAnnotation> annos = copy(t.annotations, p);
490         List<JCExpression> bounds = copy(t.bounds, p);
491         return M.at(t.pos).TypeParameter(t.name, bounds, annos);
492     }
493 
494     @DefinedBy(Api.COMPILER_TREE)
495     public JCTree visitInstanceOf(InstanceOfTree node, P p) {
496         JCInstanceOf t = (JCInstanceOf) node;
497         JCExpression expr = copy(t.expr, p);
498         JCTree pattern = copy(t.pattern, p);
499         return M.at(t.pos).TypeTest(expr, pattern);
500     }
501 
502     @DefinedBy(Api.COMPILER_TREE)
503     public JCTree visitBindingPattern(BindingPatternTree node, P p) {
504         JCBindingPattern t = (JCBindingPattern) node;
505         JCVariableDecl var = copy(t.var, p);
506         return M.at(t.pos).BindingPattern(var);
507     }
508 
509     @DefinedBy(Api.COMPILER_TREE)
510     public JCTree visitGuardedPattern(GuardedPatternTree node, P p) {
511         JCGuardPattern t = (JCGuardPattern) node;
512         JCPattern patt = copy(t.patt, p);
513         JCExpression expr = copy(t.expr, p);
514         return M.at(t.pos).GuardPattern(patt, expr);
515     }
516 
517     @DefinedBy(Api.COMPILER_TREE)
518     public JCTree visitParenthesizedPattern(ParenthesizedPatternTree node, P p) {
519         JCParenthesizedPattern t = (JCParenthesizedPattern) node;
520         JCPattern pattern = copy(t.pattern, p);
521         return M.at(t.pos).ParenthesizedPattern(pattern);
522     }
523 
524     @DefinedBy(Api.COMPILER_TREE)
525     public JCTree visitDefaultCaseLabel(DefaultCaseLabelTree node, P p) {
526         JCDefaultCaseLabel t = (JCDefaultCaseLabel) node;
527         return M.at(t.pos).DefaultCaseLabel();
528     }
529 
530     @DefinedBy(Api.COMPILER_TREE)
531     public JCTree visitUnary(UnaryTree node, P p) {
532         JCUnary t = (JCUnary) node;
533         JCExpression arg = copy(t.arg, p);
534         return M.at(t.pos).Unary(t.getTag(), arg);
535     }
536 
537     @DefinedBy(Api.COMPILER_TREE)
538     public JCTree visitVariable(VariableTree node, P p) {
539         JCVariableDecl t = (JCVariableDecl) node;
540         JCModifiers mods = copy(t.mods, p);
541         JCExpression vartype = copy(t.vartype, p);
542         if (t.nameexpr == null) {
543             JCExpression init = copy(t.init, p);
544             return M.at(t.pos).VarDef(mods, t.name, vartype, init);
545         } else {
546             JCExpression nameexpr = copy(t.nameexpr, p);
547             return M.at(t.pos).ReceiverVarDef(mods, nameexpr, vartype);
548         }
549     }
550 
551     @DefinedBy(Api.COMPILER_TREE)
552     public JCTree visitWhileLoop(WhileLoopTree node, P p) {
553         JCWhileLoop t = (JCWhileLoop) node;
554         JCStatement body = copy(t.body, p);
555         JCExpression cond = copy(t.cond, p);
556         return M.at(t.pos).WhileLoop(cond, body);
557     }
558 
559     @DefinedBy(Api.COMPILER_TREE)
560     public JCTree visitWithField(WithFieldTree node, P p) {
561         JCWithField t = (JCWithField) node;
562         JCExpression field = copy(t.field, p);
563         JCExpression value = copy(t.value, p);
564         return M.at(t.pos).WithField(field, value);
565     }
566 
567     @DefinedBy(Api.COMPILER_TREE)
568     public JCTree visitWildcard(WildcardTree node, P p) {
569         JCWildcard t = (JCWildcard) node;
570         TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
571         JCTree inner = copy(t.inner, p);
572         return M.at(t.pos).Wildcard(kind, inner);
573     }
574 
575     @Override @DefinedBy(Api.COMPILER_TREE)
576     public JCTree visitModule(ModuleTree node, P p) {
577         JCModuleDecl t = (JCModuleDecl) node;
578         JCModifiers mods = copy(t.mods, p);
579         JCExpression qualId = copy(t.qualId);
580         List<JCDirective> directives = copy(t.directives);
581         return M.at(t.pos).ModuleDef(mods, t.getModuleType(), qualId, directives);
582     }
583 
584     @Override @DefinedBy(Api.COMPILER_TREE)
585     public JCExports visitExports(ExportsTree node, P p) {
586         JCExports t = (JCExports) node;
587         JCExpression qualId = copy(t.qualid, p);
588         List<JCExpression> moduleNames = copy(t.moduleNames, p);
589         return M.at(t.pos).Exports(qualId, moduleNames);
590     }
591 
592     @Override @DefinedBy(Api.COMPILER_TREE)
593     public JCOpens visitOpens(OpensTree node, P p) {
594         JCOpens t = (JCOpens) node;
595         JCExpression qualId = copy(t.qualid, p);
596         List<JCExpression> moduleNames = copy(t.moduleNames, p);
597         return M.at(t.pos).Opens(qualId, moduleNames);
598     }
599 
600     @Override @DefinedBy(Api.COMPILER_TREE)
601     public JCProvides visitProvides(ProvidesTree node, P p) {
602         JCProvides t = (JCProvides) node;
603         JCExpression serviceName = copy(t.serviceName, p);
604         List<JCExpression> implNames = copy(t.implNames, p);
605         return M.at(t.pos).Provides(serviceName, implNames);
606     }
607 
608     @Override @DefinedBy(Api.COMPILER_TREE)
609     public JCRequires visitRequires(RequiresTree node, P p) {
610         JCRequires t = (JCRequires) node;
611         JCExpression moduleName = copy(t.moduleName, p);
612         return M.at(t.pos).Requires(t.isTransitive, t.isStaticPhase, moduleName);
613     }
614 
615     @Override @DefinedBy(Api.COMPILER_TREE)
616     public JCUses visitUses(UsesTree node, P p) {
617         JCUses t = (JCUses) node;
618         JCExpression serviceName = copy(t.qualid, p);
619         return M.at(t.pos).Uses(serviceName);
620     }
621 
622     @DefinedBy(Api.COMPILER_TREE)
623     public JCTree visitOther(Tree node, P p) {
624         JCTree tree = (JCTree) node;
625         switch (tree.getTag()) {
626             case LETEXPR: {
627                 LetExpr t = (LetExpr) node;
628                 List<JCStatement> defs = copy(t.defs, p);
629                 JCExpression expr = copy(t.expr, p);
630                 return M.at(t.pos).LetExpr(defs, expr);
631             }
632             default:
633                 throw new AssertionError("unknown tree tag: " + tree.getTag());
634         }
635     }
636 
637 }