001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 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.Set;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.DetailNode;
027import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
029import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
030
031/**
032 * <p>
033 * Checks that a Javadoc block can fit in a single-line and doesn't contain block tags.
034 * Javadoc comment that contains at least one block tag should be formatted in a few lines.
035 * </p>
036 * <ul>
037 * <li>
038 * Property {@code ignoreInlineTags} - Control whether
039 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
040 * inline tags</a> must be ignored.
041 * Type is {@code boolean}.
042 * Default value is {@code true}.
043 * </li>
044 * <li>
045 * Property {@code ignoredTags} - Specify
046 * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
047 * block tags</a> which are ignored by the check.
048 * Type is {@code java.lang.String[]}.
049 * Default value is {@code ""}.
050 * </li>
051 * <li>
052 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
053 * if the Javadoc being examined by this check violates the tight html rules defined at
054 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
055 * Type is {@code boolean}.
056 * Default value is {@code false}.
057 * </li>
058 * </ul>
059 * <p>
060 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
061 * </p>
062 * <p>
063 * Violation Message Keys:
064 * </p>
065 * <ul>
066 * <li>
067 * {@code javadoc.missed.html.close}
068 * </li>
069 * <li>
070 * {@code javadoc.parse.rule.error}
071 * </li>
072 * <li>
073 * {@code javadoc.wrong.singleton.html.tag}
074 * </li>
075 * <li>
076 * {@code singleline.javadoc}
077 * </li>
078 * </ul>
079 *
080 * @since 6.0
081 */
082@StatelessCheck
083public class SingleLineJavadocCheck extends AbstractJavadocCheck {
084
085    /**
086     * A key is pointing to the warning message text in "messages.properties"
087     * file.
088     */
089    public static final String MSG_KEY = "singleline.javadoc";
090
091    /**
092     * Specify
093     * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
094     * block tags</a> which are ignored by the check.
095     */
096    private Set<String> ignoredTags = Set.of();
097
098    /**
099     * Control whether
100     * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
101     * inline tags</a> must be ignored.
102     */
103    private boolean ignoreInlineTags = true;
104
105    /**
106     * Setter to specify
107     * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
108     * block tags</a> which are ignored by the check.
109     *
110     * @param tags to be ignored by check.
111     * @since 6.8
112     */
113    public void setIgnoredTags(String... tags) {
114        ignoredTags = Set.of(tags);
115    }
116
117    /**
118     * Setter to control whether
119     * <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#CHDBEFIF">
120     * inline tags</a> must be ignored.
121     *
122     * @param ignoreInlineTags whether inline tags must be ignored.
123     * @since 6.8
124     */
125    public void setIgnoreInlineTags(boolean ignoreInlineTags) {
126        this.ignoreInlineTags = ignoreInlineTags;
127    }
128
129    @Override
130    public int[] getDefaultJavadocTokens() {
131        return new int[] {
132            JavadocTokenTypes.JAVADOC,
133        };
134    }
135
136    @Override
137    public int[] getRequiredJavadocTokens() {
138        return getAcceptableJavadocTokens();
139    }
140
141    @Override
142    public void visitJavadocToken(DetailNode ast) {
143        if (isSingleLineJavadoc(getBlockCommentAst())
144                && (hasJavadocTags(ast) || !ignoreInlineTags && hasJavadocInlineTags(ast))) {
145            log(ast.getLineNumber(), MSG_KEY);
146        }
147    }
148
149    /**
150     * Checks if comment is single-line comment.
151     *
152     * @param blockCommentStart the AST tree in which a block comment starts
153     * @return true, if comment is single-line comment.
154     */
155    private static boolean isSingleLineJavadoc(DetailAST blockCommentStart) {
156        final DetailAST blockCommentEnd = blockCommentStart.getLastChild();
157        return TokenUtil.areOnSameLine(blockCommentStart, blockCommentEnd);
158    }
159
160    /**
161     * Checks if comment has javadoc tags which are not ignored. Also works
162     * on custom tags. As block tags can be interpreted only at the beginning of a line,
163     * only the first instance is checked.
164     *
165     * @param javadocRoot javadoc root node.
166     * @return true, if comment has javadoc tags which are not ignored.
167     * @see <a href=
168     * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#blockandinlinetags">
169     * Block and inline tags</a>
170     */
171    private boolean hasJavadocTags(DetailNode javadocRoot) {
172        final DetailNode javadocTagSection =
173                JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_TAG);
174        return javadocTagSection != null && !isTagIgnored(javadocTagSection);
175    }
176
177    /**
178     * Checks if comment has in-line tags which are not ignored.
179     *
180     * @param javadocRoot javadoc root node.
181     * @return true, if comment has in-line tags which are not ignored.
182     * @see <a href=
183     * "https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#javadoctags">
184     * JavadocTags</a>
185     */
186    private boolean hasJavadocInlineTags(DetailNode javadocRoot) {
187        DetailNode javadocTagSection =
188                JavadocUtil.findFirstToken(javadocRoot, JavadocTokenTypes.JAVADOC_INLINE_TAG);
189        boolean foundTag = false;
190        while (javadocTagSection != null) {
191            if (!isTagIgnored(javadocTagSection)) {
192                foundTag = true;
193                break;
194            }
195            javadocTagSection = JavadocUtil.getNextSibling(
196                    javadocTagSection, JavadocTokenTypes.JAVADOC_INLINE_TAG);
197        }
198        return foundTag;
199    }
200
201    /**
202     * Checks if list of ignored tags contains javadocTagSection's javadoc tag.
203     *
204     * @param javadocTagSection to check javadoc tag in.
205     * @return true, if ignoredTags contains javadocTagSection's javadoc tag.
206     */
207    private boolean isTagIgnored(DetailNode javadocTagSection) {
208        return ignoredTags.contains(JavadocUtil.getTagName(javadocTagSection));
209    }
210
211}