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

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

*/ public class Checker extends DocTreePathScanner { final Env env; Set foundParams = new HashSet<>(); Set foundThrows = new HashSet<>(); Map> foundAnchors = new HashMap<>(); boolean foundInheritDoc = false; boolean foundReturn = false; public enum Flag { TABLE_HAS_CAPTION, HAS_ELEMENT, HAS_INLINE_TAG, HAS_TEXT, REPORTED_BAD_INLINE } static class TagStackItem { final DocTree tree; // typically, but not always, StartElementTree final HtmlTag tag; final Set attrs; final Set flags; TagStackItem(DocTree tree, HtmlTag tag) { this.tree = tree; this.tag = tag; attrs = EnumSet.noneOf(HtmlTag.Attr.class); flags = EnumSet.noneOf(Flag.class); } @Override public String toString() { return String.valueOf(tag); } } private final Deque tagStack; // TODO: maybe want to record starting tree as well private HtmlTag currHeaderTag; private final int implicitHeaderLevel; // Checker(Env env) { env.getClass(); this.env = env; tagStack = new LinkedList<>(); implicitHeaderLevel = env.implicitHeaderLevel; } public Void scan(DocCommentTree tree, TreePath p) { env.setCurrent(p, tree); boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty(); if (p.getLeaf() == p.getCompilationUnit()) { // If p points to a compilation unit, the implied declaration is the // package declaration (if any) for the compilation unit. // Handle this case specially, because doc comments are only // expected in package-info files. JavaFileObject fo = p.getCompilationUnit().getSourceFile(); boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); if (tree == null) { if (isPkgInfo) reportMissing("dc.missing.comment"); return null; } else { if (!isPkgInfo) reportReference("dc.unexpected.comment"); } } else { if (tree == null) { if (!isSynthetic() && !isOverridingMethod) reportMissing("dc.missing.comment"); return null; } } tagStack.clear(); currHeaderTag = null; foundParams.clear(); foundThrows.clear(); foundInheritDoc = false; foundReturn = false; scan(new DocTreePath(p, tree), null); if (!isOverridingMethod) { switch (env.currElement.getKind()) { case METHOD: case CONSTRUCTOR: { ExecutableElement ee = (ExecutableElement) env.currElement; checkParamsDocumented(ee.getTypeParameters()); checkParamsDocumented(ee.getParameters()); switch (ee.getReturnType().getKind()) { case VOID: case NONE: break; default: if (!foundReturn && !foundInheritDoc && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) { reportMissing("dc.missing.return"); } } checkThrowsDocumented(ee.getThrownTypes()); } } } return null; } private void reportMissing(String code, Object... args) { env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args); } private void reportReference(String code, Object... args) { env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args); } @Override public Void visitDocComment(DocCommentTree tree, Void ignore) { super.visitDocComment(tree, ignore); for (TagStackItem tsi: tagStack) { warnIfEmpty(tsi, null); if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) { StartElementTree t = (StartElementTree) tsi.tree; env.messages.error(HTML, t, "dc.tag.not.closed", t.getName()); } } return null; } // // @Override public Void visitText(TextTree tree, Void ignore) { if (hasNonWhitespace(tree)) { checkAllowsText(tree); markEnclosingTag(Flag.HAS_TEXT); } return null; } @Override public Void visitEntity(EntityTree tree, Void ignore) { checkAllowsText(tree); markEnclosingTag(Flag.HAS_TEXT); String name = tree.getName().toString(); if (name.startsWith("#")) { int v = StringUtils.toLowerCase(name).startsWith("#x") ? Integer.parseInt(name.substring(2), 16) : Integer.parseInt(name.substring(1), 10); if (!Entity.isValid(v)) { env.messages.error(HTML, tree, "dc.entity.invalid", name); } } else if (!Entity.isValid(name)) { env.messages.error(HTML, tree, "dc.entity.invalid", name); } return null; } void checkAllowsText(DocTree tree) { TagStackItem top = tagStack.peek(); if (top != null && top.tree.getKind() == DocTree.Kind.START_ELEMENT && !top.tag.acceptsText()) { if (top.flags.add(Flag.REPORTED_BAD_INLINE)) { env.messages.error(HTML, tree, "dc.text.not.allowed", ((StartElementTree) top.tree).getName()); } } } // // @Override public Void visitStartElement(StartElementTree tree, Void ignore) { final Name treeName = tree.getName(); final HtmlTag t = HtmlTag.get(treeName); if (t == null) { env.messages.error(HTML, tree, "dc.tag.unknown", treeName); } else { boolean done = false; for (TagStackItem tsi: tagStack) { if (tsi.tag.accepts(t)) { while (tagStack.peek() != tsi) { warnIfEmpty(tagStack.peek(), null); tagStack.pop(); } done = true; break; } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) { done = true; break; } } if (!done && HtmlTag.BODY.accepts(t)) { while (!tagStack.isEmpty()) { warnIfEmpty(tagStack.peek(), null); tagStack.pop(); } } markEnclosingTag(Flag.HAS_ELEMENT); checkStructure(tree, t); // tag specific checks switch (t) { // check for out of sequence headers, such as

...

...

case H1: case H2: case H3: case H4: case H5: case H6: checkHeader(tree, t); break; } if (t.flags.contains(HtmlTag.Flag.NO_NEST)) { for (TagStackItem i: tagStack) { if (t == i.tag) { env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName); break; } } } } // check for self closing tags, such as if (tree.isSelfClosing()) { env.messages.error(HTML, tree, "dc.tag.self.closing", treeName); } try { TagStackItem parent = tagStack.peek(); TagStackItem top = new TagStackItem(tree, t); tagStack.push(top); super.visitStartElement(tree, ignore); // handle attributes that may or may not have been found in start element if (t != null) { switch (t) { case CAPTION: if (parent != null && parent.tag == HtmlTag.TABLE) parent.flags.add(Flag.TABLE_HAS_CAPTION); break; case IMG: if (!top.attrs.contains(HtmlTag.Attr.ALT)) env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image"); break; } } return null; } finally { if (t == null || t.endKind == HtmlTag.EndKind.NONE) tagStack.pop(); } } private void checkStructure(StartElementTree tree, HtmlTag t) { Name treeName = tree.getName(); TagStackItem top = tagStack.peek(); switch (t.blockType) { case BLOCK: if (top == null || top.tag.accepts(t)) return; switch (top.tree.getKind()) { case START_ELEMENT: { if (top.tag.blockType == HtmlTag.BlockType.INLINE) { Name name = ((StartElementTree) top.tree).getName(); env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element", treeName, name); return; } } break; case LINK: case LINK_PLAIN: { String name = top.tree.getKind().tagName; env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag", treeName, name); return; } } break; case INLINE: if (top == null || top.tag.accepts(t)) return; break; case LIST_ITEM: case TABLE_ITEM: if (top != null) { // reset this flag so subsequent bad inline content gets reported top.flags.remove(Flag.REPORTED_BAD_INLINE); if (top.tag.accepts(t)) return; } break; case OTHER: switch (t) { case SCRIPT: //