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