001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.FileContents;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
030
031/**
032 * <p>
033 * Checks for missing Javadoc comments in package-info.java files.
034 * </p>
035 * <p>
036 * Rationale: description and other related documentation for a package can be written up
037 * in the package-info.java file and it gets used in the production of the Javadocs.
038 * See <a
039 * href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#packagecomment"
040 * >link</a> for more info.
041 * </p>
042 * <p>
043 * To configure the check:
044 * </p>
045 * <pre>
046 * &lt;module name="MissingJavadocPackage"/&gt;
047 * </pre>
048 * <p>Example:</p>
049 * <pre>
050 * /**
051 * * Provides API classes
052 * *&#47;
053 * package com.checkstyle.api; // no violation
054 * /*
055 * * Block comment is not a javadoc
056 * *&#47;
057 * package com.checkstyle.api; // violation
058 * </pre>
059 *
060 * @since 8.22
061 */
062@StatelessCheck
063public class MissingJavadocPackageCheck extends AbstractCheck {
064
065    /**
066     * A key is pointing to the warning message text in "messages.properties"
067     * file.
068     */
069    public static final String MSG_PKG_JAVADOC_MISSING = "package.javadoc.missing";
070
071    @Override
072    public int[] getDefaultTokens() {
073        return getRequiredTokens();
074    }
075
076    @Override
077    public int[] getAcceptableTokens() {
078        return getRequiredTokens();
079    }
080
081    @Override
082    public int[] getRequiredTokens() {
083        return new int[] {TokenTypes.PACKAGE_DEF};
084    }
085
086    @Override
087    public boolean isCommentNodesRequired() {
088        return true;
089    }
090
091    @Override
092    public void visitToken(DetailAST ast) {
093        final FileContents contents = getFileContents();
094        if (contents.inPackageInfo() && !hasJavadoc(ast)) {
095            log(ast.getLineNo(), MSG_PKG_JAVADOC_MISSING);
096        }
097    }
098
099    /**
100     * Checks that there is javadoc before ast.
101     * Because of <a href="https://github.com/checkstyle/checkstyle/issues/4392">parser bug</a>
102     * parser can place javadoc comment either as previous sibling of package definition
103     * or (if there is annotation between package def and javadoc) inside package definition tree.
104     * So we should look for javadoc in both places.
105     *
106     * @param ast {@link TokenTypes#PACKAGE_DEF} token to check
107     * @return true if there is javadoc, false otherwise
108     */
109    private static boolean hasJavadoc(DetailAST ast) {
110        final boolean hasJavadocBefore = ast.getPreviousSibling() != null
111            && isJavadoc(ast.getPreviousSibling());
112        return hasJavadocBefore || hasJavadocAboveAnnotation(ast);
113    }
114
115    /**
116     * Checks javadoc existence in annotations list.
117     *
118     * @param ast package def
119     * @return true if there is a javadoc, false otherwise
120     */
121    private static boolean hasJavadocAboveAnnotation(DetailAST ast) {
122        final Optional<DetailAST> firstAnnotationChild = Optional.of(ast.getFirstChild())
123            .map(DetailAST::getFirstChild)
124            .map(DetailAST::getFirstChild);
125        boolean result = false;
126        if (firstAnnotationChild.isPresent()) {
127            for (DetailAST child = firstAnnotationChild.get(); child != null;
128                 child = child.getNextSibling()) {
129                if (isJavadoc(child)) {
130                    result = true;
131                    break;
132                }
133            }
134        }
135        return result;
136    }
137
138    /**
139     * Checks that ast is a javadoc comment.
140     * @param ast token to check
141     * @return true if ast is a javadoc comment, false otherwise
142     */
143    private static boolean isJavadoc(DetailAST ast) {
144        return ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(ast);
145    }
146}