001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.Arrays;
023import java.util.Set;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026import java.util.stream.Collectors;
027
028import com.puppycrawl.tools.checkstyle.StatelessCheck;
029import com.puppycrawl.tools.checkstyle.api.DetailNode;
030import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
031
032/**
033 * <p>
034 * Checks that a
035 * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html#block-tags">
036 * javadoc block tag</a> appears only at the beginning of a line, ignoring
037 * leading asterisks and white space. A block tag is a token that starts with
038 * {@code @} symbol and is preceded by a whitespace. This check ignores block
039 * tags in comments and inside inline tags {&#64;code } and {&#64;literal }.
040 * </p>
041 * <p>
042 * Rationale: according to
043 * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html#block-tags">
044 * the specification</a> all javadoc block tags should be placed at the beginning
045 * of a line. Tags that are not placed at the beginning are treated as plain text.
046 * To recognize intentional tag placement to text area it is better to escape the
047 * {@code @} symbol, and all non-escaped tags should be located at the beginning
048 * of the line. See NOTE section for details on how to escape.
049 * </p>
050 * <p>
051 * To place a tag explicitly as text, escape the {@code @} symbol with HTML entity
052 * &amp;#64; or place it inside {@code {@code }}, for example:
053 * </p>
054 * <pre>
055 * &#47;**
056 *  * &amp;#64;serial literal in {&#64;code &#64;serial} Javadoc tag.
057 *  *&#47;
058 * </pre>
059 * <ul>
060 * <li>
061 * Property {@code tags} - Specify the javadoc tags to process.
062 * Type is {@code java.lang.String[]}.
063 * Default value is {@code author, deprecated, exception, hidden, param, provides,
064 * return, see, serial, serialData, serialField, since, throws, uses, version}.
065 * </li>
066 * <li>
067 * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
068 * if the Javadoc being examined by this check violates the tight html rules defined at
069 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
070 * Type is {@code boolean}.
071 * Default value is {@code false}.
072 * </li>
073 * </ul>
074 * <p>
075 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
076 * </p>
077 * <p>
078 * Violation Message Keys:
079 * </p>
080 * <ul>
081 * <li>
082 * {@code javadoc.blockTagLocation}
083 * </li>
084 * <li>
085 * {@code javadoc.missed.html.close}
086 * </li>
087 * <li>
088 * {@code javadoc.parse.rule.error}
089 * </li>
090 * <li>
091 * {@code javadoc.unclosedHtml}
092 * </li>
093 * <li>
094 * {@code javadoc.wrong.singleton.html.tag}
095 * </li>
096 * </ul>
097 *
098 * @since 8.24
099 */
100@StatelessCheck
101public class JavadocBlockTagLocationCheck extends AbstractJavadocCheck {
102
103    /**
104     * A key is pointing to the warning message text in "messages.properties" file.
105     */
106    public static final String MSG_BLOCK_TAG_LOCATION = "javadoc.blockTagLocation";
107
108    /**
109     * This regexp is used to extract the javadoc tags.
110     */
111    private static final Pattern JAVADOC_BLOCK_TAG_PATTERN = Pattern.compile("\\s@(\\w+)");
112
113    /**
114     * Block tags from Java 11
115     * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html">
116     * Documentation Comment Specification</a>.
117     */
118    private static final String[] DEFAULT_TAGS = {
119        "author",
120        "deprecated",
121        "exception",
122        "hidden",
123        "param",
124        "provides",
125        "return",
126        "see",
127        "serial",
128        "serialData",
129        "serialField",
130        "since",
131        "throws",
132        "uses",
133        "version",
134    };
135
136    /**
137     * Specify the javadoc tags to process.
138     */
139    private Set<String> tags;
140
141    /**
142     * Creates a new {@code JavadocBlockTagLocationCheck} instance with default settings.
143     */
144    public JavadocBlockTagLocationCheck() {
145        setTags(DEFAULT_TAGS);
146    }
147
148    /**
149     * Setter to specify the javadoc tags to process.
150     *
151     * @param values user's values.
152     * @since 8.24
153     */
154    public final void setTags(String... values) {
155        tags = Arrays.stream(values).collect(Collectors.toUnmodifiableSet());
156    }
157
158    /**
159     * The javadoc tokens that this check must be registered for. According to
160     * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html#block-tags">
161     * the specs</a> each block tag must appear at the beginning of a line, otherwise
162     * it will be interpreted as a plain text. This check looks for a block tag
163     * in the javadoc text, thus it needs the {@code TEXT} tokens.
164     *
165     * @return the javadoc token set this must be registered for.
166     * @see JavadocTokenTypes
167     */
168    @Override
169    public int[] getRequiredJavadocTokens() {
170        return new int[] {
171            JavadocTokenTypes.TEXT,
172        };
173    }
174
175    @Override
176    public int[] getAcceptableJavadocTokens() {
177        return getRequiredJavadocTokens();
178    }
179
180    @Override
181    public int[] getDefaultJavadocTokens() {
182        return getRequiredJavadocTokens();
183    }
184
185    @Override
186    public void visitJavadocToken(DetailNode ast) {
187        if (!isCommentOrInlineTag(ast.getParent())) {
188            final Matcher tagMatcher = JAVADOC_BLOCK_TAG_PATTERN.matcher(ast.getText());
189            while (tagMatcher.find()) {
190                final String tagName = tagMatcher.group(1);
191                if (tags.contains(tagName)) {
192                    log(ast.getLineNumber(), MSG_BLOCK_TAG_LOCATION, tagName);
193                }
194            }
195        }
196    }
197
198    /**
199     * Checks if the node can contain an unescaped block tag without violation.
200     *
201     * @param node to check
202     * @return {@code true} if node is {@code @code}, {@code @literal} or HTML comment.
203     */
204    private static boolean isCommentOrInlineTag(DetailNode node) {
205        return node.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG
206                || node.getType() == JavadocTokenTypes.HTML_COMMENT;
207    }
208
209}