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