001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.javadoc;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.DetailNode;
023import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
024
025/**
026 * Checks that a JavaDoc block which can fit on a single line and doesn't
027 * contain at-clauses. Javadoc comment that contains at least one at-clause
028 * should be formatted in few lines.
029 *
030 * Default configuration:
031 * <pre>
032 * &lt;module name=&quot;SingleLineJavadoc&quot;/&gt;
033 * </pre>
034 *
035 * @author baratali
036 * @author maxvetrenko
037 *
038 */
039public class SingleLineJavadocCheck extends AbstractJavadocCheck
040{
041
042    @Override
043    public int[] getDefaultJavadocTokens()
044    {
045        return new int[] {
046            JavadocTokenTypes.JAVADOC,
047        };
048    }
049
050    @Override
051    public void visitJavadocToken(DetailNode ast)
052    {
053        if (isSingleLineJavadoc()
054                && (hasJavadocTags(ast) || hasJavadocInlineTags(ast)))
055        {
056            log(ast.getLineNumber(), "singleline.javadoc");
057        }
058    }
059
060    /**
061     * Checks if comment is single line comment.
062     *
063     * @return true, if comment is single line comment.
064     */
065    private boolean isSingleLineJavadoc()
066    {
067        final DetailAST blockCommentStart = getBlockCommentAst();
068        final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
069
070        return blockCommentStart.getLineNo() == blockCommentEnd.getLineNo();
071    }
072
073    /**
074     * Checks if comment has javadoc tags.
075     *
076     * @param javadocRoot javadoc root node.
077     * @return true, if comment has javadoc tags.
078     */
079    private boolean hasJavadocTags(DetailNode javadocRoot)
080    {
081        final DetailNode javadocTagSection =
082                JavadocUtils.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
083        return javadocTagSection != null;
084    }
085
086    /**
087     * Checks if comment has in-line tags tags.
088     *
089     * @param javadocRoot javadoc root node.
090     * @return true, if comment has in-line tags tags.
091     */
092    private boolean hasJavadocInlineTags(DetailNode javadocRoot)
093    {
094        return JavadocUtils.branchContains(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
095    }
096}