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@1650: import java.io.IOException;
jjg@1650: import java.io.StringWriter;
jjg@1455: import java.net.URI;
jjg@1650: import java.net.URISyntaxException;
jjg@1455: import java.util.Deque;
jjg@1455: import java.util.EnumSet;
jjg@1912: import java.util.HashMap;
jjg@1455: import java.util.HashSet;
jjg@1455: import java.util.LinkedList;
jjg@1455: import java.util.List;
jjg@1912: import java.util.Map;
jjg@1455: import java.util.Set;
jjg@1650: import java.util.regex.Matcher;
jjg@1650: import java.util.regex.Pattern;
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@2053: import javax.lang.model.element.VariableElement;
jjg@1455: import javax.lang.model.type.TypeKind;
jjg@1455: import javax.lang.model.type.TypeMirror;
jjg@1455: import javax.tools.Diagnostic.Kind;
jjg@1912: import javax.tools.JavaFileObject;
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@1650: import com.sun.source.doctree.DocRootTree;
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@1650: import com.sun.source.doctree.LinkTree;
jjg@1650: import com.sun.source.doctree.LiteralTree;
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;
bpatel@2169: import com.sun.source.doctree.UnknownBlockTagTree;
bpatel@2169: import com.sun.source.doctree.UnknownInlineTagTree;
jjg@1650: import com.sun.source.doctree.ValueTree;
jjg@1455: import com.sun.source.doctree.VersionTree;
jlahoda@1726: import com.sun.source.util.DocTreePath;
jlahoda@1726: import com.sun.source.util.DocTreePathScanner;
jjg@1455: import com.sun.source.util.TreePath;
jjg@1455: import com.sun.tools.doclint.HtmlTag.AttrKind;
jjg@1650: import com.sun.tools.javac.tree.DocPretty;
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: */
jlahoda@1726: public class Checker extends DocTreePathScanner {
jjg@1455: final Env env;
jjg@1455:
jjg@1912: Set foundParams = new HashSet<>();
jjg@1912: Set foundThrows = new HashSet<>();
jjg@2052: Map> foundAnchors = new HashMap<>();
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@1650: HAS_INLINE_TAG,
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@1668: private final int implicitHeaderLevel;
jjg@1668:
jjg@1455: //
jjg@1455:
jjg@1455: Checker(Env env) {
jjg@1455: env.getClass();
jjg@1455: this.env = env;
jjg@1912: tagStack = new LinkedList<>();
jjg@1668: implicitHeaderLevel = env.implicitHeaderLevel;
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@1915: if (p.getLeaf() == p.getCompilationUnit()) {
jjg@1915: // If p points to a compilation unit, the implied declaration is the
jjg@1915: // package declaration (if any) for the compilation unit.
jjg@1915: // Handle this case specially, because doc comments are only
jjg@1915: // expected in package-info files.
jjg@1915: JavaFileObject fo = p.getCompilationUnit().getSourceFile();
jjg@1915: boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
jjg@1915: if (tree == null) {
jjg@1915: if (isPkgInfo)
jjg@1915: reportMissing("dc.missing.comment");
jjg@1915: return null;
jjg@1915: } else {
jjg@1915: if (!isPkgInfo)
jjg@1915: reportReference("dc.unexpected.comment");
jjg@1915: }
jjg@1915: } else {
jjg@1915: if (tree == null) {
jjg@1915: if (!isSynthetic() && !isOverridingMethod)
jjg@1915: reportMissing("dc.missing.comment");
jjg@1915: return null;
jjg@1915: }
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:
jlahoda@1726: scan(new DocTreePath(p, tree), 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@1915: private void reportReference(String code, Object... args) {
jjg@1915: env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args);
jjg@1915: }
jjg@1915:
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@2110: warnIfEmpty(tsi, null);
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: 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@2110: while (tagStack.peek() != tsi) {
jjg@2110: warnIfEmpty(tagStack.peek(), null);
jjg@2110: tagStack.pop();
jjg@2110: }
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@2110: while (!tagStack.isEmpty()) {
jjg@2110: warnIfEmpty(tagStack.peek(), null);
jjg@2110: tagStack.pop();
jjg@2110: }
jjg@1507: }
jjg@1507:
jjg@2110: markEnclosingTag(Flag.HAS_ELEMENT);
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@1668: return implicitHeaderLevel;
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@2110: warnIfEmpty(top, tree);
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@2110:
jjg@2110: void warnIfEmpty(TagStackItem tsi, DocTree endTree) {
jjg@2110: if (tsi.tag != null && tsi.tree instanceof StartElementTree) {
jjg@2110: if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
jjg@2110: && !tsi.flags.contains(Flag.HAS_TEXT)
jjg@2110: && !tsi.flags.contains(Flag.HAS_ELEMENT)
jjg@2110: && !tsi.flags.contains(Flag.HAS_INLINE_TAG)) {
jjg@2110: DocTree tree = (endTree != null) ? endTree : tsi.tree;
jjg@2110: Name treeName = ((StartElementTree) tsi.tree).getName();
jjg@2110: env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
jjg@2110: }
jjg@2110: }
jjg@2110: }
jjg@2110:
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@1912: if (!checkAnchor(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@1793:
jjg@1793: case VALUE:
jjg@1793: if (currTag == HtmlTag.LI) {
jjg@1793: String v = getAttrValue(tree);
jjg@1793: if (v == null || v.isEmpty()) {
jjg@1793: env.messages.error(HTML, tree, "dc.attr.lacks.value");
jjg@1793: } else if (!validNumber.matcher(v).matches()) {
jjg@1793: env.messages.error(HTML, tree, "dc.attr.not.number");
jjg@1793: }
jjg@1793: }
jjg@1793: 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@1912: private boolean checkAnchor(String name) {
jjg@2052: Element e = getEnclosingPackageOrClass(env.currElement);
jjg@2052: if (e == null)
jjg@2052: return true;
jjg@2052: Set set = foundAnchors.get(e);
jjg@1912: if (set == null)
jjg@2052: foundAnchors.put(e, set = new HashSet<>());
jjg@1912: return set.add(name);
jjg@1912: }
jjg@1912:
jjg@2052: private Element getEnclosingPackageOrClass(Element e) {
jjg@2052: while (e != null) {
jjg@2052: switch (e.getKind()) {
jjg@2052: case CLASS:
jjg@2052: case ENUM:
jjg@2052: case INTERFACE:
jjg@2052: case PACKAGE:
jjg@2052: return e;
jjg@2052: default:
jjg@2052: e = e.getEnclosingElement();
jjg@2052: }
jjg@2052: }
jjg@2052: return e;
jjg@2052: }
jjg@2052:
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@1793: private static final Pattern validNumber = Pattern.compile("-?[0-9]+");
jjg@1793:
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@1650: public Void visitDocRoot(DocRootTree tree, Void ignore) {
jjg@1650: markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1650: return super.visitDocRoot(tree, ignore);
jjg@1650: }
jjg@1650:
jjg@1650: @Override
jjg@1455: public Void visitInheritDoc(InheritDocTree tree, Void ignore) {
jjg@1650: markEnclosingTag(Flag.HAS_INLINE_TAG);
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@1650: markEnclosingTag(Flag.HAS_INLINE_TAG);
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@1650: markEnclosingTag(Flag.HAS_INLINE_TAG);
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
jlahoda@1726: @SuppressWarnings("fallthrough")
jjg@1455: public Void visitParam(ParamTree tree, Void ignore) {
jjg@1455: boolean typaram = tree.isTypeParameter();
jjg@1455: IdentifierTree nameTree = tree.getName();
jlahoda@1726: Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;
jlahoda@1726:
jlahoda@1726: if (paramElement == null) {
jlahoda@1726: switch (env.currElement.getKind()) {
jlahoda@1726: case CLASS: case INTERFACE: {
jlahoda@1726: if (!typaram) {
jlahoda@1726: env.messages.error(REFERENCE, tree, "dc.invalid.param");
jlahoda@1726: break;
jlahoda@1726: }
jlahoda@1726: }
jlahoda@1726: case METHOD: case CONSTRUCTOR: {
jlahoda@1726: env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
jlahoda@1726: break;
jlahoda@1726: }
jlahoda@1726:
jlahoda@1726: default:
jlahoda@1726: env.messages.error(REFERENCE, tree, "dc.invalid.param");
jlahoda@1726: break;
jjg@1455: }
jlahoda@1726: } else {
jlahoda@1726: foundParams.add(paramElement);
jlahoda@1726: }
jjg@1455:
jjg@1455: warnIfEmpty(tree, tree.getDescription());
jjg@1455: return super.visitParam(tree, ignore);
jjg@1455: }
jjg@1455:
jjg@1455: private void checkParamsDocumented(List extends Element> 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@2054: String sig = tree.getSignature();
jjg@2054: if (sig.contains("<") || sig.contains(">"))
jjg@2054: env.messages.error(REFERENCE, tree, "dc.type.arg.not.allowed");
jjg@2054:
jlahoda@1726: Element e = env.trees.getElement(getCurrentPath());
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();
jlahoda@1726: Element ex = env.trees.getElement(new DocTreePath(getCurrentPath(), exName));
jjg@1455: if (ex == null) {
jjg@1455: env.messages.error(REFERENCE, tree, "dc.ref.not.found");
jjg@1917: } else if (isThrowable(ex.asType())) {
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@1917: private boolean isThrowable(TypeMirror tm) {
jjg@1917: switch (tm.getKind()) {
jjg@1917: case DECLARED:
jjg@1917: case TYPEVAR:
jjg@1917: return env.types.isAssignable(tm, env.java_lang_Throwable);
jjg@1917: }
jjg@1917: return false;
jjg@1917: }
jjg@1917:
jjg@1455: private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List extends TypeMirror> 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 extends TypeMirror> 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
bpatel@2169: public Void visitUnknownBlockTag(UnknownBlockTagTree tree, Void ignore) {
bpatel@2169: checkUnknownTag(tree, tree.getTagName());
bpatel@2169: return super.visitUnknownBlockTag(tree, ignore);
bpatel@2169: }
bpatel@2169:
bpatel@2169: @Override
bpatel@2169: public Void visitUnknownInlineTag(UnknownInlineTagTree tree, Void ignore) {
bpatel@2169: checkUnknownTag(tree, tree.getTagName());
bpatel@2169: return super.visitUnknownInlineTag(tree, ignore);
bpatel@2169: }
bpatel@2169:
bpatel@2169: private void checkUnknownTag(DocTree tree, String tagName) {
bpatel@2169: if (env.customTags != null && !env.customTags.contains(tagName))
bpatel@2169: env.messages.error(SYNTAX, tree, "dc.tag.unknown", tagName);
bpatel@2169: }
bpatel@2169:
bpatel@2169: @Override
jjg@1650: public Void visitValue(ValueTree tree, Void ignore) {
jjg@2053: ReferenceTree ref = tree.getReference();
jjg@2053: if (ref == null || ref.getSignature().isEmpty()) {
jjg@2053: if (!isConstant(env.currElement))
jjg@2053: env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here");
jjg@2053: } else {
jjg@2053: Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref));
jjg@2053: if (!isConstant(e))
jjg@2053: env.messages.error(REFERENCE, tree, "dc.value.not.a.constant");
jjg@2053: }
jjg@2053:
jjg@1650: markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1650: return super.visitValue(tree, ignore);
jjg@1650: }
jjg@1650:
jjg@2053: private boolean isConstant(Element e) {
jjg@2053: if (e == null)
jjg@2053: return false;
jjg@2053:
jjg@2053: switch (e.getKind()) {
jjg@2053: case FIELD:
jjg@2053: Object value = ((VariableElement) e).getConstantValue();
jjg@2053: return (value != null); // can't distinguish "not a constant" from "constant is null"
jjg@2053: default:
jjg@2053: return false;
jjg@2053: }
jjg@2053: }
jjg@2053:
jjg@1650: @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 extends DocTree> 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: }