1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.javadoc;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.DetailNode;
24 import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
25
26 /**
27 * <div>
28 * Checks that there is at least one whitespace after the leading asterisk.
29 * Although spaces after asterisks are optional in the Javadoc comments, their absence
30 * makes the documentation difficult to read. It is the de facto standard to put at least
31 * one whitespace after the leading asterisk.
32 * </div>
33 *
34 * @since 8.32
35 */
36 @StatelessCheck
37 public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck {
38
39 /**
40 * A key is pointing to the warning message text in "messages.properties" file.
41 */
42 public static final String MSG_KEY = "javadoc.missing.whitespace";
43
44 @Override
45 public int[] getDefaultJavadocTokens() {
46 return new int[] {
47 JavadocCommentsTokenTypes.JAVADOC_CONTENT,
48 JavadocCommentsTokenTypes.LEADING_ASTERISK,
49 };
50 }
51
52 @Override
53 public int[] getRequiredJavadocTokens() {
54 return getAcceptableJavadocTokens();
55 }
56
57 @Override
58 public void visitJavadocToken(DetailNode detailNode) {
59 final DetailNode nextNode = resolveNextNode(detailNode);
60
61 if (nextNode != null) {
62 final String text = nextNode.getText();
63 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
64
65 if (!isLast(lastAsteriskPosition, text)
66 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
67 log(nextNode.getLineNumber(), nextNode.getColumnNumber(), MSG_KEY);
68 }
69 }
70 }
71
72 /**
73 * Resolves the first child node related to the given Javadoc {@link DetailNode}.
74 *
75 * <p>
76 * The resolution works in two steps:
77 * <ul>
78 * <li>If the current node is of type {@code JAVADOC_CONTENT}, use its first child;
79 * otherwise use its next sibling.</li>
80 * <li>If that base node has a first child, return it regardless of its type.</li>
81 * </ul>
82 *
83 * <p>
84 * The returned node may or may not be of type {@code TEXT}. If it is not,
85 * the violation logic will treat it as a violation later.
86 *
87 * @param detailNode the Javadoc node to resolve from
88 * @return the first child node if available; otherwise {@code null}
89 */
90 private static DetailNode resolveNextNode(DetailNode detailNode) {
91 final DetailNode baseNode;
92 if (detailNode.getType() == JavadocCommentsTokenTypes.JAVADOC_CONTENT) {
93 baseNode = detailNode.getFirstChild();
94 }
95 else {
96 baseNode = detailNode.getNextSibling();
97 }
98
99 DetailNode nextNode = baseNode;
100 if (baseNode != null && baseNode.getFirstChild() != null) {
101 nextNode = baseNode.getFirstChild();
102 }
103
104 return nextNode;
105 }
106
107 /**
108 * Checks if the character position is the last one of the string.
109 *
110 * @param position the position of the character
111 * @param text String literal.
112 * @return true if the character position is the last one of the string.
113 *
114 */
115 private static boolean isLast(int position, String text) {
116 return position == text.length() - 1;
117 }
118
119 /**
120 * Finds the position of the last leading asterisk in the string.
121 * If {@code text} contains no leading asterisk, -1 will be returned.
122 *
123 * @param text String literal.
124 * @return the index of the last leading asterisk.
125 *
126 */
127 private static int getLastLeadingAsteriskPosition(String text) {
128 int index = -1;
129
130 for (int i = 0; i < text.length(); i++) {
131 if (text.charAt(i) != '*') {
132 break;
133 }
134 index++;
135 }
136
137 return index;
138 }
139
140 }