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 * @author baratali
057 * @author maxvetrenko
058 * @author vladlis
059 *
060 */
061public class SingleLineJavadocCheck extends AbstractJavadocCheck {
062
063    /**
064     * A key is pointing to the warning message text in "messages.properties"
065     * file.
066     */
067    public static final String MSG_KEY = "singleline.javadoc";
068
069    /**
070     * Allows to specify a list of tags to be ignored by check.
071     */
072    private List<String> ignoredTags = new ArrayList<>();
073
074    /** Whether inline tags must be ignored. **/
075    private boolean ignoreInlineTags = true;
076
077    /**
078     * Sets a list of tags to be ignored by check.
079     *
080     * @param tags to be ignored by check.
081     */
082    public void setIgnoredTags(String... tags) {
083        ignoredTags = Arrays.stream(tags).collect(Collectors.toList());
084    }
085
086    /**
087     * Sets whether inline tags must be ignored.
088     *
089     * @param ignoreInlineTags whether inline tags must be ignored.
090     */
091    public void setIgnoreInlineTags(boolean ignoreInlineTags) {
092        this.ignoreInlineTags = ignoreInlineTags;
093    }
094
095    @Override
096    public int[] getDefaultJavadocTokens() {
097        return new int[] {
098            JavadocTokenTypes.JAVADOC,
099        };
100    }
101
102    @Override
103    public int[] getRequiredJavadocTokens() {
104        return getAcceptableJavadocTokens();
105    }
106
107    @Override
108    public void visitJavadocToken(DetailNode ast) {
109        if (isSingleLineJavadoc(getBlockCommentAst())
110                && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) {
111            log(ast.getLineNumber(), MSG_KEY);
112        }
113    }
114
115    /**
116     * Checks if comment is single line comment.
117     *
118     * @param blockCommentStart the AST tree in which a block comment starts
119     * @return true, if comment is single line comment.
120     */
121    private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) {
122        final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
123        return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo();
124    }
125
126    /**
127     * Checks if comment has javadoc tags which are not ignored. Also works
128     * on custom tags. As block tags can be interpreted only at the beginning of a line,
129     * only the first instance is checked.
130     *
131     * @param javadocRoot javadoc root node.
132     * @return true, if comment has javadoc tags which are not ignored.
133     * @see <a href=
134     * https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags>
135     * Block and inline tags</a>
136     */
137    private boolean hasJavadocTags(DetailNode javadocRoot) {
138        final DetailNode javadocTagSection =
139                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
140        return javadocTagSection != null && !isTagIgnored(javadocTagSection);
141    }
142
143    /**
144     * Checks if comment has in-line tags which are not ignored.
145     *
146     * @param javadocRoot javadoc root node.
147     * @return true, if comment has in-line tags which are not ignored.
148     * @see <a href=
149     * https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags>
150     * JavadocTags</a>
151     */
152    private boolean hasJavadocInlineTags(DetailNode javadocRoot) {
153        DetailNode javadocTagSection =
154                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
155        boolean foundTag = false;
156        while (javadocTagSection != null) {
157            if (!isTagIgnored(javadocTagSection)) {
158                foundTag = true;
159                break;
160            }
161            javadocTagSection = JavadocUtils.getNextSibling(
162                    javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG);
163        }
164        return foundTag;
165    }
166
167    /**
168     * Checks if list of ignored tags contains javadocTagSection's javadoc tag.
169     *
170     * @param javadocTagSection to check javadoc tag in.
171     * @return true, if ignoredTags contains javadocTagSection's javadoc tag.
172     */
173    private boolean isTagIgnored(DetailNode javadocTagSection) {
174        return ignoredTags.contains(JavadocUtils.getTagName(javadocTagSection));
175    }
176
177}