DocTreeScanner.java revision 3193:3b3bea483542
1/*
2 * Copyright (c) 2011, 2015, 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.doctree.*;
29import com.sun.tools.javac.tree.DCTree.DCIndex;
30
31
32/**
33 * A TreeVisitor that visits all the child tree nodes.
34 * To visit nodes of a particular type, just override the
35 * corresponding visitXYZ method.
36 * Inside your method, call super.visitXYZ to visit descendant
37 * nodes.
38 *
39 * <p>The default implementation of the visitXYZ methods will determine
40 * a result as follows:
41 * <ul>
42 * <li>If the node being visited has no children, the result will be {@code null}.
43 * <li>If the node being visited has one child, the result will be the
44 * result of calling {@code scan} on that child. The child may be a simple node
45 * or itself a list of nodes.
46 * <li> If the node being visited has more than one child, the result will
47 * be determined by calling {@code scan} each child in turn, and then combining the
48 * result of each scan after the first with the cumulative result
49 * so far, as determined by the {@link #reduce} method. Each child may be either
50 * a simple node of a list of nodes. The default behavior of the {@code reduce}
51 * method is such that the result of the visitXYZ method will be the result of
52 * the last child scanned.
53 * </ul>
54 *
55 * <p>Here is an example to count the number of erroneous nodes in a tree:
56 * <pre>
57 *   class CountErrors extends DocTreeScanner&lt;Integer,Void&gt; {
58 *      {@literal @}Override
59 *      public Integer visitErroneous(ErroneousTree node, Void p) {
60 *          return 1;
61 *      }
62 *      {@literal @}Override
63 *      public Integer reduce(Integer r1, Integer r2) {
64 *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
65 *      }
66 *   }
67 * </pre>
68 *
69 * @since 1.8
70 */
71public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
72
73    /**
74     * Scans a single node.
75     * @param node the node to be scanned
76     * @param p a parameter value passed to the visit method
77     * @return the result value from the visit method
78     */
79    public R scan(DocTree node, P p) {
80        return (node == null) ? null : node.accept(this, p);
81    }
82
83    private R scanAndReduce(DocTree node, P p, R r) {
84        return reduce(scan(node, p), r);
85    }
86
87    /**
88     * Scans a sequence of nodes.
89     * @param nodes the nodes to be scanned
90     * @param p a parameter value to be passed to the visit method for each node
91     * @return the combined return value from the visit methods.
92     *      The values are combined using the {@link #reduce reduce} method.
93     */
94    public R scan(Iterable<? extends DocTree> nodes, P p) {
95        R r = null;
96        if (nodes != null) {
97            boolean first = true;
98            for (DocTree node : nodes) {
99                r = (first ? scan(node, p) : scanAndReduce(node, p, r));
100                first = false;
101            }
102        }
103        return r;
104    }
105
106    private R scanAndReduce(Iterable<? extends DocTree> nodes, P p, R r) {
107        return reduce(scan(nodes, p), r);
108    }
109
110    /**
111     * Reduces two results into a combined result.
112     * The default implementation is to return the first parameter.
113     * The general contract of the method is that it may take any action whatsoever.
114     * @param r1 the first of the values to be combined
115     * @param r2 the second of the values to be combined
116     * @return the result of combining the two parameters
117     */
118    public R reduce(R r1, R r2) {
119        return r1;
120    }
121
122
123/* ***************************************************************************
124 * Visitor methods
125 ****************************************************************************/
126
127    /**
128     * {@inheritDoc} This implementation returns {@code null}.
129     *
130     * @param node  {@inheritDoc}
131     * @param p  {@inheritDoc}
132     * @return the result of scanning
133     */
134    @Override
135    public R visitAttribute(AttributeTree node, P p) {
136        return null;
137    }
138
139    /**
140     * {@inheritDoc} This implementation scans the children in left to right order.
141     *
142     * @param node  {@inheritDoc}
143     * @param p  {@inheritDoc}
144     * @return the result of scanning
145     */
146    @Override
147    public R visitAuthor(AuthorTree node, P p) {
148        return scan(node.getName(), p);
149    }
150
151    /**
152     * {@inheritDoc} This implementation returns {@code null}.
153     *
154     * @param node  {@inheritDoc}
155     * @param p  {@inheritDoc}
156     * @return the result of scanning
157     */
158    @Override
159    public R visitComment(CommentTree node, P p) {
160        return null;
161    }
162
163    /**
164     * {@inheritDoc} This implementation scans the children in left to right order.
165     *
166     * @param node  {@inheritDoc}
167     * @param p  {@inheritDoc}
168     * @return the result of scanning
169     */
170    @Override
171    public R visitDeprecated(DeprecatedTree node, P p) {
172        return scan(node.getBody(), p);
173    }
174
175    /**
176     * {@inheritDoc} This implementation scans the children in left to right order.
177     *
178     * @param node  {@inheritDoc}
179     * @param p  {@inheritDoc}
180     * @return the result of scanning
181     */
182    @Override
183    public R visitDocComment(DocCommentTree node, P p) {
184        R r = scan(node.getFirstSentence(), p);
185        r = scanAndReduce(node.getBody(), p, r);
186        r = scanAndReduce(node.getBlockTags(), p, r);
187        return r;
188    }
189
190    /**
191     * {@inheritDoc} This implementation returns {@code null}.
192     *
193     * @param node  {@inheritDoc}
194     * @param p  {@inheritDoc}
195     * @return the result of scanning
196     */
197    @Override
198    public R visitDocRoot(DocRootTree node, P p) {
199        return null;
200    }
201
202    /**
203     * {@inheritDoc} This implementation returns {@code null}.
204     *
205     * @param node  {@inheritDoc}
206     * @param p  {@inheritDoc}
207     * @return the result of scanning
208     */
209    @Override
210    public R visitEndElement(EndElementTree node, P p) {
211        return null;
212    }
213
214    /**
215     * {@inheritDoc} This implementation returns {@code null}.
216     *
217     * @param node  {@inheritDoc}
218     * @param p  {@inheritDoc}
219     * @return the result of scanning
220     */
221    @Override
222    public R visitEntity(EntityTree node, P p) {
223        return null;
224    }
225
226    /**
227     * {@inheritDoc} This implementation returns {@code null}.
228     *
229     * @param node  {@inheritDoc}
230     * @param p  {@inheritDoc}
231     * @return the result of scanning
232     */
233    @Override
234    public R visitErroneous(ErroneousTree node, P p) {
235        return null;
236    }
237
238    /**
239     * {@inheritDoc} This implementation returns {@code null}.
240     *
241     * @param node  {@inheritDoc}
242     * @param p  {@inheritDoc}
243     * @return the result of scanning
244     */
245    @Override
246    public R visitIdentifier(IdentifierTree node, P p) {
247        return null;
248    }
249
250    /**
251     * {@inheritDoc} This implementation returns {@code null}.
252     *
253     * @param node  {@inheritDoc}
254     * @param p  {@inheritDoc}
255     * @return the result of scanning
256     */
257    @Override
258    public R visitIndex(IndexTree node, P p) {
259        R r = scan(node.getSearchTerm(), p);
260        r = scanAndReduce(node.getDescription(), p, r);
261        return r;
262    }
263
264    /**
265     * {@inheritDoc} This implementation returns {@code null}.
266     *
267     * @param node  {@inheritDoc}
268     * @param p  {@inheritDoc}
269     * @return the result of scanning
270     */
271    @Override
272    public R visitInheritDoc(InheritDocTree node, P p) {
273        return null;
274    }
275
276    /**
277     * {@inheritDoc} This implementation scans the children in left to right order.
278     *
279     * @param node  {@inheritDoc}
280     * @param p  {@inheritDoc}
281     * @return the result of scanning
282     */
283    @Override
284    public R visitLink(LinkTree node, P p) {
285        R r = scan(node.getReference(), p);
286        r = scanAndReduce(node.getLabel(), p, r);
287        return r;
288    }
289
290    /**
291     * {@inheritDoc} This implementation returns {@code null}.
292     *
293     * @param node  {@inheritDoc}
294     * @param p  {@inheritDoc}
295     * @return the result of scanning
296     */
297    @Override
298    public R visitLiteral(LiteralTree node, P p) {
299        return null;
300    }
301
302    /**
303     * {@inheritDoc} This implementation scans the children in left to right order.
304     *
305     * @param node  {@inheritDoc}
306     * @param p  {@inheritDoc}
307     * @return the result of scanning
308     */
309    @Override
310    public R visitParam(ParamTree node, P p) {
311        R r = scan(node.getName(), p);
312        r = scanAndReduce(node.getDescription(), p, r);
313        return r;
314    }
315
316    /**
317     * {@inheritDoc} This implementation returns {@code null}.
318     *
319     * @param node  {@inheritDoc}
320     * @param p  {@inheritDoc}
321     * @return the result of scanning
322     */
323    @Override
324    public R visitReference(ReferenceTree node, P p) {
325        return null;
326    }
327
328    /**
329     * {@inheritDoc} This implementation scans the children in left to right order.
330     *
331     * @param node  {@inheritDoc}
332     * @param p  {@inheritDoc}
333     * @return the result of scanning
334     */
335    @Override
336    public R visitReturn(ReturnTree node, P p) {
337        return scan(node.getDescription(), p);
338    }
339
340    /**
341     * {@inheritDoc} This implementation scans the children in left to right order.
342     *
343     * @param node  {@inheritDoc}
344     * @param p  {@inheritDoc}
345     * @return the result of scanning
346     */
347    @Override
348    public R visitSee(SeeTree node, P p) {
349        return scan(node.getReference(), p);
350    }
351
352    /**
353     * {@inheritDoc} This implementation scans the children in left to right order.
354     *
355     * @param node  {@inheritDoc}
356     * @param p  {@inheritDoc}
357     * @return the result of scanning
358     */
359    @Override
360    public R visitSerial(SerialTree node, P p) {
361        return scan(node.getDescription(), p);
362    }
363
364    /**
365     * {@inheritDoc} This implementation scans the children in left to right order.
366     *
367     * @param node  {@inheritDoc}
368     * @param p  {@inheritDoc}
369     * @return the result of scanning
370     */
371    @Override
372    public R visitSerialData(SerialDataTree node, P p) {
373        return scan(node.getDescription(), p);
374    }
375
376    /**
377     * {@inheritDoc} This implementation scans the children in left to right order.
378     *
379     * @param node  {@inheritDoc}
380     * @param p  {@inheritDoc}
381     * @return the result of scanning
382     */
383    @Override
384    public R visitSerialField(SerialFieldTree node, P p) {
385        R r = scan(node.getName(), p);
386        r = scanAndReduce(node.getType(), p, r);
387        r = scanAndReduce(node.getDescription(), p, r);
388        return r;
389    }
390
391    /**
392     * {@inheritDoc} This implementation scans the children in left to right order.
393     *
394     * @param node  {@inheritDoc}
395     * @param p  {@inheritDoc}
396     * @return the result of scanning
397     */
398    @Override
399    public R visitSince(SinceTree node, P p) {
400        return scan(node.getBody(), p);
401    }
402
403    /**
404     * {@inheritDoc} This implementation scans the children in left to right order.
405     *
406     * @param node  {@inheritDoc}
407     * @param p  {@inheritDoc}
408     * @return the result of scanning
409     */
410    @Override
411    public R visitStartElement(StartElementTree node, P p) {
412        return scan(node.getAttributes(), p);
413    }
414
415    /**
416     * {@inheritDoc} This implementation returns {@code null}.
417     *
418     * @param node  {@inheritDoc}
419     * @param p  {@inheritDoc}
420     * @return the result of scanning
421     */
422    @Override
423    public R visitText(TextTree node, P p) {
424        return null;
425    }
426
427    /**
428     * {@inheritDoc} This implementation scans the children in left to right order.
429     *
430     * @param node  {@inheritDoc}
431     * @param p  {@inheritDoc}
432     * @return the result of scanning
433     */
434    @Override
435    public R visitThrows(ThrowsTree node, P p) {
436        R r = scan(node.getExceptionName(), p);
437        r = scanAndReduce(node.getDescription(), p, r);
438        return r;
439    }
440
441    /**
442     * {@inheritDoc} This implementation scans the children in left to right order.
443     *
444     * @param node  {@inheritDoc}
445     * @param p  {@inheritDoc}
446     * @return the result of scanning
447     */
448    @Override
449    public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {
450        return scan(node.getContent(), p);
451    }
452
453    /**
454     * {@inheritDoc} This implementation scans the children in left to right order.
455     *
456     * @param node  {@inheritDoc}
457     * @param p  {@inheritDoc}
458     * @return the result of scanning
459     */
460    @Override
461    public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {
462        return scan(node.getContent(), p);
463    }
464
465    /**
466     * {@inheritDoc} This implementation scans the children in left to right order.
467     *
468     * @param node  {@inheritDoc}
469     * @param p  {@inheritDoc}
470     * @return the result of scanning
471     */
472    @Override
473    public R visitValue(ValueTree node, P p) {
474        return scan(node.getReference(), p);
475    }
476
477    /**
478     * {@inheritDoc} This implementation scans the children in left to right order.
479     *
480     * @param node  {@inheritDoc}
481     * @param p  {@inheritDoc}
482     * @return the result of scanning
483     */
484    @Override
485    public R visitVersion(VersionTree node, P p) {
486        return scan(node.getBody(), p);
487    }
488
489    /**
490     * {@inheritDoc} This implementation returns {@code null}.
491     *
492     * @param node  {@inheritDoc}
493     * @param p  {@inheritDoc}
494     * @return the result of scanning
495     */
496    @Override
497    public R visitOther(DocTree node, P p) {
498        return null;
499    }
500
501}
502