Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/jdk.compiler/share/classes/com/sun/source/util/DocTreeScanner.java
66647 views
1
/*
2
* Copyright (c) 2011, 2021, 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.source.util;
27
28
import com.sun.source.doctree.*;
29
30
31
/**
32
* A DocTreeVisitor that visits all the child tree nodes.
33
* To visit nodes of a particular type, just override the
34
* corresponding visitXYZ method.
35
* Inside your method, call super.visitXYZ to visit descendant
36
* nodes.
37
*
38
* <p>Here is an example to count the number of erroneous nodes in a tree:
39
* <pre>
40
* class CountErrors extends DocTreeScanner&lt;Integer,Void&gt; {
41
* {@literal @}Override
42
* public Integer visitErroneous(ErroneousTree node, Void p) {
43
* return 1;
44
* }
45
* {@literal @}Override
46
* public Integer reduce(Integer r1, Integer r2) {
47
* return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
48
* }
49
* }
50
* </pre>
51
*
52
* @implSpec
53
* <p>The default implementation of the visitXYZ methods will determine
54
* a result as follows:
55
* <ul>
56
* <li>If the node being visited has no children, the result will be {@code null}.
57
* <li>If the node being visited has one child, the result will be the
58
* result of calling {@code scan} with that child. The child may be a simple node
59
* or itself a list of nodes.
60
* <li>If the node being visited has more than one child, the result will
61
* be determined by calling {@code scan} with each child in turn, and then combining the
62
* result of each scan after the first with the cumulative result
63
* so far, as determined by the {@link #reduce} method. Each child may be either
64
* a simple node or a list of nodes. The default behavior of the {@code reduce}
65
* method is such that the result of the visitXYZ method will be the result of
66
* the last child scanned.
67
* </ul>
68
*
69
* @since 1.8
70
*/
71
public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
72
/**
73
* Constructs a {@code DocTreeScanner}.
74
*/
75
public DocTreeScanner() {}
76
77
/**
78
* Scans a single node.
79
* @param node the node to be scanned
80
* @param p a parameter value passed to the visit method
81
* @return the result value from the visit method
82
*/
83
public R scan(DocTree node, P p) {
84
return (node == null) ? null : node.accept(this, p);
85
}
86
87
private R scanAndReduce(DocTree node, P p, R r) {
88
return reduce(scan(node, p), r);
89
}
90
91
/**
92
* Scans a sequence of nodes.
93
* @param nodes the nodes to be scanned
94
* @param p a parameter value to be passed to the visit method for each node
95
* @return the combined return value from the visit methods.
96
* The values are combined using the {@link #reduce reduce} method.
97
*/
98
public R scan(Iterable<? extends DocTree> nodes, P p) {
99
R r = null;
100
if (nodes != null) {
101
boolean first = true;
102
for (DocTree node : nodes) {
103
r = (first ? scan(node, p) : scanAndReduce(node, p, r));
104
first = false;
105
}
106
}
107
return r;
108
}
109
110
private R scanAndReduce(Iterable<? extends DocTree> nodes, P p, R r) {
111
return reduce(scan(nodes, p), r);
112
}
113
114
/**
115
* Reduces two results into a combined result.
116
* The default implementation is to return the first parameter.
117
* The general contract of the method is that it may take any action whatsoever.
118
* @param r1 the first of the values to be combined
119
* @param r2 the second of the values to be combined
120
* @return the result of combining the two parameters
121
*/
122
public R reduce(R r1, R r2) {
123
return r1;
124
}
125
126
127
/* ***************************************************************************
128
* Visitor methods
129
****************************************************************************/
130
131
/**
132
* {@inheritDoc}
133
*
134
* @implSpec This implementation scans the children in left to right order.
135
*
136
* @param node {@inheritDoc}
137
* @param p {@inheritDoc}
138
* @return the result of scanning
139
*/
140
@Override
141
public R visitAttribute(AttributeTree node, P p) {
142
return scan(node.getValue(), p);
143
}
144
145
/**
146
* {@inheritDoc}
147
*
148
* @implSpec This implementation scans the children in left to right order.
149
*
150
* @param node {@inheritDoc}
151
* @param p {@inheritDoc}
152
* @return the result of scanning
153
*/
154
@Override
155
public R visitAuthor(AuthorTree node, P p) {
156
return scan(node.getName(), p);
157
}
158
159
/**
160
* {@inheritDoc}
161
*
162
* @implSpec This implementation returns {@code null}.
163
*
164
* @param node {@inheritDoc}
165
* @param p {@inheritDoc}
166
* @return the result of scanning
167
*/
168
@Override
169
public R visitComment(CommentTree node, P p) {
170
return null;
171
}
172
173
/**
174
* {@inheritDoc}
175
*
176
* @implSpec 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 visitDeprecated(DeprecatedTree node, P p) {
184
return scan(node.getBody(), p);
185
}
186
187
/**
188
* {@inheritDoc}
189
*
190
* @implSpec This implementation scans the children in left to right order.
191
*
192
* @param node {@inheritDoc}
193
* @param p {@inheritDoc}
194
* @return the result of scanning
195
*/
196
@Override
197
public R visitDocComment(DocCommentTree node, P p) {
198
R r = scan(node.getFirstSentence(), p);
199
r = scanAndReduce(node.getBody(), p, r);
200
r = scanAndReduce(node.getBlockTags(), p, r);
201
return r;
202
}
203
204
/**
205
* {@inheritDoc}
206
*
207
* @implSpec This implementation returns {@code null}.
208
*
209
* @param node {@inheritDoc}
210
* @param p {@inheritDoc}
211
* @return the result of scanning
212
*/
213
@Override
214
public R visitDocRoot(DocRootTree node, P p) {
215
return null;
216
}
217
218
/**
219
* {@inheritDoc}
220
*
221
* @implSpec This implementation returns {@code null}.
222
*
223
* @param node {@inheritDoc}
224
* @param p {@inheritDoc}
225
* @return the result of scanning
226
*/
227
@Override
228
public R visitDocType(DocTypeTree node, P p) {
229
return null;
230
}
231
232
/**
233
* {@inheritDoc}
234
*
235
* @implSpec This implementation returns {@code null}.
236
*
237
* @param node {@inheritDoc}
238
* @param p {@inheritDoc}
239
* @return the result of scanning
240
*/
241
@Override
242
public R visitEndElement(EndElementTree node, P p) {
243
return null;
244
}
245
246
/**
247
* {@inheritDoc}
248
*
249
* @implSpec This implementation returns {@code null}.
250
*
251
* @param node {@inheritDoc}
252
* @param p {@inheritDoc}
253
* @return the result of scanning
254
*/
255
@Override
256
public R visitEntity(EntityTree node, P p) {
257
return null;
258
}
259
260
/**
261
* {@inheritDoc}
262
*
263
* @implSpec This implementation returns {@code null}.
264
*
265
* @param node {@inheritDoc}
266
* @param p {@inheritDoc}
267
* @return the result of scanning
268
*/
269
@Override
270
public R visitErroneous(ErroneousTree node, P p) {
271
return null;
272
}
273
274
/**
275
* {@inheritDoc}
276
*
277
* @implSpec 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 visitHidden(HiddenTree node, P p) {
285
return scan(node.getBody(), p);
286
}
287
288
/**
289
* {@inheritDoc}
290
*
291
* @implSpec 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 visitIdentifier(IdentifierTree node, P p) {
299
return null;
300
}
301
302
/**
303
* {@inheritDoc}
304
*
305
* @implSpec This implementation scans the children in left to right order.
306
*
307
* @param node {@inheritDoc}
308
* @param p {@inheritDoc}
309
* @return the result of scanning
310
*/
311
@Override
312
public R visitIndex(IndexTree node, P p) {
313
R r = scan(node.getSearchTerm(), p);
314
r = scanAndReduce(node.getDescription(), p, r);
315
return r;
316
}
317
318
/**
319
* {@inheritDoc}
320
*
321
* @implSpec This implementation returns {@code null}.
322
*
323
* @param node {@inheritDoc}
324
* @param p {@inheritDoc}
325
* @return the result of scanning
326
*/
327
@Override
328
public R visitInheritDoc(InheritDocTree node, P p) {
329
return null;
330
}
331
332
/**
333
* {@inheritDoc}
334
*
335
* @implSpec This implementation scans the children in left to right order.
336
*
337
* @param node {@inheritDoc}
338
* @param p {@inheritDoc}
339
* @return the result of scanning
340
*/
341
@Override
342
public R visitLink(LinkTree node, P p) {
343
R r = scan(node.getReference(), p);
344
r = scanAndReduce(node.getLabel(), p, r);
345
return r;
346
}
347
348
/**
349
* {@inheritDoc}
350
*
351
* @implSpec This implementation scans the children in left to right order.
352
*
353
* @param node {@inheritDoc}
354
* @param p {@inheritDoc}
355
* @return the result of scanning
356
*/
357
@Override
358
public R visitLiteral(LiteralTree node, P p) {
359
return scan(node.getBody(), p);
360
}
361
362
/**
363
* {@inheritDoc}
364
*
365
* @implSpec 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 visitParam(ParamTree node, P p) {
373
R r = scan(node.getName(), p);
374
r = scanAndReduce(node.getDescription(), p, r);
375
return r;
376
}
377
378
/**
379
* {@inheritDoc}
380
*
381
* @implSpec This implementation scans the children in left to right order.
382
*
383
* @param node {@inheritDoc}
384
* @param p {@inheritDoc}
385
* @return the result of scanning
386
*/
387
@Override
388
public R visitProvides(ProvidesTree node, P p) {
389
R r = scan(node.getServiceType(), p);
390
r = scanAndReduce(node.getDescription(), p, r);
391
return r;
392
}
393
394
/**
395
* {@inheritDoc}
396
*
397
* @implSpec This implementation returns {@code null}.
398
*
399
* @param node {@inheritDoc}
400
* @param p {@inheritDoc}
401
* @return the result of scanning
402
*/
403
@Override
404
public R visitReference(ReferenceTree node, P p) {
405
return null;
406
}
407
408
/**
409
* {@inheritDoc}
410
*
411
* @implSpec This implementation scans the children in left to right order.
412
*
413
* @param node {@inheritDoc}
414
* @param p {@inheritDoc}
415
* @return the result of scanning
416
*/
417
@Override
418
public R visitReturn(ReturnTree node, P p) {
419
return scan(node.getDescription(), p);
420
}
421
422
/**
423
* {@inheritDoc}
424
*
425
* @implSpec This implementation scans the children in left to right order.
426
*
427
* @param node {@inheritDoc}
428
* @param p {@inheritDoc}
429
* @return the result of scanning
430
*/
431
@Override
432
public R visitSee(SeeTree node, P p) {
433
return scan(node.getReference(), p);
434
}
435
436
/**
437
* {@inheritDoc}
438
*
439
* @implSpec This implementation scans the children in left to right order.
440
*
441
* @param node {@inheritDoc}
442
* @param p {@inheritDoc}
443
* @return the result of scanning
444
*/
445
@Override
446
public R visitSerial(SerialTree node, P p) {
447
return scan(node.getDescription(), p);
448
}
449
450
/**
451
* {@inheritDoc}
452
*
453
* @implSpec This implementation scans the children in left to right order.
454
*
455
* @param node {@inheritDoc}
456
* @param p {@inheritDoc}
457
* @return the result of scanning
458
*/
459
@Override
460
public R visitSerialData(SerialDataTree node, P p) {
461
return scan(node.getDescription(), p);
462
}
463
464
/**
465
* {@inheritDoc}
466
*
467
* @implSpec This implementation scans the children in left to right order.
468
*
469
* @param node {@inheritDoc}
470
* @param p {@inheritDoc}
471
* @return the result of scanning
472
*/
473
@Override
474
public R visitSerialField(SerialFieldTree node, P p) {
475
R r = scan(node.getName(), p);
476
r = scanAndReduce(node.getType(), p, r);
477
r = scanAndReduce(node.getDescription(), p, r);
478
return r;
479
}
480
481
/**
482
* {@inheritDoc}
483
*
484
* @implSpec This implementation scans the children in left to right order.
485
*
486
* @param node {@inheritDoc}
487
* @param p {@inheritDoc}
488
* @return the result of scanning
489
*/
490
@Override
491
public R visitSince(SinceTree node, P p) {
492
return scan(node.getBody(), p);
493
}
494
495
/**
496
* {@inheritDoc}
497
*
498
* @implSpec This implementation scans the children in left to right order.
499
*
500
* @param node {@inheritDoc}
501
* @param p {@inheritDoc}
502
* @return the result of scanning
503
*/
504
@Override
505
public R visitStartElement(StartElementTree node, P p) {
506
return scan(node.getAttributes(), p);
507
}
508
509
/**
510
* {@inheritDoc}
511
*
512
* @implSpec This implementation scans the children in left to right order.
513
*
514
* @param node {@inheritDoc}
515
* @param p {@inheritDoc}
516
* @return the result of scanning
517
* @since 10
518
*/
519
@Override
520
public R visitSummary(SummaryTree node, P p) {
521
return scan(node.getSummary(), p);
522
}
523
524
/**
525
* {@inheritDoc}
526
*
527
* @implSpec This implementation returns {@code null}.
528
*
529
* @param node {@inheritDoc}
530
* @param p {@inheritDoc}
531
* @return the result of scanning
532
* @since 12
533
*/
534
@Override
535
public R visitSystemProperty(SystemPropertyTree node, P p) {
536
return null;
537
}
538
539
/**
540
* {@inheritDoc}
541
*
542
* @implSpec This implementation returns {@code null}.
543
*
544
* @param node {@inheritDoc}
545
* @param p {@inheritDoc}
546
* @return the result of scanning
547
*/
548
@Override
549
public R visitText(TextTree node, P p) {
550
return null;
551
}
552
553
/**
554
* {@inheritDoc}
555
*
556
* @implSpec This implementation scans the children in left to right order.
557
*
558
* @param node {@inheritDoc}
559
* @param p {@inheritDoc}
560
* @return the result of scanning
561
*/
562
@Override
563
public R visitThrows(ThrowsTree node, P p) {
564
R r = scan(node.getExceptionName(), p);
565
r = scanAndReduce(node.getDescription(), p, r);
566
return r;
567
}
568
569
/**
570
* {@inheritDoc}
571
*
572
* @implSpec This implementation scans the children in left to right order.
573
*
574
* @param node {@inheritDoc}
575
* @param p {@inheritDoc}
576
* @return the result of scanning
577
*/
578
@Override
579
public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {
580
return scan(node.getContent(), p);
581
}
582
583
/**
584
* {@inheritDoc}
585
*
586
* @implSpec This implementation scans the children in left to right order.
587
*
588
* @param node {@inheritDoc}
589
* @param p {@inheritDoc}
590
* @return the result of scanning
591
*/
592
@Override
593
public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {
594
return scan(node.getContent(), p);
595
}
596
597
/**
598
* {@inheritDoc}
599
*
600
* @implSpec This implementation scans the children in left to right order.
601
*
602
* @param node {@inheritDoc}
603
* @param p {@inheritDoc}
604
* @return the result of scanning
605
*/
606
@Override
607
public R visitUses(UsesTree node, P p) {
608
R r = scan(node.getServiceType(), p);
609
r = scanAndReduce(node.getDescription(), p, r);
610
return r;
611
}
612
613
/**
614
* {@inheritDoc}
615
*
616
* @implSpec This implementation scans the children in left to right order.
617
*
618
* @param node {@inheritDoc}
619
* @param p {@inheritDoc}
620
* @return the result of scanning
621
*/
622
@Override
623
public R visitValue(ValueTree node, P p) {
624
return scan(node.getReference(), p);
625
}
626
627
/**
628
* {@inheritDoc}
629
*
630
* @implSpec This implementation scans the children in left to right order.
631
*
632
* @param node {@inheritDoc}
633
* @param p {@inheritDoc}
634
* @return the result of scanning
635
*/
636
@Override
637
public R visitVersion(VersionTree node, P p) {
638
return scan(node.getBody(), p);
639
}
640
641
/**
642
* {@inheritDoc}
643
*
644
* @implSpec This implementation returns {@code null}.
645
*
646
* @param node {@inheritDoc}
647
* @param p {@inheritDoc}
648
* @return the result of scanning
649
*/
650
@Override
651
public R visitOther(DocTree node, P p) {
652
return null;
653
}
654
655
}
656
657