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