-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
JavadocMissingWhitespaceAfterAsteriskCheck.java
150 lines (135 loc) · 4.86 KB
/
JavadocMissingWhitespaceAfterAsteriskCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
/**
* <p>
* Checks that there is at least one whitespace after the leading asterisk.
* Although spaces after asterisks are optional in the Javadoc comments, their absence
* makes the documentation difficult to read. It is the de facto standard to put at least
* one whitespace after the leading asterisk.
* </p>
* <ul>
* <li>
* Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
* if the Javadoc being examined by this check violates the tight html rules defined at
* <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
* Default value is {@code false}.
* </li>
* </ul>
* <p>
* To configure the default check:
* </p>
* <pre>
* <module name="JavadocMissingWhitespaceAfterAsterisk"/>
* </pre>
* <p>
* Code Example:
* </p>
* <pre>
* /** This is valid single-line Javadoc. */
* class TestClass {
* /**
* *This is invalid Javadoc.
* */
* int invalidJavaDoc;
* /**
* * This is valid Javadoc.
* */
* void validJavaDocMethod() {
* }
* /**This is invalid single-line Javadoc. */
* void invalidSingleLineJavaDocMethod() {
* }
* /** This is valid single-line Javadoc. */
* void validSingleLineJavaDocMethod() {
* }
* }
* </pre>
*
* @since 8.32
*/
@StatelessCheck
public class JavadocMissingWhitespaceAfterAsteriskCheck extends AbstractJavadocCheck {
/**
* A key is pointing to the warning message text in "messages.properties" file.
*/
public static final String MSG_KEY = "javadoc.missing.whitespace";
@Override
public int[] getDefaultJavadocTokens() {
return new int[] {
JavadocTokenTypes.JAVADOC,
JavadocTokenTypes.LEADING_ASTERISK,
};
}
@Override
public int[] getRequiredJavadocTokens() {
return getAcceptableJavadocTokens();
}
@Override
public void visitJavadocToken(DetailNode detailNode) {
final DetailNode textNode;
if (detailNode.getType() == JavadocTokenTypes.JAVADOC) {
textNode = JavadocUtil.getFirstChild(detailNode);
}
else {
textNode = JavadocUtil.getNextSibling(detailNode);
}
if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) {
final String text = textNode.getText();
final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
if (!isLast(lastAsteriskPosition, text)
&& !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY);
}
}
}
/**
* Checks if the character position is the last one of the string.
*
* @param position the position of the character
* @param text String literal.
* @return true if the character position is the last one of the string.
*
*/
private static boolean isLast(int position, String text) {
return position == text.length() - 1;
}
/**
* Finds the position of the last leading asterisk in the string.
* If {@code text} contains no leading asterisk, -1 will be returned.
*
* @param text String literal.
* @return the index of the last leading asterisk.
*
*/
private static int getLastLeadingAsteriskPosition(String text) {
int index = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) != '*') {
break;
}
index++;
}
return index;
}
}