-
Notifications
You must be signed in to change notification settings - Fork 8
/
writingjavadocchecks.html
1387 lines (1110 loc) · 49.3 KB
/
writingjavadocchecks.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.11.1 from src/xdocs/writingjavadocchecks.xml.vm at 2024-12-12
| Rendered using Apache Maven Default Skin
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 1.11.1" />
<title>checkstyle – Writing Javadoc Checks</title>
<link rel="stylesheet" href="./css/maven-base.css" />
<link rel="stylesheet" href="./css/maven-theme.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script type="text/javascript" src="./js/checkstyle.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="./js/anchors.js"></script>
<script type="text/javascript" src="./js/google-analytics.js"></script>
<link rel="icon" href="./images/favicon.png" type="image/x-icon" />
<link rel="shortcut icon" href="./images/favicon.ico" type="image/ico" />
</head>
<body class="composite">
<div id="banner">
<a href="./" id="bannerLeft" title="Checkstyle"><img src="images/header-checkstyle-logo.png" alt="Checkstyle"/></a><a href="./" id="bannerRight" title="Checkstyle"><img src="images/header-right-ruller.png" alt="Checkstyle"/></a> <div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xright"><a href="" title="toTop">toTop</a> | <span id="publishDate">Last Published: 2024-12-12</span>
| <span id="projectVersion">Version: 10.21.0</span>
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>About</h5>
<ul>
<li class="none"><a href="index.html" title="Checkstyle">Checkstyle</a></li>
<li class="none"><a href="releasenotes.html" title="Release Notes">Release Notes</a></li>
<li class="none"><a href="consulting.html" title="Consulting">Consulting</a></li>
<li class="none"><a href="sponsoring.html" title="Sponsoring">Sponsoring</a></li>
</ul>
<h5>Documentation</h5>
<ul>
<li class="expanded"><a href="config.html" title="Configuration">Configuration</a>
<ul>
<li class="none"><a href="property_types.html" title="Property Types">Property Types</a></li>
<li class="none"><a href="config_system_properties.html" title="System Properties">System Properties</a></li>
</ul></li>
<li class="expanded"><a href="running.html" title="Running">Running</a>
<ul>
<li class="none"><a href="anttask.html" title="Ant Task">Ant Task</a></li>
<li class="none"><a href="cmdline.html" title="Command Line">Command Line</a></li>
</ul></li>
<li class="expanded"><a href="checks.html" title="Checks">Checks</a>
<ul>
<li class="collapsed"><a href="checks/annotation/index.html" title="Annotations">Annotations</a></li>
<li class="collapsed"><a href="checks/blocks/index.html" title="Block Checks">Block Checks</a></li>
<li class="collapsed"><a href="checks/design/index.html" title="Class Design">Class Design</a></li>
<li class="collapsed"><a href="checks/coding/index.html" title="Coding">Coding</a></li>
<li class="collapsed"><a href="checks/header/index.html" title="Headers">Headers</a></li>
<li class="collapsed"><a href="checks/imports/index.html" title="Imports">Imports</a></li>
<li class="collapsed"><a href="checks/javadoc/index.html" title="Javadoc Comments">Javadoc Comments</a></li>
<li class="collapsed"><a href="checks/metrics/index.html" title="Metrics">Metrics</a></li>
<li class="collapsed"><a href="checks/misc/index.html" title="Miscellaneous">Miscellaneous</a></li>
<li class="collapsed"><a href="checks/modifier/index.html" title="Modifiers">Modifiers</a></li>
<li class="collapsed"><a href="checks/naming/index.html" title="Naming Conventions">Naming Conventions</a></li>
<li class="collapsed"><a href="checks/regexp/index.html" title="Regexp">Regexp</a></li>
<li class="collapsed"><a href="checks/sizes/index.html" title="Size Violations">Size Violations</a></li>
<li class="collapsed"><a href="checks/whitespace/index.html" title="Whitespace">Whitespace</a></li>
</ul></li>
<li class="collapsed"><a href="filters/index.html" title="Filters">Filters</a></li>
<li class="collapsed"><a href="filefilters/index.html" title="File Filters">File Filters</a></li>
<li class="expanded"><a href="style_configs.html" title="Style Configurations">Style Configurations</a>
<ul>
<li class="none"><a href="google_style.html" title="Google's Style">Google's Style</a></li>
<li class="none"><a href="sun_style.html" title="Sun's Style">Sun's Style</a></li>
</ul></li>
</ul>
<h5>Developers</h5>
<ul>
<li class="expanded"><a href="extending.html" title="Extending Checkstyle">Extending Checkstyle</a>
<ul>
<li class="none"><a href="writingchecks.html" title="Writing Checks">Writing Checks</a></li>
<li class="none"><strong>Writing Javadoc Checks</strong></li>
<li class="none"><a href="writingfilters.html" title="Writing Filters">Writing Filters</a></li>
<li class="none"><a href="writingfilefilters.html" title="Writing File Filters">Writing File Filters</a></li>
<li class="none"><a href="writinglisteners.html" title="Writing Listeners">Writing Listeners</a></li>
</ul></li>
<li class="none"><a href="contributing.html" title="Contributing">Contributing</a></li>
<li class="expanded"><a href="beginning_development.html" title="Beginning Development">Beginning Development</a>
<ul>
<li class="none"><a href="eclipse.html" title="Eclipse IDE">Eclipse IDE</a></li>
<li class="none"><a href="netbeans.html" title="NetBeans IDE">NetBeans IDE</a></li>
<li class="none"><a href="idea.html" title="IntelliJ IDE">IntelliJ IDE</a></li>
</ul></li>
<li class="none"><a href="apidocs/index.html" title="Javadoc">Javadoc</a></li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed"><a href="project-info.html" title="Project Information">Project Information</a></li>
<li class="collapsed"><a href="project-reports.html" title="Project Reports">Project Reports</a></li>
</ul>
<a href="https://github.com/checkstyle/checkstyle" title="GitHub" class="poweredBy">
<img class="poweredBy" alt="GitHub" src="images/github_logo_social_coding_outlined.png" />
</a>
<a href="https://twitter.com/checkstyle_java/" title="Twitter" class="poweredBy">
<img class="poweredBy" alt="Twitter" src="images/twitter_button.png" />
</a>
<a href="https://stackoverflow.com/questions/tagged/checkstyle" title="Stackoverflow" class="poweredBy">
<img class="poweredBy" alt="Stackoverflow" src="images/stackoverflow.jpeg" />
</a>
<a href="https://groups.google.com/forum/#!forum/checkstyle" title="GoogleGroups" class="poweredBy">
<img class="poweredBy" alt="GoogleGroups" src="images/groups.png" />
</a>
<a href="https://www.ej-technologies.com/products/jprofiler/overview.html" title="JProfiler" class="poweredBy">
<img class="poweredBy" alt="JProfiler" src="https://www.ej-technologies.com/images/product_banners/jprofiler_medium.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<section>
<h2><a name="Content"></a>Content</h2>
<ul>
<li><a href="#Content">Content</a></li>
<li><a href="#What_is_Javadoc_comment">What is Javadoc comment</a></li>
<li><a href="#Limitations">Limitations</a></li>
<li><a href="#Tight-HTML_rules">Tight-HTML rules</a></li>
<li><a href="#How_to_create_Javadoc_Check">How to create Javadoc Check</a></li>
<li><a href="#Difference_between_Java_Grammar_and_Javadoc_comments_Grammar">Difference between Java Grammar and Javadoc comments Grammar</a></li>
<li><a href="#Tool_to_print_Javadoc_tree_structure">Tool to print Javadoc tree structure</a></li>
<li><a href="#Access_Java_AST_from_Javadoc_Check">Access Java AST from Javadoc Check</a></li>
<li><a href="#HTML_Code_In_Javadoc_Comments">HTML Code In Javadoc Comments</a></li>
<li><a href="#Checkstyle_SDK_GUI">Checkstyle SDK GUI</a></li>
<li><a href="#Customize_token_types_in_Javadoc_Checks">Customize token types in Javadoc Checks</a></li>
<li><a href="#Integrating_new_Javadoc_Check">Integrating new Javadoc Check</a></li>
<li><a href="#Declare_check.27s_external_resource_locations">Declare check's external resource locations</a></li>
<li><a href="#Examples_of_Javadoc_Checks">Examples of Javadoc Checks</a></li>
<li><a href="#Javadoc_parser_behavior_for_current_HTML_version_and_new_HTML_version">Javadoc parser behavior for current HTML version and new HTML version</a></li></ul>
</section>
<section>
<h2><a name="What_is_Javadoc_comment"></a>What is Javadoc comment</h2>
<p>
Javadoc comment is a multiline comment <code>/* */</code> that starts with the <b>*</b>
character and placed above the class definition, interface definition, enum definition,
method definition or field definition.
If an annotation precedes any of the definitions listed above, then the javadoc comment
should be placed before the annotation.
If several multiline comments with javadoc identifiers are placed sequentially, only
the one closest to the definition, right above it, with the javadoc identifier will be used.
</p>
<p>
Javadoc comments should contain: a short summary (the first sentence), an optional
documentation section, an optional tag section. The first sentence has a special meaning
and should be clear, punchy, short, and is ended by a period symbol.
Immediately after the first sentence, the main description could begin,
which may be followed by the tag section. The tag section starts with the first block tag,
which is defined by the first <code>@</code> character that begins a line
(ignoring leading asterisks, white space, and leading separator <code>/**</code>).
</p>
<p>For example, here is java file:</p>
<div class="source">
<pre>
/**
* My <b>class</b>.
*
* @see Annotation
*/
public class MyClass {
/** Not a javadoc (ignored). */
/**
* Doubles the value.
* The long and detailed explanation what the method does.
*
* @param value for doubling.
* @return double value.
*/
/*
Multiline comment (ignored).
*/
@Annotation
/** Extra javadoc (ignored). */
// Single line comment (ignored).
public int method(int value) {
/** Inner javadoc (ignored). */
return value * 2;
}
}
</pre></div>
<p>Javadoc content for the MyClass will be:</p>
<div class="source">
<pre>
My <b>class</b>.
@see Annotation
</pre></div>
<p>Javadoc content for the MyClass.method will be:</p>
<div class="source">
<pre>
Doubles the value.
The long and detailed explanation what the method does.
@param value for doubling.
@return double value.
</pre></div>
<p>
Attention that java comment starts with <code>/*</code>, following with Identifier of
comment type. Javadoc Identifier is <code>*</code>. All symbols after Javadoc
Identifier till <code>*/</code> are part of javadoc comment.
</p>
<p>Please note that javadoc-like (multiline comment with javadoc identifier) comment inside
a method is not a javadoc comment and skipped by Sun/Oracle javadoc tool and by our javadoc
comment matcher, but such comment will be in AST.
</p>
<p>
You can find different types of documentation generation tools similar to javadoc on the
Internet. Such tools rely on specific Identifier: "!", "#", "$".
Comments look like <code>"/*! some comment */"</code> , <code>"/*# some comment */"</code> ,
<code>"/*$ some comment */"</code>. Such multiline comments are not a javadoc.
</p>
</section>
<section>
<h2><a name="Limitations"></a>Limitations</h2>
<p>
Since Oracle itself does not provide a comprehensive language specification for Javadocs,
we have interpreted the existing behavior of the Javadoc tool and accepted standards
to form our Javadoc grammar and Abstract Syntax Tree (AST). For this reason, we must
impose the following limitations on Javadoc parsing and checks.
</p>
<p>
The comment should be written in <a href="#Tight-HTML_rules">Tight-HTML</a>
to build ASTs that most Checks expect.
</p>
<p>
For more details about parsing of HTML into AST read
<a href="#HTML_Code_In_Javadoc_Comments">HTML Code In Javadoc Comments</a>
and <a href="#Javadoc_parser_behavior_for_current_HTML_version_and_new_HTML_version">
Javadoc parser behavior
</a> section.
</p>
</section>
<section>
<h2><a name="Tight-HTML_rules"></a>Tight-HTML rules</h2>
<p>
Every HTML tag should have matching end HTML tag or it is a
<a class="externalLink" href="https://www.w3.org/TR/html/syntax.html#void-elements">void element</a>.
</p>
<p>
The only exceptions are HTML 4 tags whose end tag is optional (omittable) by
HTML specification (example is
<a class="externalLink" href="https://www.w3.org/TR/html5/tabular-data.html#the-tr-element">TR</a>), so,
Checkstyle won't show an error about missing end tags, however,
it leads to broken Tight-HTML structure and as a result leads to non-nested content
of the HTML tags in the Abstract Syntax Tree of the Javadoc comment.
<br />
In other words, if HTML tags are not closed in Javadoc, our parser cannot determine the
content of these tags, so structure of the parse tree will not be nested like it is while
using <a href="#Tight-HTML_rules">Tight-HTML</a>.
</p>
<p>
Other rules:
</p>
<ul>
<li>Document Structure elements (DOCTYPE, <html>, <body>, etc) are
not mandatory.</li>
<li>Elements must always be closed, except HTML4 elements whose end tag is optional
(omittable) and HTML4
<a class="externalLink" href="https://www.w3.org/TR/html/syntax.html#void-elements">void elements</a>.
See <a href="#HTML_Code_In_Javadoc_Comments">HTML Code In Javadoc Comments</a>
section
</li>
<li>HTML elements can be either in lowercase or in uppercase</li>
<li>Attribute names can be either in lowercase or in uppercase</li>
<li>Attribute values can be either quoted or not be quoted</li>
</ul>
</section>
<section>
<h2><a name="How_to_create_Javadoc_Check"></a>How to create Javadoc Check</h2>
<p>
Writing Javadoc Checks is similar to writing Java Checks, however you should extend
AbstractJavadocCheck<link> and use JavadoctokenTypes<link>.
</p>
<p>
To start implementing new Check create a new class and extend <a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html">
AbstractJavadocCheck</a>.
It has two abstract methods you should implement:
</p>
<ul>
<li>
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#getDefaultJavadocTokens--">
getDefaultJavadocTokens()</a> - return int array of
javadoc token types your Check is going to process.
The array should contain int constants from
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html">
JavadocTokenTypes</a> class. (There is also
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">
TokenTypes</a> class in Checkstyle. Make sure you use
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html">
JavadocTokenTypes</a> class in your Check, because the
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html">
TokenTypes</a> is used to describe standard Java
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailAST.html">
DetailAST</a> token type.)
</li>
<li>
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#visitJavadocToken-com.puppycrawl.tools.checkstyle.api.DetailNode-">
visitJavadocToken(DetailNode)</a> - it's a place you should put tree nodes processing.
The argument is Javadoc tree node of type you described before in
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#getDefaultJavadocTokens--">
getDefaultJavadocTokens()</a> method.
</li>
</ul>
</section>
<section>
<h2><a name="Difference_between_Java_Grammar_and_Javadoc_comments_Grammar"></a>Difference between Java Grammar and Javadoc comments Grammar</h2>
<p>
Our Java parser parses all multiline comments (C-style comments and Javadoc comments)
as block comments, whether they are Javadocs or not. Our Javadoc parser then transforms
these block comment ASTs into our Javadoc AST.
</p>
<p>
The difference is that Java grammar uses ANTLR v2, while Javadoc grammar uses ANTLR v4.
Because of that, these two grammars and their trees are not compatible.
Java AST consists of
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailAST.html">DetailAST</a>
objects, while Javadoc AST consists of
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailNode.html">DetailNode</a>
objects.
</p>
<p>
Our Java grammar ignores whitespace in most cases, since Java itself does not care about
whitespace; however, whitespace is critical in Javadocs. For this reason, our Javadoc AST
retains most whitespace that is present in the Javadoc.
(<a href="apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#WS">WS</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#NEWLINE">
NEWLINE</a>).
</p>
</section>
<section>
<h2><a name="Tool_to_print_Javadoc_tree_structure"></a>Tool to print Javadoc tree structure</h2>
<p>
Checkstyle can print a combined Java and Javadoc Abstract Syntax Tree. You need to run the
checkstyle jar file with the <b>-J</b> argument, providing a java file.
</p>
<p>For example, here is MyClass.java file:</p>
<div class="source">
<pre>
/**
* My <b>class</b>.
* @see AbstractClass
*/
public class MyClass {
}
</pre></div>
<p>Command:</p>
<div class="wrap-content">
<div class="source">
<pre>
java -jar checkstyle-X.XX-all.jar -J MyClass.java
</pre></div>
</div>
<p>Output:</p>
<div class="source">
<pre>
CLASS_DEF -> CLASS_DEF [5:0]
|--MODIFIERS -> MODIFIERS [5:0]
| |--BLOCK_COMMENT_BEGIN -> /* [1:0]
| | |--COMMENT_CONTENT -> *\n * My <b>class</b>.\n * @see AbstractClass\n [1:2]
| | | `--JAVADOC -> JAVADOC [1:3]
| | | |--NEWLINE -> \n [1:3]
| | | |--LEADING_ASTERISK -> * [2:0]
| | | |--TEXT -> My [2:2]
| | | |--HTML_ELEMENT -> HTML_ELEMENT [2:6]
| | | | `--HTML_TAG -> HTML_TAG [2:6]
| | | | |--HTML_ELEMENT_START -> HTML_ELEMENT_START [2:6]
| | | | | |--START -> < [2:6]
| | | | | |--HTML_TAG_NAME -> b [2:7]
| | | | | `--END -> > [2:8]
| | | | |--TEXT -> class [2:9]
| | | | `--HTML_ELEMENT_END -> HTML_ELEMENT_END [2:14]
| | | | |--START -> < [2:14]
| | | | |--SLASH -> / [2:15]
| | | | |--HTML_TAG_NAME -> b [2:16]
| | | | `--END -> > [2:17]
| | | |--TEXT -> . [2:18]
| | | |--NEWLINE -> \n [2:19]
| | | |--LEADING_ASTERISK -> * [3:0]
| | | |--WS -> [3:2]
| | | |--JAVADOC_TAG -> JAVADOC_TAG [3:3]
| | | | |--SEE_LITERAL -> @see [3:3]
| | | | |--WS -> [3:7]
| | | | |--REFERENCE -> REFERENCE [3:8]
| | | | | `--CLASS -> AbstractClass [3:8]
| | | | |--NEWLINE -> \n [3:21]
| | | | `--WS -> [4:0]
| | | `--EOF -> <EOF> [4:1]
| | `--BLOCK_COMMENT_END -> */ [4:1]
| `--LITERAL_PUBLIC -> public [5:0]
|--LITERAL_CLASS -> class [5:7]
|--IDENT -> MyClass [5:13]
`--OBJBLOCK -> OBJBLOCK [5:21]
|--LCURLY -> { [5:21]
`--RCURLY -> } [7:0]
</pre></div>
<p>
In most cases while developing Javadoc Checks, you need to only parse the tree of the exact
Javadoc comment. To do that just copy Javadoc comment to a separate file and remove
<b>/**</b> at the beginning and <b>*/</b> at the end. After that, run checkstyle with
<b>-j</b> argument.
</p>
<p>MyJavadocComment.javadoc file:</p>
<div class="source">
<pre>
* My <b>class</b>.
* @see AbstractClass
</pre></div>
<p>Command:</p>
<div class="wrap-content">
<div class="source">
<pre>
java -jar checkstyle-X.XX-all.jar \
    -j MyJavadocComment.javadoc
</pre></div>
</div>
<p>Output:</p>
<div class="source">
<pre>
JAVADOC -> JAVADOC [0:0]
|--LEADING_ASTERISK -> * [0:0]
|--TEXT -> My [0:2]
|--HTML_ELEMENT -> HTML_ELEMENT [0:6]
| `--HTML_TAG -> HTML_TAG [0:6]
| |--HTML_ELEMENT_START -> HTML_ELEMENT_START [0:6]
| | |--START -> < [0:6]
| | |--HTML_TAG_NAME -> b [0:7]
| | `--END -> > [0:8]
| |--TEXT -> class [0:9]
| `--HTML_ELEMENT_END -> HTML_ELEMENT_END [0:14]
| |--START -> < [0:14]
| |--SLASH -> / [0:15]
| |--HTML_TAG_NAME -> b [0:16]
| `--END -> > [0:17]
|--TEXT -> . [0:18]
|--NEWLINE -> \n [0:19]
|--LEADING_ASTERISK -> * [1:0]
|--WS -> [1:2]
|--JAVADOC_TAG -> JAVADOC_TAG [1:3]
| |--SEE_LITERAL -> @see [1:3]
| |--WS -> [1:7]
| `--REFERENCE -> REFERENCE [1:8]
| `--CLASS -> AbstractClass [1:8]
`--EOF -> <EOF> [1:21]
</pre></div>
</section>
<section>
<h2><a name="Access_Java_AST_from_Javadoc_Check"></a>Access Java AST from Javadoc Check</h2>
<p>
As you already know the Javadoc AST is a result of parsing a block comment.
There is a method to get the original block comment from a Javadoc Check.
You may need this block comment to check its position in the
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailAST.html">DetailAST</a> tree.
</p>
<p>
For example, to write a JavadocCheck that verifies @param tags in the Javadoc comment of
a method definition, you also need all of the method's parameter names. To get a method
definition AST you should access the
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailAST.html">DetailAST</a> tree
from a javadoc Check. For this purpose use the
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#getBlockCommentAst--">
getBlockCommentAst()</a>
method that returns a
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/DetailAST.html">DetailAST</a> node.
</p>
<p>
Example:
</p>
<div class="source">
<pre>
class MyCheck extends AbstractJavadocCheck {
@Override
public int[] getDefaultJavadocTokens() {
return new int[]{JavadocTokenTypes.PARAMETER_NAME};
}
@Override
public void visitJavadocToken(DetailNode paramNameNode) {
String javadocParamName = paramNameNode.getText();
DetailAST blockCommentAst = getBlockCommentAst();
if (BlockCommentPosition.isOnMethod(blockCommentAst)) {
DetailAST methodDef = blockCommentAst.getParent();
DetailAST methodParam = findMethodParameter(methodDef);
String methodParamName = methodParam.getText();
if (!javadocParamName.equals(methodParamName)) {
log(methodParam, "params.dont.match");
}
}
}
}
</pre></div>
</section>
<section>
<h2><a name="HTML_Code_In_Javadoc_Comments"></a>HTML Code In Javadoc Comments</h2>
<p>
Checkstyle supports HTML4 tags in Javadoc comments:
<a class="externalLink" href="https://www.w3.org/TR/html4/index/elements.html">
all HTML4 elements
</a>.
</p>
<p>
HTML4 is designed to have a list of elements whose end tag is optional(omittable) and a
list of <a class="externalLink" href="https://www.w3.org/TR/html/syntax.html#void-elements">void elements</a>
(also known as <a class="externalLink" href="https://www.w3schools.com/html/html_elements.asp">empty html
tags</a>, for example <a class="externalLink" href="https://www.w3.org/TR/html4/struct/text.html#edef-BR">BR
tag</a>).
</p>
<p>
HTML4 elements whose end tag is optional (omittable): <P>, <LI>, <TR>,
<TD>, <TH>, <BODY>, <COLGROUP>, <DD>, <DT>,
<HEAD>, <HTML>, <OPTION>, <TBODY>, <THEAD>, <TFOOT>.
</p>
<p>
Void HTML4 elements: <AREA>, <BASE>, <BASEFONT>, <BR>, <COL>,
<FRAME>, <HR>, <IMG>, <INPUT>, <ISINDEX>, <LINK>,
<META>, <PARAM>.
</p>
<p>
Checkstyle does not support HTML5 tags with optional end tags. See
<a class="externalLink" href="https://github.com/checkstyle/checkstyle/issues/3332">issue</a> for details.
</p>
<p>
If the parser consumes an unknown tag (for example HTML5 tag)
it doesn't fail, but parses this tag as a
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#HTML_TAG">
HTML_TAG</a>
Javadoc token type.
Always follow <a href="#Tight-HTML_rules">Tight-HTML rules</a> to make the
Checkstyle javadoc parser create nested ASTs, even though tags are unknown.
</p>
<div class="source">
<pre>
<audio><source src="horse.ogg" type="audio/ogg"/></audio>
</pre></div>
<div class="source">
<pre>
JAVADOC -> JAVADOC [0:0]
|--HTML_ELEMENT -> HTML_ELEMENT [0:0]
| `--HTML_TAG -> HTML_TAG [0:0]
| |--HTML_ELEMENT_START -> HTML_ELEMENT_START [0:0]
| | |--START -> < [0:0]
| | |--HTML_TAG_NAME -> audio [0:1]
| | `--END -> > [0:6]
| |--HTML_ELEMENT -> HTML_ELEMENT [0:7]
| | `--SINGLETON_ELEMENT -> SINGLETON_ELEMENT [0:7]
| | `--EMPTY_TAG -> EMPTY_TAG [0:7]
| | |--START -> < [0:7]
| | |--HTML_TAG_NAME -> source [0:8]
| | |--WS -> [0:14]
| | |--ATTRIBUTE -> ATTRIBUTE [0:15]
| | | |--HTML_TAG_NAME -> src [0:15]
| | | |--EQUALS -> = [0:18]
| | | `--ATTR_VALUE -> "horse.ogg" [0:19]
| | |--WS -> [0:31]
| | |--ATTRIBUTE -> ATTRIBUTE [0:32]
| | | |--HTML_TAG_NAME -> type [0:32]
| | | |--EQUALS -> = [0:36]
| | | `--ATTR_VALUE -> "audio/ogg" [0:37]
| | `--SLASH_END -> /> [0:49]
| `--HTML_ELEMENT_END -> HTML_ELEMENT_END [0:51]
| |--START -> < [0:51]
| |--SLASH -> / [0:52]
| |--HTML_TAG_NAME -> audio [0:53]
| `--END -> > [0:58]
`--EOF -> <EOF> [0:59]
</pre></div>
<p>
This is an example of parsing an unknown tag that doesn't have a matching end tag (for
example, HTML5 tag <audio>): <br />
Input:
</p>
<div class="source">
<pre><audio>test</pre></div>
Output:
<div class="wrap-content">
<div class="source">
<pre>
[ERROR:0] Javadoc comment at column 1 has parse error. Missed HTML close tag 'audio'.
Sometimes it means that close tag missed for one of previous tags.
</pre></div>
</div>
<p>
As you see, the Javadoc parser prints an error and doesn't build the AST if an unknown
HTML tag doesn't have a matching end tag. If that's the case, please create an issue for
Checkstyle to consider adding support for this tag.
</p>
<p>
There are also HTML tags that are marked as "Not supported in HTML5"
(<a class="externalLink" href="https://www.w3schools.com/tags/default.asp">HTML Element Reference</a>).
Checkstyle Javadoc parser can parse those tags too if they are written in
<a href="#Tight-HTML_rules">Tight-HTML</a>.
<br />
Example:
<br />
Input:
</p>
<div class="source">
<pre>
<acronym title="as soon as possible">ASAP</acronym>
</pre></div>
<br />
Output:
<div class="source">
<pre>
JAVADOC -> JAVADOC [0:0]
|--HTML_ELEMENT -> HTML_ELEMENT [0:0]
| `--HTML_TAG -> HTML_TAG [0:0]
| |--HTML_ELEMENT_START -> HTML_ELEMENT_START [0:0]
| | |--START -> < [0:0]
| | |--HTML_TAG_NAME -> acronym [0:1]
| | |--WS -> [0:8]
| | |--ATTRIBUTE -> ATTRIBUTE [0:9]
| | | |--HTML_TAG_NAME -> title [0:9]
| | | |--EQUALS -> = [0:14]
| | | `--ATTR_VALUE -> "as soon as possible" [0:15]
| | `--END -> > [0:37]
| |--TEXT -> ASAP [0:38]
| `--HTML_ELEMENT_END -> HTML_ELEMENT_END [0:42]
| |--START -> < [0:42]
| |--SLASH -> / [0:43]
| |--HTML_TAG_NAME -> acronym [0:44]
| `--END -> > [0:51]
`--EOF -> <EOF> [0:52]
</pre></div>
<p>More examples:</p>
<div class="wrapper">
<table border="0" class="bodyTable" style="table-layout: fixed;">
<tr class="a">
<td align="left">
1) Unclosed paragraph HTML tag. As you see in the tree, the content of the paragraph
tag is not nested within this tag.
That is because HTML tags are not closed by pair tags </p>, and Checkstyle
requires <a href="#Tight-HTML_rules">Tight-HTML</a> code to predictably parse
Javadoc comments.
</td>
<td>
2) Here is a correct example with open and closed HTML tags.
</td>
</tr>
<tr class="b">
<td align="left">
<div class="source">
<pre>
<p> First
<p> Second
</pre></div>
</td>
<td>
<div class="source">
<pre>
<p> First </p>
<p> Second </p>
</pre></div>
</td>
</tr>
<tr class="a">
<td align="left">
<div class="source">
<pre>
JAVADOC -> JAVADOC [0:0]
|--HTML_ELEMENT -> HTML_ELEMENT [0:0]
| `--P_TAG_START -> P_TAG_START [0:0]
| |--START -> < [0:0]
| |--P_HTML_TAG_NAME -> p [0:1]
| `--END -> > [0:2]
|--TEXT -> First [0:3]
|--NEWLINE -> \n [0:9]
|--HTML_ELEMENT -> HTML_ELEMENT [1:0]
| `--P_TAG_START -> P_TAG_START [1:0]
| |--START -> < [1:0]
| |--P_HTML_TAG_NAME -> p [1:1]
| `--END -> > [1:2]
|--TEXT -> Second [1:3]
`--EOF -> <EOF> [1:10]
</pre></div>
</td>
<td>
<div class="source">
<pre>
JAVADOC -> JAVADOC [0:0]
|--HTML_ELEMENT -> HTML_ELEMENT [0:0]
| `--PARAGRAPH -> PARAGRAPH [0:0]
| |--P_TAG_START -> P_TAG_START [0:0]
| | |--START -> < [0:0]
| | |--P_HTML_TAG_NAME -> p [0:1]
| | `--END -> > [0:2]
| |--TEXT -> First [0:3]
| `--P_TAG_END -> P_TAG_END [0:10]
| |--START -> < [0:10]
| |--SLASH -> / [0:11]
| |--P_HTML_TAG_NAME -> p [0:12]
| `--END -> > [0:13]
|--NEWLINE -> \n [0:14]
|--HTML_ELEMENT -> HTML_ELEMENT [1:0]
| `--PARAGRAPH -> PARAGRAPH [1:0]
| |--P_TAG_START -> P_TAG_START [1:0]
| | |--START -> < [1:0]
| | |--P_HTML_TAG_NAME -> p [1:1]
| | `--END -> > [1:2]
| |--TEXT -> Second [1:3]
| `--P_TAG_END -> P_TAG_END [1:11]
| |--START -> < [1:11]
| |--SLASH -> / [1:12]
| |--P_HTML_TAG_NAME -> p [1:13]
| `--END -> > [1:14]
`--EOF -> <EOF> [1:15]
</pre></div>
</td>
</tr>
</table>
</div>
<p>
Checks can also be configured to log violations upon encountering non-tight HTML tags.
The <code>violateExecutionOnNonTightHtml</code> property can be used for this purpose in the
checks that support it. A custom check needs to extend <code>AbstractJavadocCheck</code> to
have this functionality readily available. Do note that a check which has this property set
to true, will log violations only for the first not-tight HTML tag found. To allow a check
to skip processing of javadocs with non-tight HTML, the
<code>acceptJavadocWithNonTightHtml</code> method in class <code>AbstractJavadocCheck</code> can be
overridden in the check. The following example illustrates how to use this property.
</p>
<p>
Input:
</p>
<div class="source">
<pre>
/**
* <body>
* <p> This class is only meant for testing. </p>
* <p> This p tag is not closed. It is non-tight. Will lead to violations if
* <tt>violateExecutionOnNonTightHtml</tt> is set to true for the check.
* <li>tight li tag <p>non-tight p tag, but only the 1st non-tight tag is logged in violation</li>
* </body>
*/
public class Test {
/**
<p><p>
paraception. Will result in a violation from the <tt>JavadocParagraph</tt> check due to
redundant tags.
</p></p>
*/
private int field1;
/**<tr> Zero </p> nesting despite `tr` is closed </tr>*/
private int field2;
/**
* <p> this paragraph is closed and would be nested in javadoc tree </p>
* <li> this list has an <p> unclosed para, but still the list would get nested </li>
*/
private int field3;
/**
* <li> Complete <p> nesting </p> </li>
*/
private int field4;
}
</pre></div>
<p>
Output with <code>violateExecutionOnNonTightHtml</code> set to false:
</p>
<div class="wrapper">
<table border="0" class="bodyTable">
<tr class="a">
<td align="left">
<div class="source">
<pre>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocParagraph">
<property name="violateExecutionOnNonTightHtml" value="false"/>
</module>
</module>
</module>
</pre></div>
</td>
<td>
<div class="source">
<pre>
Starting audit...
[ERROR] Test.java:11: Redundant <p> tag. [JavadocParagraph]
Audit done.
Checkstyle ends with 1 errors.
</pre></div>
</td>
</tr>
</table>
</div>
<p>
Output with <code>violateExecutionOnNonTightHtml</code> set to true:
</p>
<div class="wrapper">
<table border="0" class="bodyTable">
<tr class="a">
<td align="left">
<div class="source">
<pre>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocParagraph">
<property name="violateExecutionOnNonTightHtml" value="true"/>
</module>
</module>
</module>
</pre></div>
</td>
<td>
<div class="source">
<pre>
Starting audit...
[ERROR] Test.java:4: Unclosed HTML tag found: p [JavadocParagraph]
[ERROR] Test.java:11: Redundant <p> tag. [JavadocParagraph]
[ERROR] Test.java:11: Unclosed HTML tag found: p [JavadocParagraph]
[ERROR] Test.java:18: Unclosed HTML tag found: tr [JavadocParagraph]
[ERROR] Test.java:23: Unclosed HTML tag found: p [JavadocParagraph]
Audit done.
Checkstyle ends with 5 errors.
</pre></div>
</td>
</tr>
</table>
</div>
</section>
<section>
<h2><a name="Checkstyle_SDK_GUI"></a>Checkstyle SDK GUI</h2>
<p>