
org.jdrupes.mdoclet.internal.doclint.Checker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doclet Show documentation
Show all versions of doclet Show documentation
A doclet for processing javadoc comments written with Markup
/*
* Copyright (c) 2012, 2023, 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.jdrupes.mdoclet.internal.doclint;
import static org.jdrupes.mdoclet.internal.doclint.Messages.Group.*;
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.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.lang.model.SourceVersion;
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.NestingKind;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic.Kind;
import org.jdrupes.mdoclet.internal.doclint.HtmlTag.AttrKind;
import org.jdrupes.mdoclet.internal.doclint.HtmlTag.ElemKind;
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.EscapeTree;
import com.sun.source.doctree.IdentifierTree;
import com.sun.source.doctree.IndexTree;
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.ProvidesTree;
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.SummaryTree;
import com.sun.source.doctree.SystemPropertyTree;
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.UsesTree;
import com.sun.source.doctree.ValueTree;
import com.sun.source.doctree.VersionTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTreePathScanner;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.DocPretty;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
/**
* Validate a doc comment.
*/
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;
boolean hasNonWhitespaceText = false;
public enum Flag {
TABLE_HAS_CAPTION,
TABLE_IS_PRESENTATION,
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 currHeadingTag;
private int implicitHeadingRank;
private boolean inIndex;
private boolean inLink;
private boolean inSummary;
//
Checker(Env env) {
this.env = Assert.checkNonNull(env);
tagStack = new LinkedList<>();
}
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 (isDefaultConstructor()) {
if (isNormalClass(p.getParentPath())) {
reportMissing("dc.default.constructor");
}
} else if (!isOverridingMethod && !isSynthetic()
&& !isAnonymous() && !isRecordComponentOrField()) {
reportMissing("dc.missing.comment");
}
return null;
} else if (tree.getFirstSentence().isEmpty() && !isOverridingMethod
&& !pseudoElement(p)) {
if (tree.getBlockTags().isEmpty()) {
reportMissing("dc.empty.comment");
return null;
} else {
// Don't report an empty description if the comment contains
// @deprecated,
// because javadoc will use the content of that tag in
// summary tables.
if (tree.getBlockTags().stream().allMatch(
t -> t.getKind() != DocTree.Kind.DEPRECATED)) {
env.messages.report(MISSING, Kind.WARNING, tree,
"dc.empty.main.description");
}
}
}
}
tagStack.clear();
currHeadingTag = null;
foundParams.clear();
foundThrows.clear();
foundInheritDoc = false;
foundReturn = false;
hasNonWhitespaceText = false;
implicitHeadingRank = switch (p.getLeaf().getKind()) {
// the following are for declarations that have their own top-level
// page,
// and so the doc comment comes after the page title.
case MODULE, PACKAGE, CLASS, INTERFACE, ENUM, ANNOTATION_TYPE, RECORD -> 1;
// this is for html files
// ... if it is a legacy package.html, the doc comment comes after the
// page title
// ... otherwise, (e.g. overview file and doc-files/**/*.html files) no
// additional headings are inserted
case COMPILATION_UNIT -> fo.isNameCompatible("package",
JavaFileObject.Kind.HTML) ? 1 : 0;
// the following are for member declarations, which appear in the page
// for the enclosing type, and so appear after the "Members"
// aggregate heading and the specific "Member signature" heading.
case METHOD, VARIABLE -> 3;
default -> throw new AssertionError(
"unexpected tree kind: " + p.getLeaf().getKind() + " " + fo);
};
scan(new DocTreePath(p, tree), null);
// the following checks are made after the scan, which will record
// @param tags
if (isDeclaredType()) {
TypeElement te = (TypeElement) env.currElement;
checkParamsDocumented(te.getTypeParameters());
checkParamsDocumented(te.getRecordComponents());
} else if (isExecutable()) {
if (!isOverridingMethod) {
ExecutableElement ee = (ExecutableElement) env.currElement;
if (!isCanonicalRecordConstructor(ee)) {
checkParamsDocumented(ee.getTypeParameters());
checkParamsDocumented(ee.getParameters());
}
switch (ee.getReturnType().getKind()) {
case VOID, NONE -> {
}
default -> {
if (!foundReturn
&& !foundInheritDoc
&& !env.types.isSameType(ee.getReturnType(),
env.java_lang_Void)) {
reportMissing("dc.missing.return");
}
}
}
checkThrowsDocumented(ee.getThrownTypes());
}
}
return null;
}
private boolean isCanonicalRecordConstructor(ExecutableElement ee) {
TypeElement te = (TypeElement) ee.getEnclosingElement();
if (te.getKind() != ElementKind.RECORD) {
return false;
}
List extends RecordComponentElement> stateComps
= te.getRecordComponents();
List extends VariableElement> params = ee.getParameters();
if (stateComps.size() != params.size()) {
return false;
}
Iterator extends RecordComponentElement> stateIter
= stateComps.iterator();
Iterator extends VariableElement> paramIter = params.iterator();
while (paramIter.hasNext() && stateIter.hasNext()) {
VariableElement param = paramIter.next();
RecordComponentElement comp = stateIter.next();
if (!Objects.equals(param.getSimpleName(), comp.getSimpleName())
|| !env.types.isSameType(param.asType(), comp.asType())) {
return false;
}
}
return true;
}
// Checks if the passed tree path corresponds to an entity, such as
// the overview file and doc-files/**/*.html files.
private boolean pseudoElement(TreePath p) {
return p.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT
&& p.getCompilationUnit().getSourceFile()
.getKind() == JavaFileObject.Kind.HTML;
}
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) {
scan(tree.getFirstSentence(), ignore);
scan(tree.getBody(), ignore);
checkTagStack();
for (DocTree blockTag : tree.getBlockTags()) {
tagStack.clear();
scan(blockTag, ignore);
checkTagStack();
}
return null;
}
private void checkTagStack() {
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());
}
}
}
//
//
@Override
@DefinedBy(Api.COMPILER_TREE)
public Void visitText(TextTree tree, Void ignore) {
hasNonWhitespaceText = hasNonWhitespace(tree);
if (hasNonWhitespaceText) {
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
}
return null;
}
@Override
@DefinedBy(Api.COMPILER_TREE)
public Void visitEntity(EntityTree tree, Void ignore) {
hasNonWhitespaceText = true;
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
String s = env.trees.getCharacters(tree);
if (s == null) {
env.messages.error(HTML, tree, "dc.entity.invalid", tree.getName());
}
return null;
}
@Override
@DefinedBy(Api.COMPILER_TREE)
public Void visitEscape(EscapeTree tree, Void ignore) {
hasNonWhitespaceText = true;
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
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.elemKind == ElemKind.HTML4) {
env.messages.error(HTML, tree, "dc.tag.not.supported.html5",
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 headings, such as ...
// ...
case H1, H2, H3, H4, H5, H6 -> checkHeading(tree, t);
}
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() && !isSelfClosingAllowed(t)) {
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);
}
case H1, H2, H3, H4, H5, H6 -> {
if (parent != null && (parent.tag == HtmlTag.SECTION
|| parent.tag == HtmlTag.ARTICLE)) {
parent.flags.add(Flag.HAS_HEADING);
}
}
case IMG -> {
if (!top.attrs.contains(HtmlTag.Attr.ALT))
env.messages.error(ACCESSIBILITY, tree,
"dc.no.alt.attr.for.image");
}
}
}
return null;
} finally {
if (t == null || t.endKind == HtmlTag.EndKind.NONE)
tagStack.pop();
}
}
// so-called "self-closing" tags are only permitted in HTML 5, for void
// elements
// https://html.spec.whatwg.org/multipage/syntax.html#start-tags
private boolean isSelfClosingAllowed(HtmlTag tag) {
return tag.endKind == HtmlTag.EndKind.NONE;
}
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();
// Links may use block display style so issue warning
// instead of error
if ("a".equalsIgnoreCase(name.toString())) {
env.messages.warning(HTML, tree,
"dc.tag.not.allowed.element.default.style",
treeName, name);
} else {
env.messages.error(HTML, tree,
"dc.tag.not.allowed.inline.element",
treeName, name);
}
return;
}
}
case LINK, LINK_PLAIN -> {
String name = top.tree.getKind().tagName;
env.messages.warning(HTML, tree,
"dc.tag.not.allowed.tag.default.style",
treeName, name);
return;
}
}
}
case INLINE -> {
if (top == null || top.tag.accepts(t))
return;
}
case LIST_ITEM, 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;
}
}
case OTHER -> {
switch (t) {
case SCRIPT -> {
//