SimpleTreeVisitor.java revision 3792:d516975e8110
1/*
2 * Copyright (c) 2005, 2016, 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
26package com.sun.source.util;
27
28import com.sun.source.tree.*;
29
30/**
31 * A simple visitor for tree nodes.
32 *
33 * @param <R> the return type of this visitor's methods.  Use {@link
34 *            Void} for visitors that do not need to return results.
35 * @param <P> the type of the additional parameter to this visitor's
36 *            methods.  Use {@code Void} for visitors that do not need an
37 *            additional parameter.
38 *
39 * @author Peter von der Ah&eacute;
40 * @since 1.6
41 */
42public class SimpleTreeVisitor <R,P> implements TreeVisitor<R,P> {
43    /**
44     * The default value, returned by the {@link #defaultAction default action}.
45     */
46    protected final R DEFAULT_VALUE;
47
48    /**
49     * Creates a visitor, with a DEFAULT_VALUE of {@code null}.
50     */
51    protected SimpleTreeVisitor() {
52        DEFAULT_VALUE = null;
53    }
54
55    /**
56     * Creates a visitor, with a specified DEFAULT_VALUE.
57     * @param defaultValue the default value to be returned by the default action.
58     */
59    protected SimpleTreeVisitor(R defaultValue) {
60        DEFAULT_VALUE = defaultValue;
61    }
62
63    /**
64     * The default action, used by all visit methods that are not overridden.
65     * @param node the node being visited
66     * @param p the parameter value passed to the visit method
67     * @return the result value to be returned from the visit method
68     */
69    protected R defaultAction(Tree node, P p) {
70        return DEFAULT_VALUE;
71    }
72
73    /**
74     * Invokes the appropriate visit method specific to the type of the node.
75     * @param node the node on which to dispatch
76     * @param p a parameter to be passed to the appropriate visit method
77     * @return the value returns from the appropriate visit method
78     */
79    public final R visit(Tree node, P p) {
80        return (node == null) ? null : node.accept(this, p);
81    }
82
83    /**
84     * Invokes the appropriate visit method on each of a sequence of nodes.
85     * @param nodes the nodes on which to dispatch
86     * @param p a parameter value to be passed to each appropriate visit method
87     * @return the value return from the last of the visit methods, or null
88     *      if none were called.
89     */
90    public final R visit(Iterable<? extends Tree> nodes, P p) {
91        R r = null;
92        if (nodes != null)
93            for (Tree node : nodes)
94                r = visit(node, p);
95        return r;
96    }
97
98    /**
99     * {@inheritDoc} This implementation calls {@code defaultAction}.
100     *
101     * @param node {@inheritDoc}
102     * @param p {@inheritDoc}
103     * @return  the result of {@code defaultAction}
104     */
105    @Override
106    public R visitCompilationUnit(CompilationUnitTree node, P p) {
107        return defaultAction(node, p);
108    }
109
110    /**
111     * {@inheritDoc} This implementation calls {@code defaultAction}.
112     *
113     * @param node {@inheritDoc}
114     * @param p {@inheritDoc}
115     * @return  the result of {@code defaultAction}
116     */
117    @Override
118    public R visitPackage(PackageTree node, P p) {
119        return defaultAction(node, p);
120    }
121
122    /**
123     * {@inheritDoc} This implementation calls {@code defaultAction}.
124     *
125     * @param node {@inheritDoc}
126     * @param p {@inheritDoc}
127     * @return  the result of {@code defaultAction}
128     */
129    @Override
130    public R visitImport(ImportTree node, P p) {
131        return defaultAction(node, p);
132    }
133
134    /**
135     * {@inheritDoc} This implementation calls {@code defaultAction}.
136     *
137     * @param node {@inheritDoc}
138     * @param p {@inheritDoc}
139     * @return  the result of {@code defaultAction}
140     */
141    @Override
142    public R visitClass(ClassTree node, P p) {
143        return defaultAction(node, p);
144    }
145
146    /**
147     * {@inheritDoc} This implementation calls {@code defaultAction}.
148     *
149     * @param node {@inheritDoc}
150     * @param p {@inheritDoc}
151     * @return  the result of {@code defaultAction}
152     */
153    @Override
154    public R visitMethod(MethodTree node, P p) {
155        return defaultAction(node, p);
156    }
157
158    /**
159     * {@inheritDoc} This implementation calls {@code defaultAction}.
160     *
161     * @param node {@inheritDoc}
162     * @param p {@inheritDoc}
163     * @return  the result of {@code defaultAction}
164     */
165    @Override
166    public R visitVariable(VariableTree node, P p) {
167        return defaultAction(node, p);
168    }
169
170    /**
171     * {@inheritDoc} This implementation calls {@code defaultAction}.
172     *
173     * @param node {@inheritDoc}
174     * @param p {@inheritDoc}
175     * @return  the result of {@code defaultAction}
176     */
177    @Override
178    public R visitEmptyStatement(EmptyStatementTree node, P p) {
179        return defaultAction(node, p);
180    }
181
182    /**
183     * {@inheritDoc} This implementation calls {@code defaultAction}.
184     *
185     * @param node {@inheritDoc}
186     * @param p {@inheritDoc}
187     * @return  the result of {@code defaultAction}
188     */
189    @Override
190    public R visitBlock(BlockTree node, P p) {
191        return defaultAction(node, p);
192    }
193
194    /**
195     * {@inheritDoc} This implementation calls {@code defaultAction}.
196     *
197     * @param node {@inheritDoc}
198     * @param p {@inheritDoc}
199     * @return  the result of {@code defaultAction}
200     */
201    @Override
202    public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
203        return defaultAction(node, p);
204    }
205
206    /**
207     * {@inheritDoc} This implementation calls {@code defaultAction}.
208     *
209     * @param node {@inheritDoc}
210     * @param p {@inheritDoc}
211     * @return  the result of {@code defaultAction}
212     */
213    @Override
214    public R visitWhileLoop(WhileLoopTree node, P p) {
215        return defaultAction(node, p);
216    }
217
218    /**
219     * {@inheritDoc} This implementation calls {@code defaultAction}.
220     *
221     * @param node {@inheritDoc}
222     * @param p {@inheritDoc}
223     * @return  the result of {@code defaultAction}
224     */
225    @Override
226    public R visitForLoop(ForLoopTree node, P p) {
227        return defaultAction(node, p);
228    }
229
230    /**
231     * {@inheritDoc} This implementation calls {@code defaultAction}.
232     *
233     * @param node {@inheritDoc}
234     * @param p {@inheritDoc}
235     * @return  the result of {@code defaultAction}
236     */
237    @Override
238    public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
239        return defaultAction(node, p);
240    }
241
242    /**
243     * {@inheritDoc} This implementation calls {@code defaultAction}.
244     *
245     * @param node {@inheritDoc}
246     * @param p {@inheritDoc}
247     * @return  the result of {@code defaultAction}
248     */
249    @Override
250    public R visitLabeledStatement(LabeledStatementTree node, P p) {
251        return defaultAction(node, p);
252    }
253
254    /**
255     * {@inheritDoc} This implementation calls {@code defaultAction}.
256     *
257     * @param node {@inheritDoc}
258     * @param p {@inheritDoc}
259     * @return  the result of {@code defaultAction}
260     */
261    @Override
262    public R visitSwitch(SwitchTree node, P p) {
263        return defaultAction(node, p);
264    }
265
266    /**
267     * {@inheritDoc} This implementation calls {@code defaultAction}.
268     *
269     * @param node {@inheritDoc}
270     * @param p {@inheritDoc}
271     * @return  the result of {@code defaultAction}
272     */
273    @Override
274    public R visitCase(CaseTree node, P p) {
275        return defaultAction(node, p);
276    }
277
278    /**
279     * {@inheritDoc} This implementation calls {@code defaultAction}.
280     *
281     * @param node {@inheritDoc}
282     * @param p {@inheritDoc}
283     * @return  the result of {@code defaultAction}
284     */
285    @Override
286    public R visitSynchronized(SynchronizedTree node, P p) {
287        return defaultAction(node, p);
288    }
289
290    /**
291     * {@inheritDoc} This implementation calls {@code defaultAction}.
292     *
293     * @param node {@inheritDoc}
294     * @param p {@inheritDoc}
295     * @return  the result of {@code defaultAction}
296     */
297    @Override
298    public R visitTry(TryTree node, P p) {
299        return defaultAction(node, p);
300    }
301
302    /**
303     * {@inheritDoc} This implementation calls {@code defaultAction}.
304     *
305     * @param node {@inheritDoc}
306     * @param p {@inheritDoc}
307     * @return  the result of {@code defaultAction}
308     */
309    @Override
310    public R visitCatch(CatchTree node, P p) {
311        return defaultAction(node, p);
312    }
313
314    /**
315     * {@inheritDoc} This implementation calls {@code defaultAction}.
316     *
317     * @param node {@inheritDoc}
318     * @param p {@inheritDoc}
319     * @return  the result of {@code defaultAction}
320     */
321    @Override
322    public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
323        return defaultAction(node, p);
324    }
325
326    /**
327     * {@inheritDoc} This implementation calls {@code defaultAction}.
328     *
329     * @param node {@inheritDoc}
330     * @param p {@inheritDoc}
331     * @return  the result of {@code defaultAction}
332     */
333    @Override
334    public R visitIf(IfTree node, P p) {
335        return defaultAction(node, p);
336    }
337
338    /**
339     * {@inheritDoc} This implementation calls {@code defaultAction}.
340     *
341     * @param node {@inheritDoc}
342     * @param p {@inheritDoc}
343     * @return  the result of {@code defaultAction}
344     */
345    @Override
346    public R visitExpressionStatement(ExpressionStatementTree node, P p) {
347        return defaultAction(node, p);
348    }
349
350    /**
351     * {@inheritDoc} This implementation calls {@code defaultAction}.
352     *
353     * @param node {@inheritDoc}
354     * @param p {@inheritDoc}
355     * @return  the result of {@code defaultAction}
356     */
357    @Override
358    public R visitBreak(BreakTree node, P p) {
359        return defaultAction(node, p);
360    }
361
362    /**
363     * {@inheritDoc} This implementation calls {@code defaultAction}.
364     *
365     * @param node {@inheritDoc}
366     * @param p {@inheritDoc}
367     * @return  the result of {@code defaultAction}
368     */
369    @Override
370    public R visitContinue(ContinueTree node, P p) {
371        return defaultAction(node, p);
372    }
373
374    /**
375     * {@inheritDoc} This implementation calls {@code defaultAction}.
376     *
377     * @param node {@inheritDoc}
378     * @param p {@inheritDoc}
379     * @return  the result of {@code defaultAction}
380     */
381    @Override
382    public R visitReturn(ReturnTree node, P p) {
383        return defaultAction(node, p);
384    }
385
386    /**
387     * {@inheritDoc} This implementation calls {@code defaultAction}.
388     *
389     * @param node {@inheritDoc}
390     * @param p {@inheritDoc}
391     * @return  the result of {@code defaultAction}
392     */
393    @Override
394    public R visitThrow(ThrowTree node, P p) {
395        return defaultAction(node, p);
396    }
397
398    /**
399     * {@inheritDoc} This implementation calls {@code defaultAction}.
400     *
401     * @param node {@inheritDoc}
402     * @param p {@inheritDoc}
403     * @return  the result of {@code defaultAction}
404     */
405    @Override
406    public R visitAssert(AssertTree node, P p) {
407        return defaultAction(node, p);
408    }
409
410    /**
411     * {@inheritDoc} This implementation calls {@code defaultAction}.
412     *
413     * @param node {@inheritDoc}
414     * @param p {@inheritDoc}
415     * @return  the result of {@code defaultAction}
416     */
417    @Override
418    public R visitMethodInvocation(MethodInvocationTree node, P p) {
419        return defaultAction(node, p);
420    }
421
422    /**
423     * {@inheritDoc} This implementation calls {@code defaultAction}.
424     *
425     * @param node {@inheritDoc}
426     * @param p {@inheritDoc}
427     * @return  the result of {@code defaultAction}
428     */
429    @Override
430    public R visitNewClass(NewClassTree node, P p) {
431        return defaultAction(node, p);
432    }
433
434    /**
435     * {@inheritDoc} This implementation calls {@code defaultAction}.
436     *
437     * @param node {@inheritDoc}
438     * @param p {@inheritDoc}
439     * @return  the result of {@code defaultAction}
440     */
441    @Override
442    public R visitNewArray(NewArrayTree node, P p) {
443        return defaultAction(node, p);
444    }
445
446    /**
447     * {@inheritDoc} This implementation calls {@code defaultAction}.
448     *
449     * @param node {@inheritDoc}
450     * @param p {@inheritDoc}
451     * @return  the result of {@code defaultAction}
452     */
453    @Override
454    public R visitLambdaExpression(LambdaExpressionTree node, P p) {
455        return defaultAction(node, p);
456    }
457
458    /**
459     * {@inheritDoc} This implementation calls {@code defaultAction}.
460     *
461     * @param node {@inheritDoc}
462     * @param p {@inheritDoc}
463     * @return  the result of {@code defaultAction}
464     */
465    @Override
466    public R visitParenthesized(ParenthesizedTree node, P p) {
467        return defaultAction(node, p);
468    }
469
470    /**
471     * {@inheritDoc} This implementation calls {@code defaultAction}.
472     *
473     * @param node {@inheritDoc}
474     * @param p {@inheritDoc}
475     * @return  the result of {@code defaultAction}
476     */
477    @Override
478    public R visitAssignment(AssignmentTree node, P p) {
479        return defaultAction(node, p);
480    }
481
482    /**
483     * {@inheritDoc} This implementation calls {@code defaultAction}.
484     *
485     * @param node {@inheritDoc}
486     * @param p {@inheritDoc}
487     * @return  the result of {@code defaultAction}
488     */
489    @Override
490    public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
491        return defaultAction(node, p);
492    }
493
494    /**
495     * {@inheritDoc} This implementation calls {@code defaultAction}.
496     *
497     * @param node {@inheritDoc}
498     * @param p {@inheritDoc}
499     * @return  the result of {@code defaultAction}
500     */
501    @Override
502    public R visitUnary(UnaryTree node, P p) {
503        return defaultAction(node, p);
504    }
505
506    /**
507     * {@inheritDoc} This implementation calls {@code defaultAction}.
508     *
509     * @param node {@inheritDoc}
510     * @param p {@inheritDoc}
511     * @return  the result of {@code defaultAction}
512     */
513    @Override
514    public R visitBinary(BinaryTree node, P p) {
515        return defaultAction(node, p);
516    }
517
518    /**
519     * {@inheritDoc} This implementation calls {@code defaultAction}.
520     *
521     * @param node {@inheritDoc}
522     * @param p {@inheritDoc}
523     * @return  the result of {@code defaultAction}
524     */
525    @Override
526    public R visitTypeCast(TypeCastTree node, P p) {
527        return defaultAction(node, p);
528    }
529
530    /**
531     * {@inheritDoc} This implementation calls {@code defaultAction}.
532     *
533     * @param node {@inheritDoc}
534     * @param p {@inheritDoc}
535     * @return  the result of {@code defaultAction}
536     */
537    @Override
538    public R visitInstanceOf(InstanceOfTree node, P p) {
539        return defaultAction(node, p);
540    }
541
542    /**
543     * {@inheritDoc} This implementation calls {@code defaultAction}.
544     *
545     * @param node {@inheritDoc}
546     * @param p {@inheritDoc}
547     * @return  the result of {@code defaultAction}
548     */
549    @Override
550    public R visitArrayAccess(ArrayAccessTree node, P p) {
551        return defaultAction(node, p);
552    }
553
554    /**
555     * {@inheritDoc} This implementation calls {@code defaultAction}.
556     *
557     * @param node {@inheritDoc}
558     * @param p {@inheritDoc}
559     * @return  the result of {@code defaultAction}
560     */
561    @Override
562    public R visitMemberSelect(MemberSelectTree node, P p) {
563        return defaultAction(node, p);
564    }
565
566    /**
567     * {@inheritDoc} This implementation calls {@code defaultAction}.
568     *
569     * @param node {@inheritDoc}
570     * @param p {@inheritDoc}
571     * @return  the result of {@code defaultAction}
572     */
573    @Override
574    public R visitMemberReference(MemberReferenceTree node, P p) {
575        return defaultAction(node, p);
576    }
577
578    /**
579     * {@inheritDoc} This implementation calls {@code defaultAction}.
580     *
581     * @param node {@inheritDoc}
582     * @param p {@inheritDoc}
583     * @return  the result of {@code defaultAction}
584     */
585    @Override
586    public R visitIdentifier(IdentifierTree node, P p) {
587        return defaultAction(node, p);
588    }
589
590    /**
591     * {@inheritDoc} This implementation calls {@code defaultAction}.
592     *
593     * @param node {@inheritDoc}
594     * @param p {@inheritDoc}
595     * @return  the result of {@code defaultAction}
596     */
597    @Override
598    public R visitLiteral(LiteralTree node, P p) {
599        return defaultAction(node, p);
600    }
601
602    /**
603     * {@inheritDoc} This implementation calls {@code defaultAction}.
604     *
605     * @param node {@inheritDoc}
606     * @param p {@inheritDoc}
607     * @return  the result of {@code defaultAction}
608     */
609    @Override
610    public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
611        return defaultAction(node, p);
612    }
613
614    /**
615     * {@inheritDoc} This implementation calls {@code defaultAction}.
616     *
617     * @param node {@inheritDoc}
618     * @param p {@inheritDoc}
619     * @return  the result of {@code defaultAction}
620     */
621    @Override
622    public R visitArrayType(ArrayTypeTree node, P p) {
623        return defaultAction(node, p);
624    }
625
626    /**
627     * {@inheritDoc} This implementation calls {@code defaultAction}.
628     *
629     * @param node {@inheritDoc}
630     * @param p {@inheritDoc}
631     * @return  the result of {@code defaultAction}
632     */
633    @Override
634    public R visitParameterizedType(ParameterizedTypeTree node, P p) {
635        return defaultAction(node, p);
636    }
637
638    /**
639     * {@inheritDoc} This implementation calls {@code defaultAction}.
640     *
641     * @param node {@inheritDoc}
642     * @param p {@inheritDoc}
643     * @return  the result of {@code defaultAction}
644     */
645    @Override
646    public R visitUnionType(UnionTypeTree node, P p) {
647        return defaultAction(node, p);
648    }
649
650    /**
651     * {@inheritDoc} This implementation calls {@code defaultAction}.
652     *
653     * @param node {@inheritDoc}
654     * @param p {@inheritDoc}
655     * @return  the result of {@code defaultAction}
656     */
657    @Override
658    public R visitIntersectionType(IntersectionTypeTree node, P p) {
659        return defaultAction(node, p);
660    }
661
662    /**
663     * {@inheritDoc} This implementation calls {@code defaultAction}.
664     *
665     * @param node {@inheritDoc}
666     * @param p {@inheritDoc}
667     * @return  the result of {@code defaultAction}
668     */
669    @Override
670    public R visitTypeParameter(TypeParameterTree node, P p) {
671        return defaultAction(node, p);
672    }
673
674    /**
675     * {@inheritDoc} This implementation calls {@code defaultAction}.
676     *
677     * @param node {@inheritDoc}
678     * @param p {@inheritDoc}
679     * @return  the result of {@code defaultAction}
680     */
681    @Override
682    public R visitWildcard(WildcardTree node, P p) {
683        return defaultAction(node, p);
684    }
685
686    /**
687     * {@inheritDoc} This implementation calls {@code defaultAction}.
688     *
689     * @param node {@inheritDoc}
690     * @param p {@inheritDoc}
691     * @return  the result of {@code defaultAction}
692     */
693    @Override
694    public R visitModifiers(ModifiersTree node, P p) {
695        return defaultAction(node, p);
696    }
697
698    /**
699     * {@inheritDoc} This implementation calls {@code defaultAction}.
700     *
701     * @param node {@inheritDoc}
702     * @param p {@inheritDoc}
703     * @return  the result of {@code defaultAction}
704     */
705    @Override
706    public R visitAnnotation(AnnotationTree node, P p) {
707        return defaultAction(node, p);
708    }
709
710    /**
711     * {@inheritDoc} This implementation calls {@code defaultAction}.
712     *
713     * @param node {@inheritDoc}
714     * @param p {@inheritDoc}
715     * @return  the result of {@code defaultAction}
716     */
717    @Override
718    public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
719        return defaultAction(node, p);
720    }
721
722    public R visitModule(ModuleTree node, P p) {
723        return defaultAction(node, p);
724    }
725
726    @Override
727    public R visitExports(ExportsTree node, P p) {
728        return defaultAction(node, p);
729    }
730
731    @Override
732    public R visitOpens(OpensTree node, P p) {
733        return defaultAction(node, p);
734    }
735
736    @Override
737    public R visitProvides(ProvidesTree node, P p) {
738        return defaultAction(node, p);
739    }
740
741    @Override
742    public R visitRequires(RequiresTree node, P p) {
743        return defaultAction(node, p);
744    }
745
746    @Override
747    public R visitUses(UsesTree node, P p) {
748        return defaultAction(node, p);
749    }
750
751    public R visitErroneous(ErroneousTree node, P p) {
752        return defaultAction(node, p);
753    }
754
755    /**
756     * {@inheritDoc} This implementation calls {@code defaultAction}.
757     *
758     * @param node {@inheritDoc}
759     * @param p {@inheritDoc}
760     * @return  the result of {@code defaultAction}
761     */
762    @Override
763    public R visitOther(Tree node, P p) {
764        return defaultAction(node, p);
765    }
766}
767