001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.api.DetailNode;
023import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
026
027/**
028 * Checks that the at-clause tag is followed by description .
029 * Default configuration that will check {@code @param}, {@code @return},
030 * {@code @throws}, {@code @deprecated} to:
031 * <pre>
032 * &lt;module name=&quot;NonEmptyAtclauseDescription&quot;/&gt;
033 * </pre>
034 * <p>
035 * To check non-empty at-clause description for tags {@code @throws},
036 * {@code @deprecated}, use following configuration:
037 * </p>
038 * <pre>
039 * &lt;module name=&quot;NonEmptyAtclauseDescription&quot;&gt;
040 *     &lt;property name=&quot;target&quot; value=&quot;JAVADOC_TAG_THROWS_LITERAL,
041 *     JAVADOC_TAG_DEPRECATED_LITERAL&quot;/&gt;
042 * &lt;/module&gt;
043 * </pre>
044 *
045 * @author maxvetrenko
046 *
047 */
048public class NonEmptyAtclauseDescriptionCheck extends AbstractJavadocCheck {
049
050    /**
051     * A key is pointing to the warning message text in "messages.properties"
052     * file.
053     */
054    public static final String MSG_KEY = "non.empty.atclause";
055
056    @Override
057    public int[] getDefaultJavadocTokens() {
058        return new int[] {
059            JavadocTokenTypes.PARAM_LITERAL,
060            JavadocTokenTypes.RETURN_LITERAL,
061            JavadocTokenTypes.THROWS_LITERAL,
062            JavadocTokenTypes.DEPRECATED_LITERAL,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN };
069    }
070
071    @Override
072    public int[] getRequiredTokens() {
073        return getAcceptableTokens();
074    }
075
076    @Override
077    public void visitJavadocToken(DetailNode ast) {
078        if (isEmptyTag(ast.getParent())) {
079            log(ast.getLineNumber(), MSG_KEY, ast.getText());
080        }
081    }
082
083    /**
084     * Tests if at-clause tag is empty.
085     * @param tagNode at-clause tag.
086     * @return true, if at-clause tag is empty.
087     */
088    private static boolean isEmptyTag(DetailNode tagNode) {
089        final DetailNode tagDescription =
090                JavadocUtils.findFirstToken(tagNode, JavadocTokenTypes.DESCRIPTION);
091        return tagDescription == null;
092    }
093}