1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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 /**
45 * Creates a new {@code JavadocMissingWhitespaceAfterAsteriskCheck} instance.
46 */
47 public JavadocMissingWhitespaceAfterAsteriskCheck() {
48 // no code by default
49 }
50
51 @Override
52 public int[] getDefaultJavadocTokens() {
53 return new int[] {
54 JavadocCommentsTokenTypes.JAVADOC_CONTENT,
55 JavadocCommentsTokenTypes.LEADING_ASTERISK,
56 };
57 }
58
59 @Override
60 public int[] getRequiredJavadocTokens() {
61 return getAcceptableJavadocTokens();
62 }
63
64 @Override
65 public void visitJavadocToken(DetailNode detailNode) {
66 final DetailNode nextNode = resolveNextNode(detailNode);
67
68 if (nextNode != null) {
69 final String text = nextNode.getText();
70 final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
71
72 if (!isLast(lastAsteriskPosition, text)
73 && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
74 log(nextNode, MSG_KEY);
75 }
76 }
77 }
78
79 /**
80 * Resolves the first child node related to the given Javadoc {@link DetailNode}.
81 *
82 * <p>
83 * The resolution works in two steps:
84 * <ul>
85 * <li>If the current node is of type {@code JAVADOC_CONTENT}, use its first child;
86 * otherwise use its next sibling.</li>
87 * <li>If that base node has a first child, return it regardless of its type.</li>
88 * </ul>
89 *
90 * <p>
91 * The returned node may or may not be of type {@code TEXT}. If it is not,
92 * the violation logic will treat it as a violation later.
93 *
94 * @param detailNode the Javadoc node to resolve from
95 * @return the first child node if available; otherwise {@code null}
96 */
97 private static DetailNode resolveNextNode(DetailNode detailNode) {
98 final DetailNode baseNode;
99 if (detailNode.getType() == JavadocCommentsTokenTypes.JAVADOC_CONTENT) {
100 baseNode = detailNode.getFirstChild();
101 }
102 else {
103 baseNode = detailNode.getNextSibling();
104 }
105
106 DetailNode nextNode = baseNode;
107 if (baseNode != null && baseNode.getFirstChild() != null) {
108 nextNode = baseNode.getFirstChild();
109 }
110
111 return nextNode;
112 }
113
114 /**
115 * Checks if the character position is the last one of the string.
116 *
117 * @param position the position of the character
118 * @param text String literal.
119 * @return true if the character position is the last one of the string.
120 *
121 */
122 private static boolean isLast(int position, String text) {
123 return position == text.length() - 1;
124 }
125
126 /**
127 * Finds the position of the last leading asterisk in the string.
128 * If {@code text} contains no leading asterisk, -1 will be returned.
129 *
130 * @param text String literal.
131 * @return the index of the last leading asterisk.
132 *
133 */
134 private static int getLastLeadingAsteriskPosition(String text) {
135 int index = -1;
136
137 for (int i = 0; i < text.length(); i++) {
138 if (text.charAt(i) != '*') {
139 break;
140 }
141 index++;
142 }
143
144 return index;
145 }
146
147 }