001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025import java.util.stream.Collectors;
026
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.DetailNode;
029import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
030import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
032
033/**
034 * Checks that a JavaDoc block can fit on a single line and doesn't
035 * contain at-clauses. Javadoc comment that contains at least one at-clause
036 * should be formatted in a few lines.<br>
037 * All inline at-clauses are ignored by default.
038 *
039 * <p>The Check has the following properties:
040 * <br><b>ignoredTags</b> - allows to specify a list of at-clauses
041 * (including custom at-clauses) to be ignored by the check.
042 * <br><b>ignoreInlineTags</b> - whether inline at-clauses must be ignored.
043 * </p>
044 *
045 * <p>Default configuration:
046 * <pre>
047 * &lt;module name=&quot;SingleLineJavadoc&quot;/&gt;
048 * </pre>
049 * To specify a list of ignored at-clauses and make inline at-clauses not ignored in general:
050 * <pre>
051 * &lt;module name=&quot;SingleLineJavadoc&quot;&gt;
052 *     &lt;property name=&quot;ignoredTags&quot; value=&quot;&#64;inheritDoc, &#64;link&quot;/&gt;
053 *     &lt;property name=&quot;ignoreInlineTags&quot; value=&quot;false&quot;/&gt;
054 * &lt;/module&gt;
055 * </pre>
056 *
057 * @author baratali
058 * @author maxvetrenko
059 * @author vladlis
060 *
061 */
062public class SingleLineJavadocCheck extends AbstractJavadocCheck {
063
064    /**
065     * A key is pointing to the warning message text in "messages.properties"
066     * file.
067     */
068    public static final String MSG_KEY = "singleline.javadoc";
069
070    /**
071     * Allows to specify a list of tags to be ignored by check.
072     */
073    private List<String> ignoredTags = new ArrayList<>();
074
075    /** Whether inline tags must be ignored. **/
076    private boolean ignoreInlineTags = true;
077
078    /**
079     * Sets a list of tags to be ignored by check.
080     *
081     * @param tags to be ignored by check.
082     */
083    public void setIgnoredTags(String... tags) {
084        ignoredTags = Arrays.stream(tags).collect(Collectors.toList());
085    }
086
087    /**
088     * Sets whether inline tags must be ignored.
089     *
090     * @param ignoreInlineTags whether inline tags must be ignored.
091     */
092    public void setIgnoreInlineTags(boolean ignoreInlineTags) {
093        this.ignoreInlineTags = ignoreInlineTags;
094    }
095
096    @Override
097    public int[] getDefaultJavadocTokens() {
098        return new int[] {
099            JavadocTokenTypes.JAVADOC,
100        };
101    }
102
103    @Override
104    public int[] getRequiredJavadocTokens() {
105        return getAcceptableJavadocTokens();
106    }
107
108    @Override
109    public int[] getAcceptableTokens() {
110        return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN };
111    }
112
113    @Override
114    public int[] getRequiredTokens() {
115        return getAcceptableTokens();
116    }
117
118    @Override
119    public void visitJavadocToken(DetailNode ast) {
120        if (isSingleLineJavadoc(getBlockCommentAst())
121                && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) {
122            log(ast.getLineNumber(), MSG_KEY);
123        }
124    }
125
126    /**
127     * Checks if comment is single line comment.
128     *
129     * @param blockCommentStart the AST tree in which a block comment starts
130     * @return true, if comment is single line comment.
131     */
132    private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) {
133        final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
134        return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo();
135    }
136
137    /**
138     * Checks if comment has javadoc tags which are not ignored. Also works
139     * on custom tags. As block tags can be interpreted only at the beginning of a line,
140     * only the first instance is checked.
141     *
142     * @param javadocRoot javadoc root node.
143     * @return true, if comment has javadoc tags which are not ignored.
144     * @see <a href=
145     * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags>
146     * Block and inline tags</a>
147     */
148    private boolean hasJavadocTags(DetailNode javadocRoot) {
149        final DetailNode javadocTagSection =
150                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
151        return javadocTagSection != null && !isTagIgnored(javadocTagSection);
152    }
153
154    /**
155     * Checks if comment has in-line tags which are not ignored.
156     *
157     * @param javadocRoot javadoc root node.
158     * @return true, if comment has in-line tags which are not ignored.
159     * @see <a href=
160     * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags>
161     * JavadocTags</a>
162     */
163    private boolean hasJavadocInlineTags(DetailNode javadocRoot) {
164        DetailNode javadocTagSection =
165                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
166        boolean foundTag = false;
167        while (javadocTagSection != null) {
168            if (!isTagIgnored(javadocTagSection)) {
169                foundTag = true;
170                break;
171            }
172            javadocTagSection = JavadocUtils.getNextSibling(
173                    javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG);
174        }
175        return foundTag;
176    }
177
178    /**
179     * Checks if list of ignored tags contains javadocTagSection's javadoc tag.
180     *
181     * @param javadocTagSection to check javadoc tag in.
182     * @return true, if ignoredTags contains javadocTagSection's javadoc tag.
183     */
184    private boolean isTagIgnored(DetailNode javadocTagSection) {
185        return ignoredTags.contains(JavadocUtils.getTagName(javadocTagSection));
186    }
187}