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