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.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.wrong.singleton.html.tag}
092 * </li>
093 * </ul>
094 *
095 * @since 8.24
096 */
097@StatelessCheck
098public class JavadocBlockTagLocationCheck extends AbstractJavadocCheck {
099
100    /**
101     * A key is pointing to the warning message text in "messages.properties" file.
102     */
103    public static final String MSG_BLOCK_TAG_LOCATION = "javadoc.blockTagLocation";
104
105    /**
106     * This regexp is used to extract the javadoc tags.
107     */
108    private static final Pattern JAVADOC_BLOCK_TAG_PATTERN = Pattern.compile("\\s@(\\w+)");
109
110    /**
111     * Block tags from Java 11
112     * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html">
113     * Documentation Comment Specification</a>.
114     */
115    private static final String[] DEFAULT_TAGS = {
116        "author",
117        "deprecated",
118        "exception",
119        "hidden",
120        "param",
121        "provides",
122        "return",
123        "see",
124        "serial",
125        "serialData",
126        "serialField",
127        "since",
128        "throws",
129        "uses",
130        "version",
131    };
132
133    /**
134     * Specify the javadoc tags to process.
135     */
136    private Set<String> tags;
137
138    /**
139     * Creates a new {@code JavadocBlockTagLocationCheck} instance with default settings.
140     */
141    public JavadocBlockTagLocationCheck() {
142        setTags(DEFAULT_TAGS);
143    }
144
145    /**
146     * Setter to specify the javadoc tags to process.
147     *
148     * @param values user's values.
149     * @since 8.24
150     */
151    public final void setTags(String... values) {
152        tags = Arrays.stream(values).collect(Collectors.toSet());
153    }
154
155    /**
156     * The javadoc tokens that this check must be registered for. According to
157     * <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html#block-tags">
158     * the specs</a> each block tag must appear at the beginning of a line, otherwise
159     * it will be interpreted as a plain text. This check looks for a block tag
160     * in the javadoc text, thus it needs the {@code TEXT} tokens.
161     *
162     * @return the javadoc token set this must be registered for.
163     * @see JavadocTokenTypes
164     */
165    @Override
166    public int[] getRequiredJavadocTokens() {
167        return new int[] {
168            JavadocTokenTypes.TEXT,
169        };
170    }
171
172    @Override
173    public int[] getAcceptableJavadocTokens() {
174        return getRequiredJavadocTokens();
175    }
176
177    @Override
178    public int[] getDefaultJavadocTokens() {
179        return getRequiredJavadocTokens();
180    }
181
182    @Override
183    public void visitJavadocToken(DetailNode ast) {
184        if (!isCommentOrInlineTag(ast.getParent())) {
185            final Matcher tagMatcher = JAVADOC_BLOCK_TAG_PATTERN.matcher(ast.getText());
186            while (tagMatcher.find()) {
187                final String tagName = tagMatcher.group(1);
188                if (tags.contains(tagName)) {
189                    log(ast.getLineNumber(), MSG_BLOCK_TAG_LOCATION, tagName);
190                }
191            }
192        }
193    }
194
195    /**
196     * Checks if the node can contain an unescaped block tag without violation.
197     *
198     * @param node to check
199     * @return {@code true} if node is {@code @code}, {@code @literal} or HTML comment.
200     */
201    private static boolean isCommentOrInlineTag(DetailNode node) {
202        return node.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG
203                || node.getType() == JavadocTokenTypes.HTML_COMMENT;
204    }
205
206}