|
1 /* |
|
2 * Copyright (c) 2012, 2013, 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.tools.doclint; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.io.StringWriter; |
|
30 import java.net.URI; |
|
31 import java.net.URISyntaxException; |
|
32 import java.util.Deque; |
|
33 import java.util.EnumSet; |
|
34 import java.util.HashMap; |
|
35 import java.util.HashSet; |
|
36 import java.util.LinkedList; |
|
37 import java.util.List; |
|
38 import java.util.Map; |
|
39 import java.util.Set; |
|
40 import java.util.regex.Matcher; |
|
41 import java.util.regex.Pattern; |
|
42 |
|
43 import javax.lang.model.element.Element; |
|
44 import javax.lang.model.element.ElementKind; |
|
45 import javax.lang.model.element.ExecutableElement; |
|
46 import javax.lang.model.element.Name; |
|
47 import javax.lang.model.element.VariableElement; |
|
48 import javax.lang.model.type.TypeKind; |
|
49 import javax.lang.model.type.TypeMirror; |
|
50 import javax.tools.Diagnostic.Kind; |
|
51 import javax.tools.JavaFileObject; |
|
52 |
|
53 import com.sun.source.doctree.AttributeTree; |
|
54 import com.sun.source.doctree.AuthorTree; |
|
55 import com.sun.source.doctree.DocCommentTree; |
|
56 import com.sun.source.doctree.DocRootTree; |
|
57 import com.sun.source.doctree.DocTree; |
|
58 import com.sun.source.doctree.EndElementTree; |
|
59 import com.sun.source.doctree.EntityTree; |
|
60 import com.sun.source.doctree.ErroneousTree; |
|
61 import com.sun.source.doctree.IdentifierTree; |
|
62 import com.sun.source.doctree.InheritDocTree; |
|
63 import com.sun.source.doctree.LinkTree; |
|
64 import com.sun.source.doctree.LiteralTree; |
|
65 import com.sun.source.doctree.ParamTree; |
|
66 import com.sun.source.doctree.ReferenceTree; |
|
67 import com.sun.source.doctree.ReturnTree; |
|
68 import com.sun.source.doctree.SerialDataTree; |
|
69 import com.sun.source.doctree.SerialFieldTree; |
|
70 import com.sun.source.doctree.SinceTree; |
|
71 import com.sun.source.doctree.StartElementTree; |
|
72 import com.sun.source.doctree.TextTree; |
|
73 import com.sun.source.doctree.ThrowsTree; |
|
74 import com.sun.source.doctree.UnknownBlockTagTree; |
|
75 import com.sun.source.doctree.UnknownInlineTagTree; |
|
76 import com.sun.source.doctree.ValueTree; |
|
77 import com.sun.source.doctree.VersionTree; |
|
78 import com.sun.source.util.DocTreePath; |
|
79 import com.sun.source.util.DocTreePathScanner; |
|
80 import com.sun.source.util.TreePath; |
|
81 import com.sun.tools.doclint.HtmlTag.AttrKind; |
|
82 import com.sun.tools.javac.tree.DocPretty; |
|
83 import com.sun.tools.javac.util.StringUtils; |
|
84 import static com.sun.tools.doclint.Messages.Group.*; |
|
85 |
|
86 |
|
87 /** |
|
88 * Validate a doc comment. |
|
89 * |
|
90 * <p><b>This is NOT part of any supported API. |
|
91 * If you write code that depends on this, you do so at your own |
|
92 * risk. This code and its internal interfaces are subject to change |
|
93 * or deletion without notice.</b></p> |
|
94 */ |
|
95 public class Checker extends DocTreePathScanner<Void, Void> { |
|
96 final Env env; |
|
97 |
|
98 Set<Element> foundParams = new HashSet<>(); |
|
99 Set<TypeMirror> foundThrows = new HashSet<>(); |
|
100 Map<Element, Set<String>> foundAnchors = new HashMap<>(); |
|
101 boolean foundInheritDoc = false; |
|
102 boolean foundReturn = false; |
|
103 |
|
104 public enum Flag { |
|
105 TABLE_HAS_CAPTION, |
|
106 HAS_ELEMENT, |
|
107 HAS_INLINE_TAG, |
|
108 HAS_TEXT, |
|
109 REPORTED_BAD_INLINE |
|
110 } |
|
111 |
|
112 static class TagStackItem { |
|
113 final DocTree tree; // typically, but not always, StartElementTree |
|
114 final HtmlTag tag; |
|
115 final Set<HtmlTag.Attr> attrs; |
|
116 final Set<Flag> flags; |
|
117 TagStackItem(DocTree tree, HtmlTag tag) { |
|
118 this.tree = tree; |
|
119 this.tag = tag; |
|
120 attrs = EnumSet.noneOf(HtmlTag.Attr.class); |
|
121 flags = EnumSet.noneOf(Flag.class); |
|
122 } |
|
123 @Override |
|
124 public String toString() { |
|
125 return String.valueOf(tag); |
|
126 } |
|
127 } |
|
128 |
|
129 private Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well |
|
130 private HtmlTag currHeaderTag; |
|
131 |
|
132 private final int implicitHeaderLevel; |
|
133 |
|
134 // <editor-fold defaultstate="collapsed" desc="Top level"> |
|
135 |
|
136 Checker(Env env) { |
|
137 env.getClass(); |
|
138 this.env = env; |
|
139 tagStack = new LinkedList<>(); |
|
140 implicitHeaderLevel = env.implicitHeaderLevel; |
|
141 } |
|
142 |
|
143 public Void scan(DocCommentTree tree, TreePath p) { |
|
144 env.setCurrent(p, tree); |
|
145 |
|
146 boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty(); |
|
147 |
|
148 if (p.getLeaf() == p.getCompilationUnit()) { |
|
149 // If p points to a compilation unit, the implied declaration is the |
|
150 // package declaration (if any) for the compilation unit. |
|
151 // Handle this case specially, because doc comments are only |
|
152 // expected in package-info files. |
|
153 JavaFileObject fo = p.getCompilationUnit().getSourceFile(); |
|
154 boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); |
|
155 if (tree == null) { |
|
156 if (isPkgInfo) |
|
157 reportMissing("dc.missing.comment"); |
|
158 return null; |
|
159 } else { |
|
160 if (!isPkgInfo) |
|
161 reportReference("dc.unexpected.comment"); |
|
162 } |
|
163 } else { |
|
164 if (tree == null) { |
|
165 if (!isSynthetic() && !isOverridingMethod) |
|
166 reportMissing("dc.missing.comment"); |
|
167 return null; |
|
168 } |
|
169 } |
|
170 |
|
171 tagStack.clear(); |
|
172 currHeaderTag = null; |
|
173 |
|
174 foundParams.clear(); |
|
175 foundThrows.clear(); |
|
176 foundInheritDoc = false; |
|
177 foundReturn = false; |
|
178 |
|
179 scan(new DocTreePath(p, tree), null); |
|
180 |
|
181 if (!isOverridingMethod) { |
|
182 switch (env.currElement.getKind()) { |
|
183 case METHOD: |
|
184 case CONSTRUCTOR: { |
|
185 ExecutableElement ee = (ExecutableElement) env.currElement; |
|
186 checkParamsDocumented(ee.getTypeParameters()); |
|
187 checkParamsDocumented(ee.getParameters()); |
|
188 switch (ee.getReturnType().getKind()) { |
|
189 case VOID: |
|
190 case NONE: |
|
191 break; |
|
192 default: |
|
193 if (!foundReturn |
|
194 && !foundInheritDoc |
|
195 && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) { |
|
196 reportMissing("dc.missing.return"); |
|
197 } |
|
198 } |
|
199 checkThrowsDocumented(ee.getThrownTypes()); |
|
200 } |
|
201 } |
|
202 } |
|
203 |
|
204 return null; |
|
205 } |
|
206 |
|
207 private void reportMissing(String code, Object... args) { |
|
208 env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args); |
|
209 } |
|
210 |
|
211 private void reportReference(String code, Object... args) { |
|
212 env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args); |
|
213 } |
|
214 |
|
215 @Override |
|
216 public Void visitDocComment(DocCommentTree tree, Void ignore) { |
|
217 super.visitDocComment(tree, ignore); |
|
218 for (TagStackItem tsi: tagStack) { |
|
219 warnIfEmpty(tsi, null); |
|
220 if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT |
|
221 && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) { |
|
222 StartElementTree t = (StartElementTree) tsi.tree; |
|
223 env.messages.error(HTML, t, "dc.tag.not.closed", t.getName()); |
|
224 } |
|
225 } |
|
226 return null; |
|
227 } |
|
228 // </editor-fold> |
|
229 |
|
230 // <editor-fold defaultstate="collapsed" desc="Text and entities."> |
|
231 |
|
232 @Override |
|
233 public Void visitText(TextTree tree, Void ignore) { |
|
234 if (hasNonWhitespace(tree)) { |
|
235 checkAllowsText(tree); |
|
236 markEnclosingTag(Flag.HAS_TEXT); |
|
237 } |
|
238 return null; |
|
239 } |
|
240 |
|
241 @Override |
|
242 public Void visitEntity(EntityTree tree, Void ignore) { |
|
243 checkAllowsText(tree); |
|
244 markEnclosingTag(Flag.HAS_TEXT); |
|
245 String name = tree.getName().toString(); |
|
246 if (name.startsWith("#")) { |
|
247 int v = StringUtils.toLowerCase(name).startsWith("#x") |
|
248 ? Integer.parseInt(name.substring(2), 16) |
|
249 : Integer.parseInt(name.substring(1), 10); |
|
250 if (!Entity.isValid(v)) { |
|
251 env.messages.error(HTML, tree, "dc.entity.invalid", name); |
|
252 } |
|
253 } else if (!Entity.isValid(name)) { |
|
254 env.messages.error(HTML, tree, "dc.entity.invalid", name); |
|
255 } |
|
256 return null; |
|
257 } |
|
258 |
|
259 void checkAllowsText(DocTree tree) { |
|
260 TagStackItem top = tagStack.peek(); |
|
261 if (top != null |
|
262 && top.tree.getKind() == DocTree.Kind.START_ELEMENT |
|
263 && !top.tag.acceptsText()) { |
|
264 if (top.flags.add(Flag.REPORTED_BAD_INLINE)) { |
|
265 env.messages.error(HTML, tree, "dc.text.not.allowed", |
|
266 ((StartElementTree) top.tree).getName()); |
|
267 } |
|
268 } |
|
269 } |
|
270 |
|
271 // </editor-fold> |
|
272 |
|
273 // <editor-fold defaultstate="collapsed" desc="HTML elements"> |
|
274 |
|
275 @Override |
|
276 public Void visitStartElement(StartElementTree tree, Void ignore) { |
|
277 final Name treeName = tree.getName(); |
|
278 final HtmlTag t = HtmlTag.get(treeName); |
|
279 if (t == null) { |
|
280 env.messages.error(HTML, tree, "dc.tag.unknown", treeName); |
|
281 } else { |
|
282 boolean done = false; |
|
283 for (TagStackItem tsi: tagStack) { |
|
284 if (tsi.tag.accepts(t)) { |
|
285 while (tagStack.peek() != tsi) { |
|
286 warnIfEmpty(tagStack.peek(), null); |
|
287 tagStack.pop(); |
|
288 } |
|
289 done = true; |
|
290 break; |
|
291 } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) { |
|
292 done = true; |
|
293 break; |
|
294 } |
|
295 } |
|
296 if (!done && HtmlTag.BODY.accepts(t)) { |
|
297 while (!tagStack.isEmpty()) { |
|
298 warnIfEmpty(tagStack.peek(), null); |
|
299 tagStack.pop(); |
|
300 } |
|
301 } |
|
302 |
|
303 markEnclosingTag(Flag.HAS_ELEMENT); |
|
304 checkStructure(tree, t); |
|
305 |
|
306 // tag specific checks |
|
307 switch (t) { |
|
308 // check for out of sequence headers, such as <h1>...</h1> <h3>...</h3> |
|
309 case H1: case H2: case H3: case H4: case H5: case H6: |
|
310 checkHeader(tree, t); |
|
311 break; |
|
312 } |
|
313 |
|
314 if (t.flags.contains(HtmlTag.Flag.NO_NEST)) { |
|
315 for (TagStackItem i: tagStack) { |
|
316 if (t == i.tag) { |
|
317 env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName); |
|
318 break; |
|
319 } |
|
320 } |
|
321 } |
|
322 } |
|
323 |
|
324 // check for self closing tags, such as <a id="name"/> |
|
325 if (tree.isSelfClosing()) { |
|
326 env.messages.error(HTML, tree, "dc.tag.self.closing", treeName); |
|
327 } |
|
328 |
|
329 try { |
|
330 TagStackItem parent = tagStack.peek(); |
|
331 TagStackItem top = new TagStackItem(tree, t); |
|
332 tagStack.push(top); |
|
333 |
|
334 super.visitStartElement(tree, ignore); |
|
335 |
|
336 // handle attributes that may or may not have been found in start element |
|
337 if (t != null) { |
|
338 switch (t) { |
|
339 case CAPTION: |
|
340 if (parent != null && parent.tag == HtmlTag.TABLE) |
|
341 parent.flags.add(Flag.TABLE_HAS_CAPTION); |
|
342 break; |
|
343 |
|
344 case IMG: |
|
345 if (!top.attrs.contains(HtmlTag.Attr.ALT)) |
|
346 env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image"); |
|
347 break; |
|
348 } |
|
349 } |
|
350 |
|
351 return null; |
|
352 } finally { |
|
353 |
|
354 if (t == null || t.endKind == HtmlTag.EndKind.NONE) |
|
355 tagStack.pop(); |
|
356 } |
|
357 } |
|
358 |
|
359 private void checkStructure(StartElementTree tree, HtmlTag t) { |
|
360 Name treeName = tree.getName(); |
|
361 TagStackItem top = tagStack.peek(); |
|
362 switch (t.blockType) { |
|
363 case BLOCK: |
|
364 if (top == null || top.tag.accepts(t)) |
|
365 return; |
|
366 |
|
367 switch (top.tree.getKind()) { |
|
368 case START_ELEMENT: { |
|
369 if (top.tag.blockType == HtmlTag.BlockType.INLINE) { |
|
370 Name name = ((StartElementTree) top.tree).getName(); |
|
371 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element", |
|
372 treeName, name); |
|
373 return; |
|
374 } |
|
375 } |
|
376 break; |
|
377 |
|
378 case LINK: |
|
379 case LINK_PLAIN: { |
|
380 String name = top.tree.getKind().tagName; |
|
381 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag", |
|
382 treeName, name); |
|
383 return; |
|
384 } |
|
385 } |
|
386 break; |
|
387 |
|
388 case INLINE: |
|
389 if (top == null || top.tag.accepts(t)) |
|
390 return; |
|
391 break; |
|
392 |
|
393 case LIST_ITEM: |
|
394 case TABLE_ITEM: |
|
395 if (top != null) { |
|
396 // reset this flag so subsequent bad inline content gets reported |
|
397 top.flags.remove(Flag.REPORTED_BAD_INLINE); |
|
398 if (top.tag.accepts(t)) |
|
399 return; |
|
400 } |
|
401 break; |
|
402 |
|
403 case OTHER: |
|
404 env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName); |
|
405 return; |
|
406 } |
|
407 |
|
408 env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName); |
|
409 } |
|
410 |
|
411 private void checkHeader(StartElementTree tree, HtmlTag tag) { |
|
412 // verify the new tag |
|
413 if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) { |
|
414 if (currHeaderTag == null) { |
|
415 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag); |
|
416 } else { |
|
417 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2", |
|
418 tag, currHeaderTag); |
|
419 } |
|
420 } |
|
421 |
|
422 currHeaderTag = tag; |
|
423 } |
|
424 |
|
425 private int getHeaderLevel(HtmlTag tag) { |
|
426 if (tag == null) |
|
427 return implicitHeaderLevel; |
|
428 switch (tag) { |
|
429 case H1: return 1; |
|
430 case H2: return 2; |
|
431 case H3: return 3; |
|
432 case H4: return 4; |
|
433 case H5: return 5; |
|
434 case H6: return 6; |
|
435 default: throw new IllegalArgumentException(); |
|
436 } |
|
437 } |
|
438 |
|
439 @Override |
|
440 public Void visitEndElement(EndElementTree tree, Void ignore) { |
|
441 final Name treeName = tree.getName(); |
|
442 final HtmlTag t = HtmlTag.get(treeName); |
|
443 if (t == null) { |
|
444 env.messages.error(HTML, tree, "dc.tag.unknown", treeName); |
|
445 } else if (t.endKind == HtmlTag.EndKind.NONE) { |
|
446 env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName); |
|
447 } else { |
|
448 boolean done = false; |
|
449 while (!tagStack.isEmpty()) { |
|
450 TagStackItem top = tagStack.peek(); |
|
451 if (t == top.tag) { |
|
452 switch (t) { |
|
453 case TABLE: |
|
454 if (!top.attrs.contains(HtmlTag.Attr.SUMMARY) |
|
455 && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) { |
|
456 env.messages.error(ACCESSIBILITY, tree, |
|
457 "dc.no.summary.or.caption.for.table"); |
|
458 } |
|
459 } |
|
460 warnIfEmpty(top, tree); |
|
461 tagStack.pop(); |
|
462 done = true; |
|
463 break; |
|
464 } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) { |
|
465 tagStack.pop(); |
|
466 } else { |
|
467 boolean found = false; |
|
468 for (TagStackItem si: tagStack) { |
|
469 if (si.tag == t) { |
|
470 found = true; |
|
471 break; |
|
472 } |
|
473 } |
|
474 if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) { |
|
475 env.messages.error(HTML, top.tree, "dc.tag.start.unmatched", |
|
476 ((StartElementTree) top.tree).getName()); |
|
477 tagStack.pop(); |
|
478 } else { |
|
479 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
|
480 done = true; |
|
481 break; |
|
482 } |
|
483 } |
|
484 } |
|
485 |
|
486 if (!done && tagStack.isEmpty()) { |
|
487 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
|
488 } |
|
489 } |
|
490 |
|
491 return super.visitEndElement(tree, ignore); |
|
492 } |
|
493 |
|
494 void warnIfEmpty(TagStackItem tsi, DocTree endTree) { |
|
495 if (tsi.tag != null && tsi.tree instanceof StartElementTree) { |
|
496 if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT) |
|
497 && !tsi.flags.contains(Flag.HAS_TEXT) |
|
498 && !tsi.flags.contains(Flag.HAS_ELEMENT) |
|
499 && !tsi.flags.contains(Flag.HAS_INLINE_TAG)) { |
|
500 DocTree tree = (endTree != null) ? endTree : tsi.tree; |
|
501 Name treeName = ((StartElementTree) tsi.tree).getName(); |
|
502 env.messages.warning(HTML, tree, "dc.tag.empty", treeName); |
|
503 } |
|
504 } |
|
505 } |
|
506 |
|
507 // </editor-fold> |
|
508 |
|
509 // <editor-fold defaultstate="collapsed" desc="HTML attributes"> |
|
510 |
|
511 @Override @SuppressWarnings("fallthrough") |
|
512 public Void visitAttribute(AttributeTree tree, Void ignore) { |
|
513 HtmlTag currTag = tagStack.peek().tag; |
|
514 if (currTag != null) { |
|
515 Name name = tree.getName(); |
|
516 HtmlTag.Attr attr = currTag.getAttr(name); |
|
517 if (attr != null) { |
|
518 boolean first = tagStack.peek().attrs.add(attr); |
|
519 if (!first) |
|
520 env.messages.error(HTML, tree, "dc.attr.repeated", name); |
|
521 } |
|
522 AttrKind k = currTag.getAttrKind(name); |
|
523 switch (k) { |
|
524 case OK: |
|
525 break; |
|
526 |
|
527 case INVALID: |
|
528 env.messages.error(HTML, tree, "dc.attr.unknown", name); |
|
529 break; |
|
530 |
|
531 case OBSOLETE: |
|
532 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name); |
|
533 break; |
|
534 |
|
535 case USE_CSS: |
|
536 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name); |
|
537 break; |
|
538 } |
|
539 |
|
540 if (attr != null) { |
|
541 switch (attr) { |
|
542 case NAME: |
|
543 if (currTag != HtmlTag.A) { |
|
544 break; |
|
545 } |
|
546 // fallthrough |
|
547 case ID: |
|
548 String value = getAttrValue(tree); |
|
549 if (value == null) { |
|
550 env.messages.error(HTML, tree, "dc.anchor.value.missing"); |
|
551 } else { |
|
552 if (!validName.matcher(value).matches()) { |
|
553 env.messages.error(HTML, tree, "dc.invalid.anchor", value); |
|
554 } |
|
555 if (!checkAnchor(value)) { |
|
556 env.messages.error(HTML, tree, "dc.anchor.already.defined", value); |
|
557 } |
|
558 } |
|
559 break; |
|
560 |
|
561 case HREF: |
|
562 if (currTag == HtmlTag.A) { |
|
563 String v = getAttrValue(tree); |
|
564 if (v == null || v.isEmpty()) { |
|
565 env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
|
566 } else { |
|
567 Matcher m = docRoot.matcher(v); |
|
568 if (m.matches()) { |
|
569 String rest = m.group(2); |
|
570 if (!rest.isEmpty()) |
|
571 checkURI(tree, rest); |
|
572 } else { |
|
573 checkURI(tree, v); |
|
574 } |
|
575 } |
|
576 } |
|
577 break; |
|
578 |
|
579 case VALUE: |
|
580 if (currTag == HtmlTag.LI) { |
|
581 String v = getAttrValue(tree); |
|
582 if (v == null || v.isEmpty()) { |
|
583 env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
|
584 } else if (!validNumber.matcher(v).matches()) { |
|
585 env.messages.error(HTML, tree, "dc.attr.not.number"); |
|
586 } |
|
587 } |
|
588 break; |
|
589 } |
|
590 } |
|
591 } |
|
592 |
|
593 // TODO: basic check on value |
|
594 |
|
595 return super.visitAttribute(tree, ignore); |
|
596 } |
|
597 |
|
598 private boolean checkAnchor(String name) { |
|
599 Element e = getEnclosingPackageOrClass(env.currElement); |
|
600 if (e == null) |
|
601 return true; |
|
602 Set<String> set = foundAnchors.get(e); |
|
603 if (set == null) |
|
604 foundAnchors.put(e, set = new HashSet<>()); |
|
605 return set.add(name); |
|
606 } |
|
607 |
|
608 private Element getEnclosingPackageOrClass(Element e) { |
|
609 while (e != null) { |
|
610 switch (e.getKind()) { |
|
611 case CLASS: |
|
612 case ENUM: |
|
613 case INTERFACE: |
|
614 case PACKAGE: |
|
615 return e; |
|
616 default: |
|
617 e = e.getEnclosingElement(); |
|
618 } |
|
619 } |
|
620 return e; |
|
621 } |
|
622 |
|
623 // http://www.w3.org/TR/html401/types.html#type-name |
|
624 private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*"); |
|
625 |
|
626 private static final Pattern validNumber = Pattern.compile("-?[0-9]+"); |
|
627 |
|
628 // pattern to remove leading {@docRoot}/? |
|
629 private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)"); |
|
630 |
|
631 private String getAttrValue(AttributeTree tree) { |
|
632 if (tree.getValue() == null) |
|
633 return null; |
|
634 |
|
635 StringWriter sw = new StringWriter(); |
|
636 try { |
|
637 new DocPretty(sw).print(tree.getValue()); |
|
638 } catch (IOException e) { |
|
639 // cannot happen |
|
640 } |
|
641 // ignore potential use of entities for now |
|
642 return sw.toString(); |
|
643 } |
|
644 |
|
645 private void checkURI(AttributeTree tree, String uri) { |
|
646 try { |
|
647 URI u = new URI(uri); |
|
648 } catch (URISyntaxException e) { |
|
649 env.messages.error(HTML, tree, "dc.invalid.uri", uri); |
|
650 } |
|
651 } |
|
652 // </editor-fold> |
|
653 |
|
654 // <editor-fold defaultstate="collapsed" desc="javadoc tags"> |
|
655 |
|
656 @Override |
|
657 public Void visitAuthor(AuthorTree tree, Void ignore) { |
|
658 warnIfEmpty(tree, tree.getName()); |
|
659 return super.visitAuthor(tree, ignore); |
|
660 } |
|
661 |
|
662 @Override |
|
663 public Void visitDocRoot(DocRootTree tree, Void ignore) { |
|
664 markEnclosingTag(Flag.HAS_INLINE_TAG); |
|
665 return super.visitDocRoot(tree, ignore); |
|
666 } |
|
667 |
|
668 @Override |
|
669 public Void visitInheritDoc(InheritDocTree tree, Void ignore) { |
|
670 markEnclosingTag(Flag.HAS_INLINE_TAG); |
|
671 // TODO: verify on overridden method |
|
672 foundInheritDoc = true; |
|
673 return super.visitInheritDoc(tree, ignore); |
|
674 } |
|
675 |
|
676 @Override |
|
677 public Void visitLink(LinkTree tree, Void ignore) { |
|
678 markEnclosingTag(Flag.HAS_INLINE_TAG); |
|
679 // simulate inline context on tag stack |
|
680 HtmlTag t = (tree.getKind() == DocTree.Kind.LINK) |
|
681 ? HtmlTag.CODE : HtmlTag.SPAN; |
|
682 tagStack.push(new TagStackItem(tree, t)); |
|
683 try { |
|
684 return super.visitLink(tree, ignore); |
|
685 } finally { |
|
686 tagStack.pop(); |
|
687 } |
|
688 } |
|
689 |
|
690 @Override |
|
691 public Void visitLiteral(LiteralTree tree, Void ignore) { |
|
692 markEnclosingTag(Flag.HAS_INLINE_TAG); |
|
693 if (tree.getKind() == DocTree.Kind.CODE) { |
|
694 for (TagStackItem tsi: tagStack) { |
|
695 if (tsi.tag == HtmlTag.CODE) { |
|
696 env.messages.warning(HTML, tree, "dc.tag.code.within.code"); |
|
697 break; |
|
698 } |
|
699 } |
|
700 } |
|
701 return super.visitLiteral(tree, ignore); |
|
702 } |
|
703 |
|
704 @Override |
|
705 @SuppressWarnings("fallthrough") |
|
706 public Void visitParam(ParamTree tree, Void ignore) { |
|
707 boolean typaram = tree.isTypeParameter(); |
|
708 IdentifierTree nameTree = tree.getName(); |
|
709 Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null; |
|
710 |
|
711 if (paramElement == null) { |
|
712 switch (env.currElement.getKind()) { |
|
713 case CLASS: case INTERFACE: { |
|
714 if (!typaram) { |
|
715 env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
|
716 break; |
|
717 } |
|
718 } |
|
719 case METHOD: case CONSTRUCTOR: { |
|
720 env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found"); |
|
721 break; |
|
722 } |
|
723 |
|
724 default: |
|
725 env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
|
726 break; |
|
727 } |
|
728 } else { |
|
729 foundParams.add(paramElement); |
|
730 } |
|
731 |
|
732 warnIfEmpty(tree, tree.getDescription()); |
|
733 return super.visitParam(tree, ignore); |
|
734 } |
|
735 |
|
736 private void checkParamsDocumented(List<? extends Element> list) { |
|
737 if (foundInheritDoc) |
|
738 return; |
|
739 |
|
740 for (Element e: list) { |
|
741 if (!foundParams.contains(e)) { |
|
742 CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER) |
|
743 ? "<" + e.getSimpleName() + ">" |
|
744 : e.getSimpleName(); |
|
745 reportMissing("dc.missing.param", paramName); |
|
746 } |
|
747 } |
|
748 } |
|
749 |
|
750 @Override |
|
751 public Void visitReference(ReferenceTree tree, Void ignore) { |
|
752 String sig = tree.getSignature(); |
|
753 if (sig.contains("<") || sig.contains(">")) |
|
754 env.messages.error(REFERENCE, tree, "dc.type.arg.not.allowed"); |
|
755 |
|
756 Element e = env.trees.getElement(getCurrentPath()); |
|
757 if (e == null) |
|
758 env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
|
759 return super.visitReference(tree, ignore); |
|
760 } |
|
761 |
|
762 @Override |
|
763 public Void visitReturn(ReturnTree tree, Void ignore) { |
|
764 Element e = env.trees.getElement(env.currPath); |
|
765 if (e.getKind() != ElementKind.METHOD |
|
766 || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID) |
|
767 env.messages.error(REFERENCE, tree, "dc.invalid.return"); |
|
768 foundReturn = true; |
|
769 warnIfEmpty(tree, tree.getDescription()); |
|
770 return super.visitReturn(tree, ignore); |
|
771 } |
|
772 |
|
773 @Override |
|
774 public Void visitSerialData(SerialDataTree tree, Void ignore) { |
|
775 warnIfEmpty(tree, tree.getDescription()); |
|
776 return super.visitSerialData(tree, ignore); |
|
777 } |
|
778 |
|
779 @Override |
|
780 public Void visitSerialField(SerialFieldTree tree, Void ignore) { |
|
781 warnIfEmpty(tree, tree.getDescription()); |
|
782 return super.visitSerialField(tree, ignore); |
|
783 } |
|
784 |
|
785 @Override |
|
786 public Void visitSince(SinceTree tree, Void ignore) { |
|
787 warnIfEmpty(tree, tree.getBody()); |
|
788 return super.visitSince(tree, ignore); |
|
789 } |
|
790 |
|
791 @Override |
|
792 public Void visitThrows(ThrowsTree tree, Void ignore) { |
|
793 ReferenceTree exName = tree.getExceptionName(); |
|
794 Element ex = env.trees.getElement(new DocTreePath(getCurrentPath(), exName)); |
|
795 if (ex == null) { |
|
796 env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
|
797 } else if (isThrowable(ex.asType())) { |
|
798 switch (env.currElement.getKind()) { |
|
799 case CONSTRUCTOR: |
|
800 case METHOD: |
|
801 if (isCheckedException(ex.asType())) { |
|
802 ExecutableElement ee = (ExecutableElement) env.currElement; |
|
803 checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes()); |
|
804 } |
|
805 break; |
|
806 default: |
|
807 env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
|
808 } |
|
809 } else { |
|
810 env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
|
811 } |
|
812 warnIfEmpty(tree, tree.getDescription()); |
|
813 return scan(tree.getDescription(), ignore); |
|
814 } |
|
815 |
|
816 private boolean isThrowable(TypeMirror tm) { |
|
817 switch (tm.getKind()) { |
|
818 case DECLARED: |
|
819 case TYPEVAR: |
|
820 return env.types.isAssignable(tm, env.java_lang_Throwable); |
|
821 } |
|
822 return false; |
|
823 } |
|
824 |
|
825 private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) { |
|
826 boolean found = false; |
|
827 for (TypeMirror tl : list) { |
|
828 if (env.types.isAssignable(t, tl)) { |
|
829 foundThrows.add(tl); |
|
830 found = true; |
|
831 } |
|
832 } |
|
833 if (!found) |
|
834 env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t); |
|
835 } |
|
836 |
|
837 private void checkThrowsDocumented(List<? extends TypeMirror> list) { |
|
838 if (foundInheritDoc) |
|
839 return; |
|
840 |
|
841 for (TypeMirror tl: list) { |
|
842 if (isCheckedException(tl) && !foundThrows.contains(tl)) |
|
843 reportMissing("dc.missing.throws", tl); |
|
844 } |
|
845 } |
|
846 |
|
847 @Override |
|
848 public Void visitUnknownBlockTag(UnknownBlockTagTree tree, Void ignore) { |
|
849 checkUnknownTag(tree, tree.getTagName()); |
|
850 return super.visitUnknownBlockTag(tree, ignore); |
|
851 } |
|
852 |
|
853 @Override |
|
854 public Void visitUnknownInlineTag(UnknownInlineTagTree tree, Void ignore) { |
|
855 checkUnknownTag(tree, tree.getTagName()); |
|
856 return super.visitUnknownInlineTag(tree, ignore); |
|
857 } |
|
858 |
|
859 private void checkUnknownTag(DocTree tree, String tagName) { |
|
860 if (env.customTags != null && !env.customTags.contains(tagName)) |
|
861 env.messages.error(SYNTAX, tree, "dc.tag.unknown", tagName); |
|
862 } |
|
863 |
|
864 @Override |
|
865 public Void visitValue(ValueTree tree, Void ignore) { |
|
866 ReferenceTree ref = tree.getReference(); |
|
867 if (ref == null || ref.getSignature().isEmpty()) { |
|
868 if (!isConstant(env.currElement)) |
|
869 env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here"); |
|
870 } else { |
|
871 Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref)); |
|
872 if (!isConstant(e)) |
|
873 env.messages.error(REFERENCE, tree, "dc.value.not.a.constant"); |
|
874 } |
|
875 |
|
876 markEnclosingTag(Flag.HAS_INLINE_TAG); |
|
877 return super.visitValue(tree, ignore); |
|
878 } |
|
879 |
|
880 private boolean isConstant(Element e) { |
|
881 if (e == null) |
|
882 return false; |
|
883 |
|
884 switch (e.getKind()) { |
|
885 case FIELD: |
|
886 Object value = ((VariableElement) e).getConstantValue(); |
|
887 return (value != null); // can't distinguish "not a constant" from "constant is null" |
|
888 default: |
|
889 return false; |
|
890 } |
|
891 } |
|
892 |
|
893 @Override |
|
894 public Void visitVersion(VersionTree tree, Void ignore) { |
|
895 warnIfEmpty(tree, tree.getBody()); |
|
896 return super.visitVersion(tree, ignore); |
|
897 } |
|
898 |
|
899 @Override |
|
900 public Void visitErroneous(ErroneousTree tree, Void ignore) { |
|
901 env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null)); |
|
902 return null; |
|
903 } |
|
904 // </editor-fold> |
|
905 |
|
906 // <editor-fold defaultstate="collapsed" desc="Utility methods"> |
|
907 |
|
908 private boolean isCheckedException(TypeMirror t) { |
|
909 return !(env.types.isAssignable(t, env.java_lang_Error) |
|
910 || env.types.isAssignable(t, env.java_lang_RuntimeException)); |
|
911 } |
|
912 |
|
913 private boolean isSynthetic() { |
|
914 switch (env.currElement.getKind()) { |
|
915 case CONSTRUCTOR: |
|
916 // A synthetic default constructor has the same pos as the |
|
917 // enclosing class |
|
918 TreePath p = env.currPath; |
|
919 return env.getPos(p) == env.getPos(p.getParentPath()); |
|
920 } |
|
921 return false; |
|
922 } |
|
923 |
|
924 void markEnclosingTag(Flag flag) { |
|
925 TagStackItem top = tagStack.peek(); |
|
926 if (top != null) |
|
927 top.flags.add(flag); |
|
928 } |
|
929 |
|
930 String toString(TreePath p) { |
|
931 StringBuilder sb = new StringBuilder("TreePath["); |
|
932 toString(p, sb); |
|
933 sb.append("]"); |
|
934 return sb.toString(); |
|
935 } |
|
936 |
|
937 void toString(TreePath p, StringBuilder sb) { |
|
938 TreePath parent = p.getParentPath(); |
|
939 if (parent != null) { |
|
940 toString(parent, sb); |
|
941 sb.append(","); |
|
942 } |
|
943 sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p)); |
|
944 } |
|
945 |
|
946 void warnIfEmpty(DocTree tree, List<? extends DocTree> list) { |
|
947 for (DocTree d: list) { |
|
948 switch (d.getKind()) { |
|
949 case TEXT: |
|
950 if (hasNonWhitespace((TextTree) d)) |
|
951 return; |
|
952 break; |
|
953 default: |
|
954 return; |
|
955 } |
|
956 } |
|
957 env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName); |
|
958 } |
|
959 |
|
960 boolean hasNonWhitespace(TextTree tree) { |
|
961 String s = tree.getBody(); |
|
962 for (int i = 0; i < s.length(); i++) { |
|
963 if (!Character.isWhitespace(s.charAt(i))) |
|
964 return true; |
|
965 } |
|
966 return false; |
|
967 } |
|
968 |
|
969 // </editor-fold> |
|
970 |
|
971 } |