forked from openjdk/jdk
/
Checker.java
1204 lines (1069 loc) · 44.4 KB
/
Checker.java
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
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclint;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;
import com.sun.source.doctree.AttributeTree;
import com.sun.source.doctree.AuthorTree;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocRootTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.EndElementTree;
import com.sun.source.doctree.EntityTree;
import com.sun.source.doctree.ErroneousTree;
import com.sun.source.doctree.IdentifierTree;
import com.sun.source.doctree.IndexTree;
import com.sun.source.doctree.InheritDocTree;
import com.sun.source.doctree.LinkTree;
import com.sun.source.doctree.LiteralTree;
import com.sun.source.doctree.ParamTree;
import com.sun.source.doctree.ProvidesTree;
import com.sun.source.doctree.ReferenceTree;
import com.sun.source.doctree.ReturnTree;
import com.sun.source.doctree.SerialDataTree;
import com.sun.source.doctree.SerialFieldTree;
import com.sun.source.doctree.SinceTree;
import com.sun.source.doctree.StartElementTree;
import com.sun.source.doctree.SummaryTree;
import com.sun.source.doctree.SystemPropertyTree;
import com.sun.source.doctree.TextTree;
import com.sun.source.doctree.ThrowsTree;
import com.sun.source.doctree.UnknownBlockTagTree;
import com.sun.source.doctree.UnknownInlineTagTree;
import com.sun.source.doctree.UsesTree;
import com.sun.source.doctree.ValueTree;
import com.sun.source.doctree.VersionTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTreePathScanner;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.DocPretty;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import jdk.javadoc.internal.doclint.HtmlTag.AttrKind;
import jdk.javadoc.internal.doclint.HtmlTag.ElemKind;
import static jdk.javadoc.internal.doclint.Messages.Group.*;
/**
* Validate a doc comment.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own
* risk. This code and its internal interfaces are subject to change
* or deletion without notice.</b></p>
*/
public class Checker extends DocTreePathScanner<Void, Void> {
final Env env;
Set<Element> foundParams = new HashSet<>();
Set<TypeMirror> foundThrows = new HashSet<>();
Map<Element, Set<String>> foundAnchors = new HashMap<>();
boolean foundInheritDoc = false;
boolean foundReturn = false;
boolean hasNonWhitespaceText = false;
public enum Flag {
TABLE_HAS_CAPTION,
TABLE_IS_PRESENTATION,
HAS_ELEMENT,
HAS_HEADING,
HAS_INLINE_TAG,
HAS_TEXT,
REPORTED_BAD_INLINE
}
static class TagStackItem {
final DocTree tree; // typically, but not always, StartElementTree
final HtmlTag tag;
final Set<HtmlTag.Attr> attrs;
final Set<Flag> flags;
TagStackItem(DocTree tree, HtmlTag tag) {
this.tree = tree;
this.tag = tag;
attrs = EnumSet.noneOf(HtmlTag.Attr.class);
flags = EnumSet.noneOf(Flag.class);
}
@Override
public String toString() {
return String.valueOf(tag);
}
}
private final Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well
private HtmlTag currHeadingTag;
private int implicitHeadingRank;
private boolean inIndex;
private boolean inLink;
private boolean inSummary;
// <editor-fold defaultstate="collapsed" desc="Top level">
Checker(Env env) {
this.env = Assert.checkNonNull(env);
tagStack = new LinkedList<>();
}
public Void scan(DocCommentTree tree, TreePath p) {
env.initTypes();
env.setCurrent(p, tree);
boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
JavaFileObject fo = p.getCompilationUnit().getSourceFile();
if (p.getLeaf().getKind() == Tree.Kind.PACKAGE) {
// If p points to a package, the implied declaration is the
// package declaration (if any) for the compilation unit.
// Handle this case specially, because doc comments are only
// expected in package-info files.
boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
if (tree == null) {
if (isPkgInfo)
reportMissing("dc.missing.comment");
return null;
} else {
if (!isPkgInfo)
reportReference("dc.unexpected.comment");
}
} else if (tree != null && fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) {
// a package.html file with a DocCommentTree
if (tree.getFullBody().isEmpty()) {
reportMissing("dc.missing.comment");
return null;
}
} else {
if (tree == null) {
if (!isSynthetic() && !isOverridingMethod)
reportMissing("dc.missing.comment");
return null;
}
}
tagStack.clear();
currHeadingTag = null;
foundParams.clear();
foundThrows.clear();
foundInheritDoc = false;
foundReturn = false;
hasNonWhitespaceText = false;
switch (p.getLeaf().getKind()) {
// the following are for declarations that have their own top-level page,
// and so the doc comment comes after the <h1> page title.
case MODULE:
case PACKAGE:
case CLASS:
case INTERFACE:
case ENUM:
case ANNOTATION_TYPE:
case RECORD:
implicitHeadingRank = 1;
break;
// this is for html files
// ... if it is a legacy package.html, the doc comment comes after the <h1> page title
// ... otherwise, (e.g. overview file and doc-files/*.html files) no additional headings are inserted
case COMPILATION_UNIT:
implicitHeadingRank = fo.isNameCompatible("package", JavaFileObject.Kind.HTML) ? 1 : 0;
break;
// the following are for member declarations, which appear in the page
// for the enclosing type, and so appear after the <h2> "Members"
// aggregate heading and the specific <h3> "Member signature" heading.
case METHOD:
case VARIABLE:
implicitHeadingRank = 3;
break;
default:
Assert.error("unexpected tree kind: " + p.getLeaf().getKind() + " " + fo);
}
scan(new DocTreePath(p, tree), null);
if (!isOverridingMethod) {
switch (env.currElement.getKind()) {
case METHOD:
case CONSTRUCTOR: {
ExecutableElement ee = (ExecutableElement) env.currElement;
checkParamsDocumented(ee.getTypeParameters());
checkParamsDocumented(ee.getParameters());
switch (ee.getReturnType().getKind()) {
case VOID:
case NONE:
break;
default:
if (!foundReturn
&& !foundInheritDoc
&& !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
reportMissing("dc.missing.return");
}
}
checkThrowsDocumented(ee.getThrownTypes());
}
}
}
return null;
}
private void reportMissing(String code, Object... args) {
env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args);
}
private void reportReference(String code, Object... args) {
env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitDocComment(DocCommentTree tree, Void ignore) {
scan(tree.getFirstSentence(), ignore);
scan(tree.getBody(), ignore);
checkTagStack();
for (DocTree blockTag : tree.getBlockTags()) {
tagStack.clear();
scan(blockTag, ignore);
checkTagStack();
}
return null;
}
private void checkTagStack() {
for (TagStackItem tsi: tagStack) {
warnIfEmpty(tsi, null);
if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT
&& tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) {
StartElementTree t = (StartElementTree) tsi.tree;
env.messages.error(HTML, t, "dc.tag.not.closed", t.getName());
}
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Text and entities.">
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitText(TextTree tree, Void ignore) {
hasNonWhitespaceText = hasNonWhitespace(tree);
if (hasNonWhitespaceText) {
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
}
return null;
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitEntity(EntityTree tree, Void ignore) {
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
String s = env.trees.getCharacters(tree);
if (s == null) {
env.messages.error(HTML, tree, "dc.entity.invalid", tree.getName());
}
return null;
}
void checkAllowsText(DocTree tree) {
TagStackItem top = tagStack.peek();
if (top != null
&& top.tree.getKind() == DocTree.Kind.START_ELEMENT
&& !top.tag.acceptsText()) {
if (top.flags.add(Flag.REPORTED_BAD_INLINE)) {
env.messages.error(HTML, tree, "dc.text.not.allowed",
((StartElementTree) top.tree).getName());
}
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="HTML elements">
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitStartElement(StartElementTree tree, Void ignore) {
final Name treeName = tree.getName();
final HtmlTag t = HtmlTag.get(treeName);
if (t == null) {
env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
} else if (t.elemKind == ElemKind.HTML4) {
env.messages.error(HTML, tree, "dc.tag.not.supported.html5", treeName);
} else {
boolean done = false;
for (TagStackItem tsi: tagStack) {
if (tsi.tag.accepts(t)) {
while (tagStack.peek() != tsi) {
warnIfEmpty(tagStack.peek(), null);
tagStack.pop();
}
done = true;
break;
} else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
done = true;
break;
}
}
if (!done && HtmlTag.BODY.accepts(t)) {
while (!tagStack.isEmpty()) {
warnIfEmpty(tagStack.peek(), null);
tagStack.pop();
}
}
markEnclosingTag(Flag.HAS_ELEMENT);
checkStructure(tree, t);
// tag specific checks
switch (t) {
// check for out of sequence headings, such as <h1>...</h1> <h3>...</h3>
case H1: case H2: case H3: case H4: case H5: case H6:
checkHeading(tree, t);
break;
}
if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
for (TagStackItem i: tagStack) {
if (t == i.tag) {
env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
break;
}
}
}
}
// check for self closing tags, such as <a id="name"/>
if (tree.isSelfClosing() && !isSelfClosingAllowed(t)) {
env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
}
try {
TagStackItem parent = tagStack.peek();
TagStackItem top = new TagStackItem(tree, t);
tagStack.push(top);
super.visitStartElement(tree, ignore);
// handle attributes that may or may not have been found in start element
if (t != null) {
switch (t) {
case CAPTION:
if (parent != null && parent.tag == HtmlTag.TABLE)
parent.flags.add(Flag.TABLE_HAS_CAPTION);
break;
case H1: case H2: case H3: case H4: case H5: case H6:
if (parent != null && (parent.tag == HtmlTag.SECTION || parent.tag == HtmlTag.ARTICLE)) {
parent.flags.add(Flag.HAS_HEADING);
}
break;
case IMG:
if (!top.attrs.contains(HtmlTag.Attr.ALT))
env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
break;
}
}
return null;
} finally {
if (t == null || t.endKind == HtmlTag.EndKind.NONE)
tagStack.pop();
}
}
// so-called "self-closing" tags are only permitted in HTML 5, for void elements
// https://html.spec.whatwg.org/multipage/syntax.html#start-tags
private boolean isSelfClosingAllowed(HtmlTag tag) {
return tag.endKind == HtmlTag.EndKind.NONE;
}
private void checkStructure(StartElementTree tree, HtmlTag t) {
Name treeName = tree.getName();
TagStackItem top = tagStack.peek();
switch (t.blockType) {
case BLOCK:
if (top == null || top.tag.accepts(t))
return;
switch (top.tree.getKind()) {
case START_ELEMENT: {
if (top.tag.blockType == HtmlTag.BlockType.INLINE) {
Name name = ((StartElementTree) top.tree).getName();
env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element",
treeName, name);
return;
}
}
break;
case LINK:
case LINK_PLAIN: {
String name = top.tree.getKind().tagName;
env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag",
treeName, name);
return;
}
}
break;
case INLINE:
if (top == null || top.tag.accepts(t))
return;
break;
case LIST_ITEM:
case TABLE_ITEM:
if (top != null) {
// reset this flag so subsequent bad inline content gets reported
top.flags.remove(Flag.REPORTED_BAD_INLINE);
if (top.tag.accepts(t))
return;
}
break;
case OTHER:
switch (t) {
case SCRIPT:
// <script> may or may not be allowed, depending on --allow-script-in-comments
// but we allow it here, and rely on a separate scanner to detect all uses
// of JavaScript, including <script> tags, and use in attributes, etc.
break;
default:
env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName);
}
return;
}
env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName);
}
private void checkHeading(StartElementTree tree, HtmlTag tag) {
// verify the new tag
if (getHeadingRank(tag) > getHeadingRank(currHeadingTag) + 1) {
if (currHeadingTag == null) {
env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.1",
tag, implicitHeadingRank);
} else {
env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.2",
tag, currHeadingTag);
}
} else if (getHeadingRank(tag) <= implicitHeadingRank) {
env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.3",
tag, implicitHeadingRank);
}
currHeadingTag = tag;
}
private int getHeadingRank(HtmlTag tag) {
if (tag == null)
return implicitHeadingRank;
switch (tag) {
case H1: return 1;
case H2: return 2;
case H3: return 3;
case H4: return 4;
case H5: return 5;
case H6: return 6;
default: throw new IllegalArgumentException();
}
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitEndElement(EndElementTree tree, Void ignore) {
final Name treeName = tree.getName();
final HtmlTag t = HtmlTag.get(treeName);
if (t == null) {
env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
} else if (t.endKind == HtmlTag.EndKind.NONE) {
env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName);
} else {
boolean done = false;
while (!tagStack.isEmpty()) {
TagStackItem top = tagStack.peek();
if (t == top.tag) {
switch (t) {
case TABLE:
if (!top.flags.contains(Flag.TABLE_IS_PRESENTATION)
&& !top.attrs.contains(HtmlTag.Attr.SUMMARY)
&& !top.flags.contains(Flag.TABLE_HAS_CAPTION)) {
env.messages.error(ACCESSIBILITY, tree,
"dc.no.summary.or.caption.for.table");
}
break;
case SECTION:
case ARTICLE:
if (!top.flags.contains(Flag.HAS_HEADING)) {
env.messages.error(HTML, tree, "dc.tag.requires.heading", treeName);
}
break;
}
warnIfEmpty(top, tree);
tagStack.pop();
done = true;
break;
} else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) {
warnIfEmpty(top, null);
tagStack.pop();
} else {
boolean found = false;
for (TagStackItem si: tagStack) {
if (si.tag == t) {
found = true;
break;
}
}
if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) {
env.messages.error(HTML, top.tree, "dc.tag.start.unmatched",
((StartElementTree) top.tree).getName());
tagStack.pop();
} else {
env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
done = true;
break;
}
}
}
if (!done && tagStack.isEmpty()) {
env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
}
}
return super.visitEndElement(tree, ignore);
}
void warnIfEmpty(TagStackItem tsi, DocTree endTree) {
if (tsi.tag != null && tsi.tree instanceof StartElementTree startElementTree) {
if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
&& !tsi.flags.contains(Flag.HAS_TEXT)
&& !tsi.flags.contains(Flag.HAS_ELEMENT)
&& !tsi.flags.contains(Flag.HAS_INLINE_TAG)
&& !(tsi.tag.elemKind == ElemKind.HTML4)) {
DocTree tree = (endTree != null) ? endTree : startElementTree;
Name treeName = startElementTree.getName();
env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
}
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="HTML attributes">
@Override @DefinedBy(Api.COMPILER_TREE) @SuppressWarnings("fallthrough")
public Void visitAttribute(AttributeTree tree, Void ignore) {
HtmlTag currTag = tagStack.peek().tag;
if (currTag != null && currTag.elemKind != ElemKind.HTML4) {
Name name = tree.getName();
HtmlTag.Attr attr = currTag.getAttr(name);
if (attr != null) {
boolean first = tagStack.peek().attrs.add(attr);
if (!first)
env.messages.error(HTML, tree, "dc.attr.repeated", name);
}
// for now, doclint allows all attribute names beginning with "on" as event handler names,
// without checking the validity or applicability of the name
if (!name.toString().startsWith("on")) {
AttrKind k = currTag.getAttrKind(name);
switch (k) {
case OK:
break;
case OBSOLETE:
env.messages.warning(HTML, tree, "dc.attr.obsolete", name);
break;
case HTML4:
env.messages.error(HTML, tree, "dc.attr.not.supported.html5", name);
break;
case INVALID:
env.messages.error(HTML, tree, "dc.attr.unknown", name);
break;
}
}
if (attr != null) {
switch (attr) {
case ID:
String value = getAttrValue(tree);
if (value == null) {
env.messages.error(HTML, tree, "dc.anchor.value.missing");
} else {
if (!validId.matcher(value).matches()) {
env.messages.error(HTML, tree, "dc.invalid.anchor", value);
}
if (!checkAnchor(value)) {
env.messages.error(HTML, tree, "dc.anchor.already.defined", value);
}
}
break;
case HREF:
if (currTag == HtmlTag.A) {
String v = getAttrValue(tree);
if (v == null || v.isEmpty()) {
env.messages.error(HTML, tree, "dc.attr.lacks.value");
} else {
Matcher m = docRoot.matcher(v);
if (m.matches()) {
String rest = m.group(2);
if (!rest.isEmpty())
checkURI(tree, rest);
} else {
checkURI(tree, v);
}
}
}
break;
case VALUE:
if (currTag == HtmlTag.LI) {
String v = getAttrValue(tree);
if (v == null || v.isEmpty()) {
env.messages.error(HTML, tree, "dc.attr.lacks.value");
} else if (!validNumber.matcher(v).matches()) {
env.messages.error(HTML, tree, "dc.attr.not.number");
}
}
break;
case BORDER:
if (currTag == HtmlTag.TABLE) {
String v = getAttrValue(tree);
try {
if (v == null || (!v.isEmpty() && Integer.parseInt(v) != 1)) {
env.messages.error(HTML, tree, "dc.attr.table.border.not.valid", attr);
}
} catch (NumberFormatException ex) {
env.messages.error(HTML, tree, "dc.attr.table.border.not.number", attr);
}
} else if (currTag == HtmlTag.IMG) {
String v = getAttrValue(tree);
try {
if (v == null || (!v.isEmpty() && Integer.parseInt(v) != 0)) {
env.messages.error(HTML, tree, "dc.attr.img.border.not.valid", attr);
}
} catch (NumberFormatException ex) {
env.messages.error(HTML, tree, "dc.attr.img.border.not.number", attr);
}
}
break;
case ROLE:
if (currTag == HtmlTag.TABLE) {
String v = getAttrValue(tree);
if (Objects.equals(v, "presentation")) {
tagStack.peek().flags.add(Flag.TABLE_IS_PRESENTATION);
}
}
break;
}
}
}
// TODO: basic check on value
return null;
}
private boolean checkAnchor(String name) {
Element e = getEnclosingPackageOrClass(env.currElement);
if (e == null)
return true;
Set<String> set = foundAnchors.get(e);
if (set == null)
foundAnchors.put(e, set = new HashSet<>());
return set.add(name);
}
private Element getEnclosingPackageOrClass(Element e) {
while (e != null) {
switch (e.getKind()) {
case CLASS:
case ENUM:
case INTERFACE:
case PACKAGE:
return e;
default:
e = e.getEnclosingElement();
}
}
return e;
}
// https://html.spec.whatwg.org/#the-id-attribute
private static final Pattern validId = Pattern.compile("[^\\s]+");
private static final Pattern validNumber = Pattern.compile("-?[0-9]+");
// pattern to remove leading {@docRoot}/?
private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)");
private String getAttrValue(AttributeTree tree) {
if (tree.getValue() == null)
return null;
StringWriter sw = new StringWriter();
try {
new DocPretty(sw).print(tree.getValue());
} catch (IOException e) {
// cannot happen
}
// ignore potential use of entities for now
return sw.toString();
}
private void checkURI(AttributeTree tree, String uri) {
// allow URIs beginning with javascript:, which would otherwise be rejected by the URI API.
if (uri.startsWith("javascript:"))
return;
try {
URI u = new URI(uri);
} catch (URISyntaxException e) {
env.messages.error(HTML, tree, "dc.invalid.uri", uri);
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="javadoc tags">
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitAuthor(AuthorTree tree, Void ignore) {
warnIfEmpty(tree, tree.getName());
return super.visitAuthor(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitDocRoot(DocRootTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
return super.visitDocRoot(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitIndex(IndexTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
if (inIndex) {
env.messages.warning(HTML, tree, "dc.tag.nested.tag", "@" + tree.getTagName());
}
for (TagStackItem tsi : tagStack) {
if (tsi.tag == HtmlTag.A) {
env.messages.warning(HTML, tree, "dc.tag.a.within.a",
"{@" + tree.getTagName() + "}");
break;
}
}
boolean prevInIndex = inIndex;
try {
inIndex = true;
return super.visitIndex(tree, ignore);
} finally {
inIndex = prevInIndex;
}
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitInheritDoc(InheritDocTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
// TODO: verify on overridden method
foundInheritDoc = true;
return super.visitInheritDoc(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitLink(LinkTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
if (inLink) {
env.messages.warning(HTML, tree, "dc.tag.nested.tag", "@" + tree.getTagName());
}
boolean prevInLink = inLink;
// simulate inline context on tag stack
HtmlTag t = (tree.getKind() == DocTree.Kind.LINK)
? HtmlTag.CODE : HtmlTag.SPAN;
tagStack.push(new TagStackItem(tree, t));
try {
inLink = true;
return super.visitLink(tree, ignore);
} finally {
tagStack.pop();
inLink = prevInLink;
}
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitLiteral(LiteralTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
if (tree.getKind() == DocTree.Kind.CODE) {
for (TagStackItem tsi: tagStack) {
if (tsi.tag == HtmlTag.CODE) {
env.messages.warning(HTML, tree, "dc.tag.code.within.code");
break;
}
}
}
return super.visitLiteral(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
boolean typaram = tree.isTypeParameter();
IdentifierTree nameTree = tree.getName();
Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;
if (paramElement == null) {
switch (env.currElement.getKind()) {
case CLASS: case INTERFACE: {
if (!typaram) {
env.messages.error(REFERENCE, tree, "dc.invalid.param");
break;
}
}
case METHOD: case CONSTRUCTOR: {
env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
break;
}
default:
env.messages.error(REFERENCE, tree, "dc.invalid.param");
break;
}
} else {
boolean unique = foundParams.add(paramElement);
if (!unique) {
env.messages.warning(REFERENCE, tree, "dc.exists.param", nameTree);
}
}
warnIfEmpty(tree, tree.getDescription());
return super.visitParam(tree, ignore);
}
private void checkParamsDocumented(List<? extends Element> list) {
if (foundInheritDoc)
return;
for (Element e: list) {
if (!foundParams.contains(e)) {
CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER)
? "<" + e.getSimpleName() + ">"
: e.getSimpleName();
reportMissing("dc.missing.param", paramName);
}
}
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitProvides(ProvidesTree tree, Void ignore) {
Element e = env.trees.getElement(env.currPath);
if (e.getKind() != ElementKind.MODULE) {
env.messages.error(REFERENCE, tree, "dc.invalid.provides");
}
ReferenceTree serviceType = tree.getServiceType();
Element se = env.trees.getElement(new DocTreePath(getCurrentPath(), serviceType));
if (se == null) {
env.messages.error(REFERENCE, tree, "dc.service.not.found");
}
return super.visitProvides(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitReference(ReferenceTree tree, Void ignore) {
Element e = env.trees.getElement(getCurrentPath());
if (e == null)
env.messages.error(REFERENCE, tree, "dc.ref.not.found");
return super.visitReference(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitReturn(ReturnTree tree, Void ignore) {
if (foundReturn) {
env.messages.warning(REFERENCE, tree, "dc.exists.return");
}
if (tree.isInline()) {
DocCommentTree dct = getCurrentPath().getDocComment();
if (tree != dct.getFirstSentence().get(0)) {
env.messages.warning(REFERENCE, tree, "dc.return.not.first");
}
}
Element e = env.trees.getElement(env.currPath);
if (e.getKind() != ElementKind.METHOD
|| ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID)
env.messages.error(REFERENCE, tree, "dc.invalid.return");
foundReturn = true;
warnIfEmpty(tree, tree.getDescription());
return super.visitReturn(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitSerialData(SerialDataTree tree, Void ignore) {
warnIfEmpty(tree, tree.getDescription());
return super.visitSerialData(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitSerialField(SerialFieldTree tree, Void ignore) {
warnIfEmpty(tree, tree.getDescription());
return super.visitSerialField(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitSince(SinceTree tree, Void ignore) {
warnIfEmpty(tree, tree.getBody());
return super.visitSince(tree, ignore);
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitSummary(SummaryTree tree, Void aVoid) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
if (inSummary) {
env.messages.warning(HTML, tree, "dc.tag.nested.tag", "@" + tree.getTagName());
}
int idx = env.currDocComment.getFullBody().indexOf(tree);
// Warn if the node is preceded by non-whitespace characters,
// or other non-text nodes.
if ((idx == 1 && hasNonWhitespaceText) || idx > 1) {
env.messages.warning(SYNTAX, tree, "dc.invalid.summary", tree.getTagName());
}
boolean prevInSummary = inSummary;
try {
inSummary = true;
return super.visitSummary(tree, aVoid);
} finally {
inSummary = prevInSummary;
}
}
@Override @DefinedBy(Api.COMPILER_TREE)
public Void visitSystemProperty(SystemPropertyTree tree, Void ignore) {
markEnclosingTag(Flag.HAS_INLINE_TAG);
for (TagStackItem tsi : tagStack) {
if (tsi.tag == HtmlTag.A) {
env.messages.warning(HTML, tree, "dc.tag.a.within.a",