All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openjdk.tools.doclint.Checker Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2012, 2017, 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 org.openjdk.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 org.openjdk.source.doctree.AttributeTree;
import org.openjdk.source.doctree.AuthorTree;
import org.openjdk.source.doctree.DocCommentTree;
import org.openjdk.source.doctree.DocRootTree;
import org.openjdk.source.doctree.DocTree;
import org.openjdk.source.doctree.EndElementTree;
import org.openjdk.source.doctree.EntityTree;
import org.openjdk.source.doctree.ErroneousTree;
import org.openjdk.source.doctree.IdentifierTree;
import org.openjdk.source.doctree.InheritDocTree;
import org.openjdk.source.doctree.LinkTree;
import org.openjdk.source.doctree.LiteralTree;
import org.openjdk.source.doctree.ParamTree;
import org.openjdk.source.doctree.ProvidesTree;
import org.openjdk.source.doctree.ReferenceTree;
import org.openjdk.source.doctree.ReturnTree;
import org.openjdk.source.doctree.SerialDataTree;
import org.openjdk.source.doctree.SerialFieldTree;
import org.openjdk.source.doctree.SinceTree;
import org.openjdk.source.doctree.StartElementTree;
import org.openjdk.source.doctree.TextTree;
import org.openjdk.source.doctree.ThrowsTree;
import org.openjdk.source.doctree.UnknownBlockTagTree;
import org.openjdk.source.doctree.UnknownInlineTagTree;
import org.openjdk.source.doctree.UsesTree;
import org.openjdk.source.doctree.ValueTree;
import org.openjdk.source.doctree.VersionTree;
import org.openjdk.source.tree.Tree;
import org.openjdk.source.util.DocTreePath;
import org.openjdk.source.util.DocTreePathScanner;
import org.openjdk.source.util.TreePath;
import org.openjdk.tools.doclint.HtmlTag.AttrKind;
import org.openjdk.tools.javac.tree.DocPretty;
import org.openjdk.tools.javac.util.Assert;
import org.openjdk.tools.javac.util.DefinedBy;
import org.openjdk.tools.javac.util.DefinedBy.Api;
import org.openjdk.tools.javac.util.StringUtils;

import static org.openjdk.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_HEADING, 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) { this.env = Assert.checkNonNull(env); tagStack = new LinkedList<>(); implicitHeaderLevel = env.implicitHeaderLevel; } public Void scan(DocCommentTree tree, TreePath p) { env.initTypes(); env.setCurrent(p, tree); boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty(); JavaFileObject fo = p.getCompilationUnit().getSourceFile(); if (p.getLeaf().getKind() == Tree.Kind.PACKAGE) { // If p points to a package, 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. 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 && fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) { // a package.html file with a DocCommentTree if (tree.getFullBody().isEmpty()) { reportMissing("dc.missing.comment"); return null; } } 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 @DefinedBy(Api.COMPILER_TREE) 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 @DefinedBy(Api.COMPILER_TREE) public Void visitText(TextTree tree, Void ignore) { if (hasNonWhitespace(tree)) { checkAllowsText(tree); markEnclosingTag(Flag.HAS_TEXT); } return null; } @Override @DefinedBy(Api.COMPILER_TREE) 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 @DefinedBy(Api.COMPILER_TREE) 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 if (t.allowedVersion != HtmlVersion.ALL && t.allowedVersion != env.htmlVersion) { env.messages.error(HTML, tree, "dc.tag.not.supported", 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 H1: case H2: case H3: case H4: case H5: case H6: if (parent != null && (parent.tag == HtmlTag.SECTION || parent.tag == HtmlTag.ARTICLE)) { parent.flags.add(Flag.HAS_HEADING); } 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: //