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 com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailNode;
024import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
026
027/**
028 * <p>
029 * Checks that the at-clause tag is followed by description.
030 * </p>
031 * <ul>
032 * <li>
033 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
034 * if the Javadoc being examined by this check violates the tight html rules defined at
035 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
036 * Default value is {@code false}.
037 * </li>
038 * <li>
039 * Property {@code javadocTokens} - javadoc tokens to check
040 * Default value is
041 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#PARAM_LITERAL">
042 * PARAM_LITERAL</a>,
043 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#RETURN_LITERAL">
044 * RETURN_LITERAL</a>,
045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#THROWS_LITERAL">
046 * THROWS_LITERAL</a>,
047 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#EXCEPTION_LITERAL">
048 * EXCEPTION_LITERAL</a>,
049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html#DEPRECATED_LITERAL">
050 * DEPRECATED_LITERAL</a>.
051 * </li>
052 * </ul>
053 * <p>
054 * Default configuration that will check {@code @param}, {@code @return},
055 * {@code @throws}, {@code @deprecated}:
056 * </p>
057 * <pre>
058 * &lt;module name="NonEmptyAtclauseDescription"/&gt;
059 * </pre>
060 * <p>
061 * To configure the check to validate only {@code @param} and {@code @return} tags:
062 * </p>
063 * <pre>
064 * &lt;module name="NonEmptyAtclauseDescription"&gt;
065 *   &lt;property name="javadocTokens" value="PARAM_LITERAL,RETURN_LITERAL"/&gt;
066 * &lt;/module&gt;
067 * </pre>
068 *
069 * @since 6.0
070 */
071@StatelessCheck
072public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck {
073
074    /**
075     * A key is pointing to the warning message text in "messages.properties"
076     * file.
077     */
078    public static final String MSG_KEY = "non.empty.atclause";
079
080    @Override
081    public int[] getDefaultJavadocTokens() {
082        return new int[] {
083            JavadocTokenTypes.PARAM_LITERAL,
084            JavadocTokenTypes.RETURN_LITERAL,
085            JavadocTokenTypes.THROWS_LITERAL,
086            JavadocTokenTypes.EXCEPTION_LITERAL,
087            JavadocTokenTypes.DEPRECATED_LITERAL,
088        };
089    }
090
091    @Override
092    public void visitJavadocToken(DetailNode ast) {
093        if (isEmptyTag(ast.getParent())) {
094            log(ast.getLineNumber(), MSG_KEY, ast.getText());
095        }
096    }
097
098    /**
099     * Tests if at-clause tag is empty.
100     * @param tagNode at-clause tag.
101     * @return true, if at-clause tag is empty.
102     */
103    private static boolean isEmptyTag(DetailNode tagNode) {
104        final DetailNode tagDescription =
105                JavadocUtil.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION);
106        return tagDescription == null;
107    }
108
109}