001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.javadoc;
021
022import java.util.Optional;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
029import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
030
031/**
032 * <div>
033 * Checks for missing package definition Javadoc comments in package-info.java files.
034 * </div>
035 *
036 * <p>
037 * Rationale: description and other related documentation for a package can be written up
038 * in the package-info.java file and it gets used in the production of the Javadocs.
039 * See <a
040 * href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#packagecomment"
041 * >link</a> for more info.
042 * </p>
043 *
044 * <p>
045 * This check specifically only validates package definitions. It will not validate any methods or
046 * interfaces declared in the package-info.java file.
047 * </p>
048 *
049 * @since 8.22
050 */
051@StatelessCheck
052public class MissingJavadocPackageCheck extends AbstractCheck {
053
054    /**
055     * A key is pointing to the warning message text in "messages.properties"
056     * file.
057     */
058    public static final String MSG_PKG_JAVADOC_MISSING = "package.javadoc.missing";
059
060    @Override
061    public int[] getDefaultTokens() {
062        return getRequiredTokens();
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return getRequiredTokens();
068    }
069
070    @Override
071    public int[] getRequiredTokens() {
072        return new int[] {TokenTypes.PACKAGE_DEF};
073    }
074
075    @Override
076    public boolean isCommentNodesRequired() {
077        return true;
078    }
079
080    @Override
081    public void visitToken(DetailAST ast) {
082        if (CheckUtil.isPackageInfo(getFilePath()) && !hasJavadoc(ast)) {
083            log(ast, MSG_PKG_JAVADOC_MISSING);
084        }
085    }
086
087    /**
088     * Checks that there is javadoc before ast.
089     * Because of <a href="https://github.com/checkstyle/checkstyle/issues/4392">parser bug</a>
090     * parser can place javadoc comment either as previous sibling of package definition
091     * or (if there is annotation between package def and javadoc) inside package definition tree.
092     * So we should look for javadoc in both places.
093     *
094     * @param ast {@link TokenTypes#PACKAGE_DEF} token to check
095     * @return true if there is javadoc, false otherwise
096     */
097    private static boolean hasJavadoc(DetailAST ast) {
098        final boolean hasJavadocBefore = ast.getPreviousSibling() != null
099            && isJavadoc(ast.getPreviousSibling());
100        return hasJavadocBefore || hasJavadocAboveAnnotation(ast);
101    }
102
103    /**
104     * Checks javadoc existence in annotations list.
105     *
106     * @param ast package def
107     * @return true if there is a javadoc, false otherwise
108     */
109    private static boolean hasJavadocAboveAnnotation(DetailAST ast) {
110        final Optional<DetailAST> firstAnnotationChild = Optional.of(ast.getFirstChild())
111            .map(DetailAST::getFirstChild)
112            .map(DetailAST::getFirstChild);
113        boolean result = false;
114        if (firstAnnotationChild.isPresent()) {
115            for (DetailAST child = firstAnnotationChild.orElseThrow(); child != null;
116                 child = child.getNextSibling()) {
117                if (isJavadoc(child)) {
118                    result = true;
119                    break;
120                }
121            }
122        }
123        return result;
124    }
125
126    /**
127     * Checks that ast is a javadoc comment.
128     *
129     * @param ast token to check
130     * @return true if ast is a javadoc comment, false otherwise
131     */
132    private static boolean isJavadoc(DetailAST ast) {
133        return ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(ast);
134    }
135}