001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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.List;
024
025import com.google.common.collect.Lists;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.DetailNode;
028import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
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 = Lists.newArrayList(tags);
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[] getAcceptableTokens() {
104        return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN };
105    }
106
107    @Override
108    public int[] getRequiredTokens() {
109        return getAcceptableTokens();
110    }
111
112    @Override
113    public void visitJavadocToken(DetailNode ast) {
114        if (isSingleLineJavadoc(getBlockCommentAst())
115                && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) {
116            log(ast.getLineNumber(), MSG_KEY);
117        }
118    }
119
120    /**
121     * Checks if comment is single line comment.
122     *
123     * @param blockCommentStart the AST tree in which a block comment starts
124     * @return true, if comment is single line comment.
125     */
126    private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) {
127        final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
128        return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo();
129    }
130
131    /**
132     * Checks if comment has javadoc tags which are not ignored. Also works
133     * on custom tags. As block tags can be interpreted only at the beginning of a line,
134     * only the first instance is checked.
135     *
136     * @param javadocRoot javadoc root node.
137     * @return true, if comment has javadoc tags which are not ignored.
138     * @see <a href=
139     * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags>
140     * Block and inline tags</a>
141     */
142    private boolean hasJavadocTags(DetailNode javadocRoot) {
143        final DetailNode javadocTagSection =
144                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
145        return javadocTagSection != null && !isTagIgnored(javadocTagSection);
146    }
147
148    /**
149     * Checks if comment has in-line tags which are not ignored.
150     *
151     * @param javadocRoot javadoc root node.
152     * @return true, if comment has in-line tags which are not ignored.
153     * @see <a href=
154     * http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags>
155     * JavadocTags</a>
156     */
157    private boolean hasJavadocInlineTags(DetailNode javadocRoot) {
158        DetailNode javadocTagSection =
159                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
160        boolean foundTag = false;
161        while (javadocTagSection != null) {
162            if (!isTagIgnored(javadocTagSection)) {
163                foundTag = true;
164                break;
165            }
166            javadocTagSection = JavadocUtils.getNextSibling(
167                    javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG);
168        }
169        return foundTag;
170    }
171
172    /**
173     * Checks if list of ignored tags contains javadocTagSection's javadoc tag.
174     *
175     * @param javadocTagSection to check javadoc tag in.
176     * @return true, if ignoredTags contains javadocTagSection's javadoc tag.
177     */
178    private boolean isTagIgnored(DetailNode javadocTagSection) {
179        return ignoredTags.contains(JavadocUtils.getTagName(javadocTagSection));
180    }
181}