jjg@1455: /* jjg@1495: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1455: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1455: * jjg@1455: * This code is free software; you can redistribute it and/or modify it jjg@1455: * under the terms of the GNU General Public License version 2 only, as jjg@1455: * published by the Free Software Foundation. Oracle designates this jjg@1455: * particular file as subject to the "Classpath" exception as provided jjg@1455: * by Oracle in the LICENSE file that accompanied this code. jjg@1455: * jjg@1455: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1455: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1455: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1455: * version 2 for more details (a copy is included in the LICENSE file that jjg@1455: * accompanied this code). jjg@1455: * jjg@1455: * You should have received a copy of the GNU General Public License version jjg@1455: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1455: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1455: * jjg@1455: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1455: * or visit www.oracle.com if you need additional information or have any jjg@1455: * questions. jjg@1455: */ jjg@1455: jjg@1455: package com.sun.tools.doclint; jjg@1455: jjg@1499: import com.sun.source.doctree.LiteralTree; jjg@1455: import java.util.regex.Matcher; jjg@1455: import com.sun.source.doctree.LinkTree; jjg@1455: import java.net.URI; jjg@1455: import java.util.regex.Pattern; jjg@1455: import java.io.IOException; jjg@1455: import com.sun.tools.javac.tree.DocPretty; jjg@1455: import java.io.StringWriter; jjg@1455: import java.util.Deque; jjg@1455: import java.util.EnumSet; jjg@1455: import java.util.HashSet; jjg@1455: import java.util.LinkedList; jjg@1455: import java.util.List; jjg@1455: import java.util.Set; jjg@1455: jjg@1455: import javax.lang.model.element.Element; jjg@1455: import javax.lang.model.element.ElementKind; jjg@1455: import javax.lang.model.element.ExecutableElement; jjg@1455: import javax.lang.model.element.Name; jjg@1455: import javax.lang.model.element.TypeElement; jjg@1455: import javax.lang.model.type.TypeKind; jjg@1455: import javax.lang.model.type.TypeMirror; jjg@1455: import javax.tools.Diagnostic.Kind; jjg@1455: jjg@1455: import com.sun.source.doctree.AttributeTree; jjg@1455: import com.sun.source.doctree.AuthorTree; jjg@1455: import com.sun.source.doctree.DocCommentTree; jjg@1455: import com.sun.source.doctree.DocTree; jjg@1455: import com.sun.source.doctree.EndElementTree; jjg@1455: import com.sun.source.doctree.EntityTree; jjg@1455: import com.sun.source.doctree.ErroneousTree; jjg@1455: import com.sun.source.doctree.IdentifierTree; jjg@1455: import com.sun.source.doctree.InheritDocTree; jjg@1455: import com.sun.source.doctree.ParamTree; jjg@1455: import com.sun.source.doctree.ReferenceTree; jjg@1455: import com.sun.source.doctree.ReturnTree; jjg@1455: import com.sun.source.doctree.SerialDataTree; jjg@1455: import com.sun.source.doctree.SerialFieldTree; jjg@1455: import com.sun.source.doctree.SinceTree; jjg@1455: import com.sun.source.doctree.StartElementTree; jjg@1455: import com.sun.source.doctree.TextTree; jjg@1455: import com.sun.source.doctree.ThrowsTree; jjg@1455: import com.sun.source.doctree.VersionTree; jjg@1455: import com.sun.source.util.DocTreeScanner; jjg@1455: import com.sun.source.util.TreePath; jjg@1455: import com.sun.tools.doclint.HtmlTag.AttrKind; jjg@1455: import java.net.URISyntaxException; jjg@1455: import static com.sun.tools.doclint.Messages.Group.*; jjg@1455: jjg@1455: jjg@1455: /** jjg@1455: * Validate a doc comment. jjg@1455: * jjg@1455: *

This is NOT part of any supported API. jjg@1455: * If you write code that depends on this, you do so at your own jjg@1455: * risk. This code and its internal interfaces are subject to change jjg@1455: * or deletion without notice.

jjg@1455: */ jjg@1455: public class Checker extends DocTreeScanner { jjg@1455: final Env env; jjg@1455: jjg@1455: Set foundParams = new HashSet(); jjg@1455: Set foundThrows = new HashSet(); jjg@1455: Set foundAnchors = new HashSet(); jjg@1455: boolean foundInheritDoc = false; jjg@1455: boolean foundReturn = false; jjg@1455: jjg@1506: public enum Flag { jjg@1455: TABLE_HAS_CAPTION, jjg@1455: HAS_ELEMENT, jjg@1507: HAS_TEXT, jjg@1507: REPORTED_BAD_INLINE jjg@1455: } jjg@1455: jjg@1455: static class TagStackItem { jjg@1455: final DocTree tree; // typically, but not always, StartElementTree jjg@1455: final HtmlTag tag; jjg@1455: final Set attrs; jjg@1455: final Set flags; jjg@1455: TagStackItem(DocTree tree, HtmlTag tag) { jjg@1455: this.tree = tree; jjg@1455: this.tag = tag; jjg@1455: attrs = EnumSet.noneOf(HtmlTag.Attr.class); jjg@1455: flags = EnumSet.noneOf(Flag.class); jjg@1455: } jjg@1455: @Override jjg@1455: public String toString() { jjg@1455: return String.valueOf(tag); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: private Deque tagStack; // TODO: maybe want to record starting tree as well jjg@1455: private HtmlTag currHeaderTag; jjg@1455: jjg@1455: // jjg@1455: jjg@1455: Checker(Env env) { jjg@1455: env.getClass(); jjg@1455: this.env = env; jjg@1455: tagStack = new LinkedList(); jjg@1455: } jjg@1455: jjg@1455: public Void scan(DocCommentTree tree, TreePath p) { jjg@1455: env.setCurrent(p, tree); jjg@1455: jjg@1455: boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty(); jjg@1455: jjg@1455: if (tree == null) { jjg@1455: if (!isSynthetic() && !isOverridingMethod) jjg@1455: reportMissing("dc.missing.comment"); jjg@1455: return null; jjg@1455: } jjg@1455: jjg@1455: tagStack.clear(); jjg@1455: currHeaderTag = null; jjg@1455: jjg@1455: foundParams.clear(); jjg@1455: foundThrows.clear(); jjg@1455: foundInheritDoc = false; jjg@1455: foundReturn = false; jjg@1455: jjg@1455: scan(tree, (Void) null); jjg@1455: jjg@1455: if (!isOverridingMethod) { jjg@1455: switch (env.currElement.getKind()) { jjg@1455: case METHOD: jjg@1455: case CONSTRUCTOR: { jjg@1455: ExecutableElement ee = (ExecutableElement) env.currElement; jjg@1455: checkParamsDocumented(ee.getTypeParameters()); jjg@1455: checkParamsDocumented(ee.getParameters()); jjg@1455: switch (ee.getReturnType().getKind()) { jjg@1455: case VOID: jjg@1455: case NONE: jjg@1455: break; jjg@1455: default: jjg@1455: if (!foundReturn jjg@1455: && !foundInheritDoc jjg@1455: && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) { jjg@1455: reportMissing("dc.missing.return"); jjg@1455: } jjg@1455: } jjg@1455: checkThrowsDocumented(ee.getThrownTypes()); jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: return null; jjg@1455: } jjg@1455: jjg@1455: private void reportMissing(String code, Object... args) { jjg@1455: env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitDocComment(DocCommentTree tree, Void ignore) { jjg@1455: super.visitDocComment(tree, ignore); jjg@1455: for (TagStackItem tsi: tagStack) { jjg@1455: if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT jjg@1455: && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) { jjg@1455: StartElementTree t = (StartElementTree) tsi.tree; jjg@1455: env.messages.error(HTML, t, "dc.tag.not.closed", t.getName()); jjg@1455: } jjg@1455: } jjg@1455: return null; jjg@1455: } jjg@1455: // jjg@1455: jjg@1455: // jjg@1455: jjg@1455: @Override jjg@1455: public Void visitText(TextTree tree, Void ignore) { jjg@1507: if (hasNonWhitespace(tree)) { jjg@1507: checkAllowsText(tree); jjg@1455: markEnclosingTag(Flag.HAS_TEXT); jjg@1455: } jjg@1455: return null; jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitEntity(EntityTree tree, Void ignore) { jjg@1507: checkAllowsText(tree); jjg@1455: markEnclosingTag(Flag.HAS_TEXT); jjg@1455: String name = tree.getName().toString(); jjg@1455: if (name.startsWith("#")) { jjg@1455: int v = name.toLowerCase().startsWith("#x") jjg@1455: ? Integer.parseInt(name.substring(2), 16) jjg@1455: : Integer.parseInt(name.substring(1), 10); jjg@1455: if (!Entity.isValid(v)) { jjg@1455: env.messages.error(HTML, tree, "dc.entity.invalid", name); jjg@1455: } jjg@1455: } else if (!Entity.isValid(name)) { jjg@1455: env.messages.error(HTML, tree, "dc.entity.invalid", name); jjg@1455: } jjg@1455: return null; jjg@1455: } jjg@1455: jjg@1507: void checkAllowsText(DocTree tree) { jjg@1507: TagStackItem top = tagStack.peek(); jjg@1507: if (top != null jjg@1507: && top.tree.getKind() == DocTree.Kind.START_ELEMENT jjg@1507: && !top.tag.acceptsText()) { jjg@1507: if (top.flags.add(Flag.REPORTED_BAD_INLINE)) { jjg@1507: env.messages.error(HTML, tree, "dc.text.not.allowed", jjg@1507: ((StartElementTree) top.tree).getName()); jjg@1507: } jjg@1507: } jjg@1507: } jjg@1507: jjg@1455: // jjg@1455: jjg@1455: // jjg@1455: jjg@1455: @Override jjg@1455: public Void visitStartElement(StartElementTree tree, Void ignore) { jjg@1455: markEnclosingTag(Flag.HAS_ELEMENT); jjg@1455: final Name treeName = tree.getName(); jjg@1455: final HtmlTag t = HtmlTag.get(treeName); jjg@1455: if (t == null) { jjg@1455: env.messages.error(HTML, tree, "dc.tag.unknown", treeName); jjg@1455: } else { jjg@1552: boolean done = false; jjg@1507: for (TagStackItem tsi: tagStack) { jjg@1507: if (tsi.tag.accepts(t)) { jjg@1507: while (tagStack.peek() != tsi) tagStack.pop(); jjg@1552: done = true; jjg@1507: break; jjg@1552: } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) { jjg@1552: done = true; jjg@1507: break; jjg@1552: } jjg@1552: } jjg@1552: if (!done && HtmlTag.BODY.accepts(t)) { jjg@1552: tagStack.clear(); jjg@1507: } jjg@1507: jjg@1507: checkStructure(tree, t); jjg@1507: jjg@1455: // tag specific checks jjg@1455: switch (t) { jjg@1455: // check for out of sequence headers, such as

...

...

jjg@1455: case H1: case H2: case H3: case H4: case H5: case H6: jjg@1455: checkHeader(tree, t); jjg@1455: break; jjg@1455: } jjg@1455: jjg@1455: if (t.flags.contains(HtmlTag.Flag.NO_NEST)) { jjg@1455: for (TagStackItem i: tagStack) { jjg@1455: if (t == i.tag) { jjg@1455: env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName); jjg@1455: break; jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: // check for self closing tags, such as jjg@1455: if (tree.isSelfClosing()) { jjg@1455: env.messages.error(HTML, tree, "dc.tag.self.closing", treeName); jjg@1455: } jjg@1455: jjg@1455: try { jjg@1455: TagStackItem parent = tagStack.peek(); jjg@1455: TagStackItem top = new TagStackItem(tree, t); jjg@1455: tagStack.push(top); jjg@1455: jjg@1455: super.visitStartElement(tree, ignore); jjg@1455: jjg@1455: // handle attributes that may or may not have been found in start element jjg@1455: if (t != null) { jjg@1455: switch (t) { jjg@1455: case CAPTION: jjg@1455: if (parent != null && parent.tag == HtmlTag.TABLE) jjg@1455: parent.flags.add(Flag.TABLE_HAS_CAPTION); jjg@1455: break; jjg@1455: jjg@1455: case IMG: jjg@1455: if (!top.attrs.contains(HtmlTag.Attr.ALT)) jjg@1455: env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image"); jjg@1455: break; jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: return null; jjg@1455: } finally { jjg@1455: jjg@1455: if (t == null || t.endKind == HtmlTag.EndKind.NONE) jjg@1455: tagStack.pop(); jjg@1455: } jjg@1455: } jjg@1455: jjg@1507: private void checkStructure(StartElementTree tree, HtmlTag t) { jjg@1507: Name treeName = tree.getName(); jjg@1507: TagStackItem top = tagStack.peek(); jjg@1507: switch (t.blockType) { jjg@1507: case BLOCK: jjg@1507: if (top == null || top.tag.accepts(t)) jjg@1507: return; jjg@1507: jjg@1507: switch (top.tree.getKind()) { jjg@1507: case START_ELEMENT: { jjg@1507: if (top.tag.blockType == HtmlTag.BlockType.INLINE) { jjg@1507: Name name = ((StartElementTree) top.tree).getName(); jjg@1507: env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element", jjg@1507: treeName, name); jjg@1507: return; jjg@1507: } jjg@1507: } jjg@1507: break; jjg@1507: jjg@1507: case LINK: jjg@1507: case LINK_PLAIN: { jjg@1507: String name = top.tree.getKind().tagName; jjg@1507: env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag", jjg@1507: treeName, name); jjg@1507: return; jjg@1507: } jjg@1507: } jjg@1507: break; jjg@1507: jjg@1507: case INLINE: jjg@1507: if (top == null || top.tag.accepts(t)) jjg@1507: return; jjg@1507: break; jjg@1507: jjg@1507: case LIST_ITEM: jjg@1507: case TABLE_ITEM: jjg@1507: if (top != null) { jjg@1507: // reset this flag so subsequent bad inline content gets reported jjg@1507: top.flags.remove(Flag.REPORTED_BAD_INLINE); jjg@1507: if (top.tag.accepts(t)) jjg@1507: return; jjg@1507: } jjg@1507: break; jjg@1507: jjg@1507: case OTHER: jjg@1507: env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName); jjg@1507: return; jjg@1507: } jjg@1507: jjg@1507: env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName); jjg@1507: } jjg@1507: jjg@1455: private void checkHeader(StartElementTree tree, HtmlTag tag) { jjg@1455: // verify the new tag jjg@1455: if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) { jjg@1455: if (currHeaderTag == null) { jjg@1455: env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag); jjg@1455: } else { jjg@1455: env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2", jjg@1455: tag, currHeaderTag); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: currHeaderTag = tag; jjg@1455: } jjg@1455: jjg@1455: private int getHeaderLevel(HtmlTag tag) { jjg@1455: if (tag == null) jjg@1455: return 0; jjg@1455: switch (tag) { jjg@1455: case H1: return 1; jjg@1455: case H2: return 2; jjg@1455: case H3: return 3; jjg@1455: case H4: return 4; jjg@1455: case H5: return 5; jjg@1455: case H6: return 6; jjg@1455: default: throw new IllegalArgumentException(); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitEndElement(EndElementTree tree, Void ignore) { jjg@1455: final Name treeName = tree.getName(); jjg@1455: final HtmlTag t = HtmlTag.get(treeName); jjg@1455: if (t == null) { jjg@1455: env.messages.error(HTML, tree, "dc.tag.unknown", treeName); jjg@1455: } else if (t.endKind == HtmlTag.EndKind.NONE) { jjg@1455: env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName); jjg@1455: } else { jjg@1499: boolean done = false; jjg@1455: while (!tagStack.isEmpty()) { jjg@1455: TagStackItem top = tagStack.peek(); jjg@1455: if (t == top.tag) { jjg@1455: switch (t) { jjg@1455: case TABLE: jjg@1455: if (!top.attrs.contains(HtmlTag.Attr.SUMMARY) jjg@1455: && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) { jjg@1455: env.messages.error(ACCESSIBILITY, tree, jjg@1455: "dc.no.summary.or.caption.for.table"); jjg@1455: } jjg@1455: } jjg@1455: if (t.flags.contains(HtmlTag.Flag.EXPECT_CONTENT) jjg@1455: && !top.flags.contains(Flag.HAS_TEXT) jjg@1455: && !top.flags.contains(Flag.HAS_ELEMENT)) { jjg@1455: env.messages.warning(HTML, tree, "dc.tag.empty", treeName); jjg@1455: } jjg@1455: tagStack.pop(); jjg@1499: done = true; jjg@1455: break; jjg@1455: } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) { jjg@1455: tagStack.pop(); jjg@1455: } else { jjg@1455: boolean found = false; jjg@1455: for (TagStackItem si: tagStack) { jjg@1455: if (si.tag == t) { jjg@1455: found = true; jjg@1455: break; jjg@1455: } jjg@1455: } jjg@1455: if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) { jjg@1455: env.messages.error(HTML, top.tree, "dc.tag.start.unmatched", jjg@1455: ((StartElementTree) top.tree).getName()); jjg@1455: tagStack.pop(); jjg@1455: } else { jjg@1455: env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); jjg@1499: done = true; jjg@1455: break; jjg@1455: } jjg@1455: } jjg@1455: } jjg@1499: jjg@1499: if (!done && tagStack.isEmpty()) { jjg@1499: env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); jjg@1499: } jjg@1455: } jjg@1455: jjg@1455: return super.visitEndElement(tree, ignore); jjg@1455: } jjg@1455: //
jjg@1455: jjg@1455: // jjg@1455: jjg@1455: @Override @SuppressWarnings("fallthrough") jjg@1455: public Void visitAttribute(AttributeTree tree, Void ignore) { jjg@1455: HtmlTag currTag = tagStack.peek().tag; jjg@1455: if (currTag != null) { jjg@1455: Name name = tree.getName(); jjg@1455: HtmlTag.Attr attr = currTag.getAttr(name); jjg@1455: if (attr != null) { jjg@1455: boolean first = tagStack.peek().attrs.add(attr); jjg@1455: if (!first) jjg@1455: env.messages.error(HTML, tree, "dc.attr.repeated", name); jjg@1455: } jjg@1455: AttrKind k = currTag.getAttrKind(name); jjg@1455: switch (k) { jjg@1455: case OK: jjg@1455: break; jjg@1455: jjg@1455: case INVALID: jjg@1455: env.messages.error(HTML, tree, "dc.attr.unknown", name); jjg@1455: break; jjg@1455: jjg@1455: case OBSOLETE: jjg@1455: env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name); jjg@1455: break; jjg@1455: jjg@1455: case USE_CSS: jjg@1455: env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name); jjg@1455: break; jjg@1455: } jjg@1455: jjg@1455: if (attr != null) { jjg@1455: switch (attr) { jjg@1455: case NAME: jjg@1455: if (currTag != HtmlTag.A) { jjg@1455: break; jjg@1455: } jjg@1495: // fallthrough jjg@1455: case ID: jjg@1455: String value = getAttrValue(tree); jjg@1495: if (value == null) { jjg@1495: env.messages.error(HTML, tree, "dc.anchor.value.missing"); jjg@1495: } else { jjg@1495: if (!validName.matcher(value).matches()) { jjg@1495: env.messages.error(HTML, tree, "dc.invalid.anchor", value); jjg@1495: } jjg@1495: if (!foundAnchors.add(value)) { jjg@1495: env.messages.error(HTML, tree, "dc.anchor.already.defined", value); jjg@1495: } jjg@1455: } jjg@1455: break; jjg@1455: jjg@1455: case HREF: jjg@1455: if (currTag == HtmlTag.A) { jjg@1455: String v = getAttrValue(tree); jjg@1455: if (v == null || v.isEmpty()) { jjg@1455: env.messages.error(HTML, tree, "dc.attr.lacks.value"); jjg@1455: } else { jjg@1455: Matcher m = docRoot.matcher(v); jjg@1455: if (m.matches()) { jjg@1455: String rest = m.group(2); jjg@1455: if (!rest.isEmpty()) jjg@1455: checkURI(tree, rest); jjg@1455: } else { jjg@1455: checkURI(tree, v); jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: break; jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: // TODO: basic check on value jjg@1455: jjg@1455: return super.visitAttribute(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: // http://www.w3.org/TR/html401/types.html#type-name jjg@1455: private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*"); jjg@1455: jjg@1455: // pattern to remove leading {@docRoot}/? jjg@1455: private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)"); jjg@1455: jjg@1455: private String getAttrValue(AttributeTree tree) { jjg@1455: if (tree.getValue() == null) jjg@1455: return null; jjg@1455: jjg@1455: StringWriter sw = new StringWriter(); jjg@1455: try { jjg@1455: new DocPretty(sw).print(tree.getValue()); jjg@1455: } catch (IOException e) { jjg@1455: // cannot happen jjg@1455: } jjg@1455: // ignore potential use of entities for now jjg@1455: return sw.toString(); jjg@1455: } jjg@1455: jjg@1455: private void checkURI(AttributeTree tree, String uri) { jjg@1455: try { jjg@1455: URI u = new URI(uri); jjg@1455: } catch (URISyntaxException e) { jjg@1455: env.messages.error(HTML, tree, "dc.invalid.uri", uri); jjg@1455: } jjg@1455: } jjg@1455: // jjg@1455: jjg@1455: // jjg@1455: jjg@1455: @Override jjg@1455: public Void visitAuthor(AuthorTree tree, Void ignore) { jjg@1455: warnIfEmpty(tree, tree.getName()); jjg@1455: return super.visitAuthor(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitInheritDoc(InheritDocTree tree, Void ignore) { jjg@1455: // TODO: verify on overridden method jjg@1455: foundInheritDoc = true; jjg@1455: return super.visitInheritDoc(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitLink(LinkTree tree, Void ignore) { jjg@1455: // simulate inline context on tag stack jjg@1455: HtmlTag t = (tree.getKind() == DocTree.Kind.LINK) jjg@1455: ? HtmlTag.CODE : HtmlTag.SPAN; jjg@1455: tagStack.push(new TagStackItem(tree, t)); jjg@1455: try { jjg@1455: return super.visitLink(tree, ignore); jjg@1455: } finally { jjg@1455: tagStack.pop(); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1499: public Void visitLiteral(LiteralTree tree, Void ignore) { jjg@1499: if (tree.getKind() == DocTree.Kind.CODE) { jjg@1499: for (TagStackItem tsi: tagStack) { jjg@1499: if (tsi.tag == HtmlTag.CODE) { jjg@1502: env.messages.warning(HTML, tree, "dc.tag.code.within.code"); jjg@1499: break; jjg@1499: } jjg@1499: } jjg@1499: } jjg@1499: return super.visitLiteral(tree, ignore); jjg@1499: } jjg@1499: jjg@1499: @Override jjg@1455: public Void visitParam(ParamTree tree, Void ignore) { jjg@1455: boolean typaram = tree.isTypeParameter(); jjg@1455: IdentifierTree nameTree = tree.getName(); jjg@1455: Element e = env.currElement; jjg@1455: switch (e.getKind()) { jjg@1455: case METHOD: case CONSTRUCTOR: { jjg@1455: ExecutableElement ee = (ExecutableElement) e; jjg@1455: checkParamDeclared(nameTree, typaram ? ee.getTypeParameters() : ee.getParameters()); jjg@1455: break; jjg@1455: } jjg@1455: jjg@1455: case CLASS: case INTERFACE: { jjg@1455: TypeElement te = (TypeElement) e; jjg@1455: if (typaram) { jjg@1455: checkParamDeclared(nameTree, te.getTypeParameters()); jjg@1455: } else { jjg@1455: env.messages.error(REFERENCE, tree, "dc.invalid.param"); jjg@1455: } jjg@1455: break; jjg@1455: } jjg@1455: jjg@1455: default: jjg@1455: env.messages.error(REFERENCE, tree, "dc.invalid.param"); jjg@1455: break; jjg@1455: } jjg@1455: warnIfEmpty(tree, tree.getDescription()); jjg@1455: return super.visitParam(tree, ignore); jjg@1455: } jjg@1455: // where jjg@1455: private void checkParamDeclared(IdentifierTree nameTree, List list) { jjg@1455: Name name = nameTree.getName(); jjg@1455: boolean found = false; jjg@1455: for (Element e: list) { jjg@1455: if (name.equals(e.getSimpleName())) { jjg@1455: foundParams.add(e); jjg@1455: found = true; jjg@1455: } jjg@1455: } jjg@1455: if (!found) jjg@1455: env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found"); jjg@1455: } jjg@1455: jjg@1455: private void checkParamsDocumented(List list) { jjg@1455: if (foundInheritDoc) jjg@1455: return; jjg@1455: jjg@1455: for (Element e: list) { jjg@1455: if (!foundParams.contains(e)) { jjg@1455: CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER) jjg@1455: ? "<" + e.getSimpleName() + ">" jjg@1455: : e.getSimpleName(); jjg@1455: reportMissing("dc.missing.param", paramName); jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitReference(ReferenceTree tree, Void ignore) { jjg@1455: Element e = env.trees.getElement(env.currPath, tree); jjg@1455: if (e == null) jjg@1455: env.messages.error(REFERENCE, tree, "dc.ref.not.found"); jjg@1455: return super.visitReference(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitReturn(ReturnTree tree, Void ignore) { jjg@1455: Element e = env.trees.getElement(env.currPath); jjg@1455: if (e.getKind() != ElementKind.METHOD jjg@1455: || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID) jjg@1455: env.messages.error(REFERENCE, tree, "dc.invalid.return"); jjg@1455: foundReturn = true; jjg@1455: warnIfEmpty(tree, tree.getDescription()); jjg@1455: return super.visitReturn(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitSerialData(SerialDataTree tree, Void ignore) { jjg@1455: warnIfEmpty(tree, tree.getDescription()); jjg@1455: return super.visitSerialData(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitSerialField(SerialFieldTree tree, Void ignore) { jjg@1455: warnIfEmpty(tree, tree.getDescription()); jjg@1455: return super.visitSerialField(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitSince(SinceTree tree, Void ignore) { jjg@1455: warnIfEmpty(tree, tree.getBody()); jjg@1455: return super.visitSince(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitThrows(ThrowsTree tree, Void ignore) { jjg@1455: ReferenceTree exName = tree.getExceptionName(); jjg@1455: Element ex = env.trees.getElement(env.currPath, exName); jjg@1455: if (ex == null) { jjg@1455: env.messages.error(REFERENCE, tree, "dc.ref.not.found"); jjg@1455: } else if (ex.asType().getKind() == TypeKind.DECLARED jjg@1455: && env.types.isAssignable(ex.asType(), env.java_lang_Throwable)) { jjg@1455: switch (env.currElement.getKind()) { jjg@1455: case CONSTRUCTOR: jjg@1455: case METHOD: jjg@1455: if (isCheckedException(ex.asType())) { jjg@1455: ExecutableElement ee = (ExecutableElement) env.currElement; jjg@1455: checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes()); jjg@1455: } jjg@1455: break; jjg@1455: default: jjg@1455: env.messages.error(REFERENCE, tree, "dc.invalid.throws"); jjg@1455: } jjg@1455: } else { jjg@1455: env.messages.error(REFERENCE, tree, "dc.invalid.throws"); jjg@1455: } jjg@1455: warnIfEmpty(tree, tree.getDescription()); jjg@1455: return scan(tree.getDescription(), ignore); jjg@1455: } jjg@1455: jjg@1455: private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List list) { jjg@1455: boolean found = false; jjg@1455: for (TypeMirror tl : list) { jjg@1455: if (env.types.isAssignable(t, tl)) { jjg@1455: foundThrows.add(tl); jjg@1455: found = true; jjg@1455: } jjg@1455: } jjg@1455: if (!found) jjg@1455: env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t); jjg@1455: } jjg@1455: jjg@1455: private void checkThrowsDocumented(List list) { jjg@1455: if (foundInheritDoc) jjg@1455: return; jjg@1455: jjg@1455: for (TypeMirror tl: list) { jjg@1455: if (isCheckedException(tl) && !foundThrows.contains(tl)) jjg@1455: reportMissing("dc.missing.throws", tl); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitVersion(VersionTree tree, Void ignore) { jjg@1455: warnIfEmpty(tree, tree.getBody()); jjg@1455: return super.visitVersion(tree, ignore); jjg@1455: } jjg@1455: jjg@1455: @Override jjg@1455: public Void visitErroneous(ErroneousTree tree, Void ignore) { jjg@1455: env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null)); jjg@1455: return null; jjg@1455: } jjg@1455: // jjg@1455: jjg@1455: // jjg@1455: jjg@1455: private boolean isCheckedException(TypeMirror t) { jjg@1455: return !(env.types.isAssignable(t, env.java_lang_Error) jjg@1455: || env.types.isAssignable(t, env.java_lang_RuntimeException)); jjg@1455: } jjg@1455: jjg@1455: private boolean isSynthetic() { jjg@1455: switch (env.currElement.getKind()) { jjg@1455: case CONSTRUCTOR: jjg@1455: // A synthetic default constructor has the same pos as the jjg@1455: // enclosing class jjg@1455: TreePath p = env.currPath; jjg@1455: return env.getPos(p) == env.getPos(p.getParentPath()); jjg@1455: } jjg@1455: return false; jjg@1455: } jjg@1455: jjg@1455: void markEnclosingTag(Flag flag) { jjg@1455: TagStackItem top = tagStack.peek(); jjg@1455: if (top != null) jjg@1455: top.flags.add(flag); jjg@1455: } jjg@1455: jjg@1455: String toString(TreePath p) { jjg@1455: StringBuilder sb = new StringBuilder("TreePath["); jjg@1455: toString(p, sb); jjg@1455: sb.append("]"); jjg@1455: return sb.toString(); jjg@1455: } jjg@1455: jjg@1455: void toString(TreePath p, StringBuilder sb) { jjg@1455: TreePath parent = p.getParentPath(); jjg@1455: if (parent != null) { jjg@1455: toString(parent, sb); jjg@1455: sb.append(","); jjg@1455: } jjg@1455: sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p)); jjg@1455: } jjg@1455: jjg@1455: void warnIfEmpty(DocTree tree, List list) { jjg@1455: for (DocTree d: list) { jjg@1455: switch (d.getKind()) { jjg@1455: case TEXT: jjg@1507: if (hasNonWhitespace((TextTree) d)) jjg@1455: return; jjg@1455: break; jjg@1455: default: jjg@1455: return; jjg@1455: } jjg@1455: } jjg@1455: env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName); jjg@1455: } jjg@1507: jjg@1507: boolean hasNonWhitespace(TextTree tree) { jjg@1507: String s = tree.getBody(); jjg@1507: for (int i = 0; i < s.length(); i++) { jjg@1507: if (!Character.isWhitespace(s.charAt(i))) jjg@1507: return true; jjg@1507: } jjg@1507: return false; jjg@1507: } jjg@1507: jjg@1455: // jjg@1455: jjg@1455: }