001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.Locale;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
029import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
030
031/**
032 * <div>
033 * Checks that the Javadoc content begins from the same position
034 * for all Javadoc comments in the project. Any leading asterisks and spaces
035 * are not counted as the beginning of the content and are therefore ignored.
036 * </div>
037 *
038 * <p>
039 * It is possible to enforce two different styles:
040 * </p>
041 * <ul>
042 * <li>
043 * {@code first_line} - Javadoc content starts from the first line:
044 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
045 * &#47;** Summary text.
046 *   * More details.
047 *   *&#47;
048 * public void method();
049 * </code></pre></div>
050 * </li>
051 * <li>
052 * {@code second_line} - Javadoc content starts from the second line:
053 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
054 * &#47;**
055 *   * Summary text.
056 *   * More details.
057 *   *&#47;
058 * public void method();
059 * </code></pre></div>
060 * </li>
061 * </ul>
062 *
063 * <p>
064 * Notes:
065 * This check does not validate the Javadoc summary itself nor its presence.
066 * The check will not report any violations for missing or malformed javadoc summary.
067 * To validate the Javadoc summary use
068 * <a href="https://checkstyle.org/checks/javadoc/summaryjavadoc.html#SummaryJavadoc">
069 * SummaryJavadoc</a> check.
070 * </p>
071 *
072 * <p>
073 * The <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/javadoc/doc-comment-spec.html">
074 * Documentation Comment Specification</a> permits leading asterisks on the first line.
075 * For these Javadoc comments:
076 * </p>
077 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
078 * &#47;***
079 *   * Some text.
080 *   *&#47;
081 * &#47;************
082 *   * Some text.
083 *   *&#47;
084 * &#47;**           **
085 *   * Some text.
086 *   *&#47;
087 * </code></pre></div>
088 *
089 * <p>
090 * The documentation generated will be just "Some text." without any asterisks.
091 * Since these asterisks will not appear in the generated documentation,
092 * they should not be considered as the beginning of the Javadoc content.
093 * In such cases, the check assumes that the Javadoc content begins on the second line.
094 * </p>
095 * <ul>
096 * <li>
097 * Property {@code location} - Specify the policy on placement of the Javadoc content.
098 * Type is {@code com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocContentLocationOption}.
099 * Default value is {@code second_line}.
100 * </li>
101 * </ul>
102 *
103 * <p>
104 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
105 * </p>
106 *
107 * <p>
108 * Violation Message Keys:
109 * </p>
110 * <ul>
111 * <li>
112 * {@code javadoc.content.first.line}
113 * </li>
114 * <li>
115 * {@code javadoc.content.second.line}
116 * </li>
117 * </ul>
118 *
119 * @since 8.27
120 */
121@StatelessCheck
122public class JavadocContentLocationCheck extends AbstractCheck {
123
124    /**
125     * A key is pointing to the warning message text in "messages.properties" file.
126     */
127    public static final String MSG_JAVADOC_CONTENT_FIRST_LINE = "javadoc.content.first.line";
128
129    /**
130     * A key is pointing to the warning message text in "messages.properties" file.
131     */
132    public static final String MSG_JAVADOC_CONTENT_SECOND_LINE = "javadoc.content.second.line";
133
134    /**
135     * Specify the policy on placement of the Javadoc content.
136     */
137    private JavadocContentLocationOption location = JavadocContentLocationOption.SECOND_LINE;
138
139    @Override
140    public int[] getRequiredTokens() {
141        return new int[] {
142            TokenTypes.BLOCK_COMMENT_BEGIN,
143        };
144    }
145
146    @Override
147    public int[] getAcceptableTokens() {
148        return getRequiredTokens();
149    }
150
151    @Override
152    public int[] getDefaultTokens() {
153        return getRequiredTokens();
154    }
155
156    @Override
157    public boolean isCommentNodesRequired() {
158        return true;
159    }
160
161    /**
162     * Setter to specify the policy on placement of the Javadoc content.
163     *
164     * @param value string to decode location from
165     * @throws IllegalArgumentException if unable to decode
166     * @since 8.27
167     */
168    public void setLocation(String value) {
169        location = JavadocContentLocationOption.valueOf(value.trim().toUpperCase(Locale.ENGLISH));
170    }
171
172    @Override
173    public void visitToken(DetailAST ast) {
174        if (isMultilineComment(ast) && JavadocUtil.isJavadocComment(ast)) {
175            final String commentContent = JavadocUtil.getJavadocCommentContent(ast);
176            final int indexOfFirstNonBlankLine = findIndexOfFirstNonBlankLine(commentContent);
177            if (indexOfFirstNonBlankLine >= 0) {
178                if (location == JavadocContentLocationOption.FIRST_LINE
179                        && indexOfFirstNonBlankLine != 0) {
180                    log(ast, MSG_JAVADOC_CONTENT_FIRST_LINE);
181                }
182                else if (location == JavadocContentLocationOption.SECOND_LINE
183                        && indexOfFirstNonBlankLine != 1) {
184                    log(ast, MSG_JAVADOC_CONTENT_SECOND_LINE);
185                }
186            }
187        }
188    }
189
190    /**
191     * Checks if a DetailAST of type {@code TokenTypes.BLOCK_COMMENT_BEGIN} span
192     * more than one line. The node always has at least one child of the type
193     * {@code TokenTypes.BLOCK_COMMENT_END}.
194     *
195     * @param node node to check
196     * @return {@code true} for multi-line comment nodes
197     */
198    private static boolean isMultilineComment(DetailAST node) {
199        return !TokenUtil.areOnSameLine(node, node.getLastChild());
200    }
201
202    /**
203     * Returns the index of the first non-blank line.
204     * All lines consists only of asterisks and whitespaces are treated as blank.
205     *
206     * @param commentContent Javadoc content to process
207     * @return the index of the first non-blank line or {@code -1} if all lines are blank
208     */
209    private static int findIndexOfFirstNonBlankLine(String commentContent) {
210        int lineNo = 0;
211        boolean noContent = true;
212        for (int i = 0; i < commentContent.length(); ++i) {
213            final char character = commentContent.charAt(i);
214            if (character == '\n') {
215                ++lineNo;
216            }
217            else if (character != '*' && !Character.isWhitespace(character)) {
218                noContent = false;
219                break;
220            }
221        }
222        if (noContent) {
223            lineNo = -1;
224        }
225        return lineNo;
226    }
227
228}