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 */
TreeCopier(TreeMaker M)48     public TreeCopier(TreeMaker M) {
49         this.M = M;
50     }
51 
copy(T tree)52     public <T extends JCTree> T copy(T tree) {
53         return copy(tree, null);
54     }
55 
56     @SuppressWarnings("unchecked")
copy(T tree, P p)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 
copy(List<T> trees)63     public <T extends JCTree> List<T> copy(List<T> trees) {
64         return copy(trees, null);
65     }
66 
copy(List<T> trees, P p)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)
visitAnnotatedType(AnnotatedTypeTree node, P p)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)
visitAnnotation(AnnotationTree node, P p)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)
visitAssert(AssertTree node, P p)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)
visitAssignment(AssignmentTree node, P p)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)
visitCompoundAssignment(CompoundAssignmentTree node, P p)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)
visitBinary(BinaryTree node, P p)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)
visitBlock(BlockTree node, P p)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)
visitBreak(BreakTree node, P p)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)
visitYield(YieldTree node, P p)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)
visitCase(CaseTree node, P p)153     public JCTree visitCase(CaseTree node, P p) {
154         JCCase t = (JCCase) node;
155         List<JCExpression> pats = copy(t.pats, 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, pats, stats, body);
165     }
166 
167     @DefinedBy(Api.COMPILER_TREE)
visitCatch(CatchTree node, P p)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)
visitClass(ClassTree node, P p)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)
visitConditionalExpression(ConditionalExpressionTree node, P p)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)
visitContinue(ContinueTree node, P p)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)
visitDoWhileLoop(DoWhileLoopTree node, P p)202     public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
203         JCDoWhileLoop t = (JCDoWhileLoop) node;
204         JCStatement body = copy(t.body, p);
205         JCExpression cond = copy(t.cond, p);
206         return M.at(t.pos).DoLoop(body, cond);
207     }
208 
209     @DefinedBy(Api.COMPILER_TREE)
visitErroneous(ErroneousTree node, P p)210     public JCTree visitErroneous(ErroneousTree node, P p) {
211         JCErroneous t = (JCErroneous) node;
212         List<? extends JCTree> errs = copy(t.errs, p);
213         return M.at(t.pos).Erroneous(errs);
214     }
215 
216     @DefinedBy(Api.COMPILER_TREE)
visitExpressionStatement(ExpressionStatementTree node, P p)217     public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
218         JCExpressionStatement t = (JCExpressionStatement) node;
219         JCExpression expr = copy(t.expr, p);
220         return M.at(t.pos).Exec(expr);
221     }
222 
223     @DefinedBy(Api.COMPILER_TREE)
visitEnhancedForLoop(EnhancedForLoopTree node, P p)224     public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
225         JCEnhancedForLoop t = (JCEnhancedForLoop) node;
226         JCVariableDecl var = copy(t.var, p);
227         JCExpression expr = copy(t.expr, p);
228         JCStatement body = copy(t.body, p);
229         return M.at(t.pos).ForeachLoop(var, expr, body);
230     }
231 
232     @DefinedBy(Api.COMPILER_TREE)
visitForLoop(ForLoopTree node, P p)233     public JCTree visitForLoop(ForLoopTree node, P p) {
234         JCForLoop t = (JCForLoop) node;
235         List<JCStatement> init = copy(t.init, p);
236         JCExpression cond = copy(t.cond, p);
237         List<JCExpressionStatement> step = copy(t.step, p);
238         JCStatement body = copy(t.body, p);
239         return M.at(t.pos).ForLoop(init, cond, step, body);
240     }
241 
242     @DefinedBy(Api.COMPILER_TREE)
visitIdentifier(IdentifierTree node, P p)243     public JCTree visitIdentifier(IdentifierTree node, P p) {
244         JCIdent t = (JCIdent) node;
245         return M.at(t.pos).Ident(t.name);
246     }
247 
248     @DefinedBy(Api.COMPILER_TREE)
visitIf(IfTree node, P p)249     public JCTree visitIf(IfTree node, P p) {
250         JCIf t = (JCIf) node;
251         JCExpression cond = copy(t.cond, p);
252         JCStatement thenpart = copy(t.thenpart, p);
253         JCStatement elsepart = copy(t.elsepart, p);
254         return M.at(t.pos).If(cond, thenpart, elsepart);
255     }
256 
257     @DefinedBy(Api.COMPILER_TREE)
visitImport(ImportTree node, P p)258     public JCTree visitImport(ImportTree node, P p) {
259         JCImport t = (JCImport) node;
260         JCTree qualid = copy(t.qualid, p);
261         return M.at(t.pos).Import(qualid, t.staticImport);
262     }
263 
264     @DefinedBy(Api.COMPILER_TREE)
visitArrayAccess(ArrayAccessTree node, P p)265     public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
266         JCArrayAccess t = (JCArrayAccess) node;
267         JCExpression indexed = copy(t.indexed, p);
268         JCExpression index = copy(t.index, p);
269         return M.at(t.pos).Indexed(indexed, index);
270     }
271 
272     @DefinedBy(Api.COMPILER_TREE)
visitLabeledStatement(LabeledStatementTree node, P p)273     public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
274         JCLabeledStatement t = (JCLabeledStatement) node;
275         JCStatement body = copy(t.body, p);
276         return M.at(t.pos).Labelled(t.label, body);
277     }
278 
279     @DefinedBy(Api.COMPILER_TREE)
visitLiteral(LiteralTree node, P p)280     public JCTree visitLiteral(LiteralTree node, P p) {
281         JCLiteral t = (JCLiteral) node;
282         return M.at(t.pos).Literal(t.typetag, t.value);
283     }
284 
285     @DefinedBy(Api.COMPILER_TREE)
visitMethod(MethodTree node, P p)286     public JCTree visitMethod(MethodTree node, P p) {
287         JCMethodDecl t  = (JCMethodDecl) node;
288         JCModifiers mods = copy(t.mods, p);
289         JCExpression restype = copy(t.restype, p);
290         List<JCTypeParameter> typarams = copy(t.typarams, p);
291         List<JCVariableDecl> params = copy(t.params, p);
292         JCVariableDecl recvparam = copy(t.recvparam, p);
293         List<JCExpression> thrown = copy(t.thrown, p);
294         JCBlock body = copy(t.body, p);
295         JCExpression defaultValue = copy(t.defaultValue, p);
296         return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, recvparam, params, thrown, body, defaultValue);
297     }
298 
299     @DefinedBy(Api.COMPILER_TREE)
visitMethodInvocation(MethodInvocationTree node, P p)300     public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
301         JCMethodInvocation t = (JCMethodInvocation) node;
302         List<JCExpression> typeargs = copy(t.typeargs, p);
303         JCExpression meth = copy(t.meth, p);
304         List<JCExpression> args = copy(t.args, p);
305         return M.at(t.pos).Apply(typeargs, meth, args);
306     }
307 
308     @DefinedBy(Api.COMPILER_TREE)
visitModifiers(ModifiersTree node, P p)309     public JCTree visitModifiers(ModifiersTree node, P p) {
310         JCModifiers t = (JCModifiers) node;
311         List<JCAnnotation> annotations = copy(t.annotations, p);
312         return M.at(t.pos).Modifiers(t.flags, annotations);
313     }
314 
315     @DefinedBy(Api.COMPILER_TREE)
visitNewArray(NewArrayTree node, P p)316     public JCTree visitNewArray(NewArrayTree node, P p) {
317         JCNewArray t = (JCNewArray) node;
318         JCExpression elemtype = copy(t.elemtype, p);
319         List<JCExpression> dims = copy(t.dims, p);
320         List<JCExpression> elems = copy(t.elems, p);
321         return M.at(t.pos).NewArray(elemtype, dims, elems);
322     }
323 
324     @DefinedBy(Api.COMPILER_TREE)
visitNewClass(NewClassTree node, P p)325     public JCTree visitNewClass(NewClassTree node, P p) {
326         JCNewClass t = (JCNewClass) node;
327         JCExpression encl = copy(t.encl, p);
328         List<JCExpression> typeargs = copy(t.typeargs, p);
329         JCExpression clazz = copy(t.clazz, p);
330         List<JCExpression> args = copy(t.args, p);
331         JCClassDecl def = copy(t.def, p);
332         return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
333     }
334 
335     @DefinedBy(Api.COMPILER_TREE)
visitLambdaExpression(LambdaExpressionTree node, P p)336     public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
337         JCLambda t = (JCLambda) node;
338         List<JCVariableDecl> params = copy(t.params, p);
339         JCTree body = copy(t.body, p);
340         return M.at(t.pos).Lambda(params, body);
341     }
342 
343     @DefinedBy(Api.COMPILER_TREE)
visitParenthesized(ParenthesizedTree node, P p)344     public JCTree visitParenthesized(ParenthesizedTree node, P p) {
345         JCParens t = (JCParens) node;
346         JCExpression expr = copy(t.expr, p);
347         return M.at(t.pos).Parens(expr);
348     }
349 
350     @DefinedBy(Api.COMPILER_TREE)
visitReturn(ReturnTree node, P p)351     public JCTree visitReturn(ReturnTree node, P p) {
352         JCReturn t = (JCReturn) node;
353         JCExpression expr = copy(t.expr, p);
354         return M.at(t.pos).Return(expr);
355     }
356 
357     @DefinedBy(Api.COMPILER_TREE)
visitMemberSelect(MemberSelectTree node, P p)358     public JCTree visitMemberSelect(MemberSelectTree node, P p) {
359         JCFieldAccess t = (JCFieldAccess) node;
360         JCExpression selected = copy(t.selected, p);
361         return M.at(t.pos).Select(selected, t.name);
362     }
363 
364     @DefinedBy(Api.COMPILER_TREE)
visitMemberReference(MemberReferenceTree node, P p)365     public JCTree visitMemberReference(MemberReferenceTree node, P p) {
366         JCMemberReference t = (JCMemberReference) node;
367         JCExpression expr = copy(t.expr, p);
368         List<JCExpression> typeargs = copy(t.typeargs, p);
369         return M.at(t.pos).Reference(t.mode, t.name, expr, typeargs);
370     }
371 
372     @DefinedBy(Api.COMPILER_TREE)
visitEmptyStatement(EmptyStatementTree node, P p)373     public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
374         JCSkip t = (JCSkip) node;
375         return M.at(t.pos).Skip();
376     }
377 
378     @DefinedBy(Api.COMPILER_TREE)
visitSwitch(SwitchTree node, P p)379     public JCTree visitSwitch(SwitchTree node, P p) {
380         JCSwitch t = (JCSwitch) node;
381         JCExpression selector = copy(t.selector, p);
382         List<JCCase> cases = copy(t.cases, p);
383         return M.at(t.pos).Switch(selector, cases);
384     }
385 
386     @DefinedBy(Api.COMPILER_TREE)
visitSwitchExpression(SwitchExpressionTree node, P p)387     public JCTree visitSwitchExpression(SwitchExpressionTree node, P p) {
388         JCSwitchExpression t = (JCSwitchExpression) node;
389         JCExpression selector = copy(t.selector, p);
390         List<JCCase> cases = copy(t.cases, p);
391         return M.at(t.pos).SwitchExpression(selector, cases);
392     }
393 
394     @DefinedBy(Api.COMPILER_TREE)
visitSynchronized(SynchronizedTree node, P p)395     public JCTree visitSynchronized(SynchronizedTree node, P p) {
396         JCSynchronized t = (JCSynchronized) node;
397         JCExpression lock = copy(t.lock, p);
398         JCBlock body = copy(t.body, p);
399         return M.at(t.pos).Synchronized(lock, body);
400     }
401 
402     @DefinedBy(Api.COMPILER_TREE)
visitThrow(ThrowTree node, P p)403     public JCTree visitThrow(ThrowTree node, P p) {
404         JCThrow t = (JCThrow) node;
405         JCExpression expr = copy(t.expr, p);
406         return M.at(t.pos).Throw(expr);
407     }
408 
409     @DefinedBy(Api.COMPILER_TREE)
visitCompilationUnit(CompilationUnitTree node, P p)410     public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
411         JCCompilationUnit t = (JCCompilationUnit) node;
412         List<JCTree> defs = copy(t.defs, p);
413         return M.at(t.pos).TopLevel(defs);
414     }
415 
416     @DefinedBy(Api.COMPILER_TREE)
visitPackage(PackageTree node, P p)417     public JCTree visitPackage(PackageTree node, P p) {
418         JCPackageDecl t = (JCPackageDecl) node;
419         List<JCAnnotation> annotations = copy(t.annotations, p);
420         JCExpression pid = copy(t.pid, p);
421         return M.at(t.pos).PackageDecl(annotations, pid);
422     }
423 
424     @DefinedBy(Api.COMPILER_TREE)
visitTry(TryTree node, P p)425     public JCTree visitTry(TryTree node, P p) {
426         JCTry t = (JCTry) node;
427         List<JCTree> resources = copy(t.resources, p);
428         JCBlock body = copy(t.body, p);
429         List<JCCatch> catchers = copy(t.catchers, p);
430         JCBlock finalizer = copy(t.finalizer, p);
431         return M.at(t.pos).Try(resources, body, catchers, finalizer);
432     }
433 
434     @DefinedBy(Api.COMPILER_TREE)
visitParameterizedType(ParameterizedTypeTree node, P p)435     public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
436         JCTypeApply t = (JCTypeApply) node;
437         JCExpression clazz = copy(t.clazz, p);
438         List<JCExpression> arguments = copy(t.arguments, p);
439         return M.at(t.pos).TypeApply(clazz, arguments);
440     }
441 
442     @DefinedBy(Api.COMPILER_TREE)
visitUnionType(UnionTypeTree node, P p)443     public JCTree visitUnionType(UnionTypeTree node, P p) {
444         JCTypeUnion t = (JCTypeUnion) node;
445         List<JCExpression> components = copy(t.alternatives, p);
446         return M.at(t.pos).TypeUnion(components);
447     }
448 
449     @DefinedBy(Api.COMPILER_TREE)
visitIntersectionType(IntersectionTypeTree node, P p)450     public JCTree visitIntersectionType(IntersectionTypeTree node, P p) {
451         JCTypeIntersection t = (JCTypeIntersection) node;
452         List<JCExpression> bounds = copy(t.bounds, p);
453         return M.at(t.pos).TypeIntersection(bounds);
454     }
455 
456     @DefinedBy(Api.COMPILER_TREE)
visitArrayType(ArrayTypeTree node, P p)457     public JCTree visitArrayType(ArrayTypeTree node, P p) {
458         JCArrayTypeTree t = (JCArrayTypeTree) node;
459         JCExpression elemtype = copy(t.elemtype, p);
460         return M.at(t.pos).TypeArray(elemtype);
461     }
462 
463     @DefinedBy(Api.COMPILER_TREE)
visitTypeCast(TypeCastTree node, P p)464     public JCTree visitTypeCast(TypeCastTree node, P p) {
465         JCTypeCast t = (JCTypeCast) node;
466         JCTree clazz = copy(t.clazz, p);
467         JCExpression expr = copy(t.expr, p);
468         return M.at(t.pos).TypeCast(clazz, expr);
469     }
470 
471     @DefinedBy(Api.COMPILER_TREE)
visitPrimitiveType(PrimitiveTypeTree node, P p)472     public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
473         JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
474         return M.at(t.pos).TypeIdent(t.typetag);
475     }
476 
477     @DefinedBy(Api.COMPILER_TREE)
visitTypeParameter(TypeParameterTree node, P p)478     public JCTree visitTypeParameter(TypeParameterTree node, P p) {
479         JCTypeParameter t = (JCTypeParameter) node;
480         List<JCAnnotation> annos = copy(t.annotations, p);
481         List<JCExpression> bounds = copy(t.bounds, p);
482         return M.at(t.pos).TypeParameter(t.name, bounds, annos);
483     }
484 
485     @DefinedBy(Api.COMPILER_TREE)
visitInstanceOf(InstanceOfTree node, P p)486     public JCTree visitInstanceOf(InstanceOfTree node, P p) {
487         JCInstanceOf t = (JCInstanceOf) node;
488         JCExpression expr = copy(t.expr, p);
489         JCTree pattern = copy(t.pattern, p);
490         return M.at(t.pos).TypeTest(expr, pattern);
491     }
492 
493     @DefinedBy(Api.COMPILER_TREE)
visitBindingPattern(BindingPatternTree node, P p)494     public JCTree visitBindingPattern(BindingPatternTree node, P p) {
495         JCBindingPattern t = (JCBindingPattern) node;
496         JCVariableDecl var = copy(t.var, p);
497         return M.at(t.pos).BindingPattern(var);
498     }
499 
500     @DefinedBy(Api.COMPILER_TREE)
visitUnary(UnaryTree node, P p)501     public JCTree visitUnary(UnaryTree node, P p) {
502         JCUnary t = (JCUnary) node;
503         JCExpression arg = copy(t.arg, p);
504         return M.at(t.pos).Unary(t.getTag(), arg);
505     }
506 
507     @DefinedBy(Api.COMPILER_TREE)
visitVariable(VariableTree node, P p)508     public JCTree visitVariable(VariableTree node, P p) {
509         JCVariableDecl t = (JCVariableDecl) node;
510         JCModifiers mods = copy(t.mods, p);
511         JCExpression vartype = copy(t.vartype, p);
512         if (t.nameexpr == null) {
513             JCExpression init = copy(t.init, p);
514             return M.at(t.pos).VarDef(mods, t.name, vartype, init);
515         } else {
516             JCExpression nameexpr = copy(t.nameexpr, p);
517             return M.at(t.pos).ReceiverVarDef(mods, nameexpr, vartype);
518         }
519     }
520 
521     @DefinedBy(Api.COMPILER_TREE)
visitWhileLoop(WhileLoopTree node, P p)522     public JCTree visitWhileLoop(WhileLoopTree node, P p) {
523         JCWhileLoop t = (JCWhileLoop) node;
524         JCStatement body = copy(t.body, p);
525         JCExpression cond = copy(t.cond, p);
526         return M.at(t.pos).WhileLoop(cond, body);
527     }
528 
529     @DefinedBy(Api.COMPILER_TREE)
visitWildcard(WildcardTree node, P p)530     public JCTree visitWildcard(WildcardTree node, P p) {
531         JCWildcard t = (JCWildcard) node;
532         TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
533         JCTree inner = copy(t.inner, p);
534         return M.at(t.pos).Wildcard(kind, inner);
535     }
536 
537     @Override @DefinedBy(Api.COMPILER_TREE)
visitModule(ModuleTree node, P p)538     public JCTree visitModule(ModuleTree node, P p) {
539         JCModuleDecl t = (JCModuleDecl) node;
540         JCModifiers mods = copy(t.mods, p);
541         JCExpression qualId = copy(t.qualId);
542         List<JCDirective> directives = copy(t.directives);
543         return M.at(t.pos).ModuleDef(mods, t.getModuleType(), qualId, directives);
544     }
545 
546     @Override @DefinedBy(Api.COMPILER_TREE)
visitExports(ExportsTree node, P p)547     public JCExports visitExports(ExportsTree node, P p) {
548         JCExports t = (JCExports) node;
549         JCExpression qualId = copy(t.qualid, p);
550         List<JCExpression> moduleNames = copy(t.moduleNames, p);
551         return M.at(t.pos).Exports(qualId, moduleNames);
552     }
553 
554     @Override @DefinedBy(Api.COMPILER_TREE)
visitOpens(OpensTree node, P p)555     public JCOpens visitOpens(OpensTree node, P p) {
556         JCOpens t = (JCOpens) node;
557         JCExpression qualId = copy(t.qualid, p);
558         List<JCExpression> moduleNames = copy(t.moduleNames, p);
559         return M.at(t.pos).Opens(qualId, moduleNames);
560     }
561 
562     @Override @DefinedBy(Api.COMPILER_TREE)
visitProvides(ProvidesTree node, P p)563     public JCProvides visitProvides(ProvidesTree node, P p) {
564         JCProvides t = (JCProvides) node;
565         JCExpression serviceName = copy(t.serviceName, p);
566         List<JCExpression> implNames = copy(t.implNames, p);
567         return M.at(t.pos).Provides(serviceName, implNames);
568     }
569 
570     @Override @DefinedBy(Api.COMPILER_TREE)
visitRequires(RequiresTree node, P p)571     public JCRequires visitRequires(RequiresTree node, P p) {
572         JCRequires t = (JCRequires) node;
573         JCExpression moduleName = copy(t.moduleName, p);
574         return M.at(t.pos).Requires(t.isTransitive, t.isStaticPhase, moduleName);
575     }
576 
577     @Override @DefinedBy(Api.COMPILER_TREE)
visitUses(UsesTree node, P p)578     public JCUses visitUses(UsesTree node, P p) {
579         JCUses t = (JCUses) node;
580         JCExpression serviceName = copy(t.qualid, p);
581         return M.at(t.pos).Uses(serviceName);
582     }
583 
584     @DefinedBy(Api.COMPILER_TREE)
visitOther(Tree node, P p)585     public JCTree visitOther(Tree node, P p) {
586         JCTree tree = (JCTree) node;
587         switch (tree.getTag()) {
588             case LETEXPR: {
589                 LetExpr t = (LetExpr) node;
590                 List<JCStatement> defs = copy(t.defs, p);
591                 JCExpression expr = copy(t.expr, p);
592                 return M.at(t.pos).LetExpr(defs, expr);
593             }
594             default:
595                 throw new AssertionError("unknown tree tag: " + tree.getTag());
596         }
597     }
598 
599 }
600