001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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    /**
061     * Creates a new {@code MissingJavadocPackageCheck} instance.
062     */
063    public MissingJavadocPackageCheck() {
064        // no code by default
065    }
066
067    @Override
068    public int[] getDefaultTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getAcceptableTokens() {
074        return getRequiredTokens();
075    }
076
077    @Override
078    public int[] getRequiredTokens() {
079        return new int[] {TokenTypes.PACKAGE_DEF};
080    }
081
082    @Override
083    public boolean isCommentNodesRequired() {
084        return true;
085    }
086
087    @Override
088    public void visitToken(DetailAST ast) {
089        if (CheckUtil.isPackageInfo(getFilePath()) && !hasJavadoc(ast)) {
090            log(ast, MSG_PKG_JAVADOC_MISSING);
091        }
092    }
093
094    /**
095     * Checks that there is javadoc before ast.
096     * Because of <a href="https://github.com/checkstyle/checkstyle/issues/4392">parser bug</a>
097     * parser can place javadoc comment either as previous sibling of package definition
098     * or (if there is annotation between package def and javadoc) inside package definition tree.
099     * So we should look for javadoc in both places.
100     *
101     * @param ast {@link TokenTypes#PACKAGE_DEF} token to check
102     * @return true if there is javadoc, false otherwise
103     */
104    private static boolean hasJavadoc(DetailAST ast) {
105        final boolean hasJavadocBefore = ast.getPreviousSibling() != null
106            && isJavadoc(ast.getPreviousSibling());
107        return hasJavadocBefore || hasJavadocAboveAnnotation(ast);
108    }
109
110    /**
111     * Checks javadoc existence in annotations list.
112     *
113     * @param ast package def
114     * @return true if there is a javadoc, false otherwise
115     */
116    private static boolean hasJavadocAboveAnnotation(DetailAST ast) {
117        final Optional<DetailAST> firstAnnotationChild = Optional.of(ast.getFirstChild())
118            .map(DetailAST::getFirstChild)
119            .map(DetailAST::getFirstChild);
120        boolean result = false;
121        if (firstAnnotationChild.isPresent()) {
122            for (DetailAST child = firstAnnotationChild.orElseThrow(); child != null;
123                 child = child.getNextSibling()) {
124                if (isJavadoc(child)) {
125                    result = true;
126                    break;
127                }
128            }
129        }
130        return result;
131    }
132
133    /**
134     * Checks that ast is a javadoc comment.
135     *
136     * @param ast token to check
137     * @return true if ast is a javadoc comment, false otherwise
138     */
139    private static boolean isJavadoc(DetailAST ast) {
140        return ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(ast);
141    }
142
143}