-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
JavadocContentLocationCheck.java
244 lines (228 loc) · 7.99 KB
/
JavadocContentLocationCheck.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
////////////////////////////////////////////////////////////////////////////////
// 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 java.util.Locale;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
/**
* <p>
* Checks that the Javadoc content begins from the same position
* for all Javadoc comments in the project. Any leading asterisks and spaces
* are not counted as the beginning of the content and are therefore ignored.
* </p>
* <p>
* It is possible to enforce two different styles:
* </p>
* <ul>
* <li>
* {@code first_line} - Javadoc content starts from the first line:
* <pre>
* /** Summary text.
* * More details.
* */
* public void method();
* </pre>
* </li>
* <li>
* {@code second_line} - Javadoc content starts from the second line:
* <pre>
* /**
* * Summary text.
* * More details.
* */
* public void method();
* </pre>
* </li>
* </ul>
* <p>
* This check does not validate the Javadoc summary itself nor its presence.
* The check will not report any violations for missing or malformed javadoc summary.
* To validate the Javadoc summary use
* <a href="https://checkstyle.org/config_javadoc.html#SummaryJavadoc">SummaryJavadoc</a> check.
* </p>
* <p>
* The <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html">
* Documentation Comment Specification</a> permits leading asterisks on the first line.
* For these Javadoc comments:
* </p>
* <pre>
* /***
* * Some text.
* */
* /************
* * Some text.
* */
* /** **
* * Some text.
* */
* </pre>
* <p>
* The documentation generated will be just "Some text." without any asterisks.
* Since these asterisks will not appear in the generated documentation,
* they should not be considered as the beginning of the Javadoc content.
* In such cases, the check assumes that the Javadoc content begins on the second line.
* </p>
* <ul>
* <li>
* Property {@code location} - Specify the policy on placement of the Javadoc content.
* Default value is {@code second_line}.
* </li>
* </ul>
* <p>
* By default Check validate that the Javadoc content starts from the second line:
* </p>
* <pre>
* <module name="JavadocContentLocationCheck"/>
* </pre>
* <p>
* This setting produces a violation for each multi-line comment starting
* on the same line as the initial asterisks:
* </p>
* <pre>
* /** This comment causes a violation because it starts from the first line
* * and spans several lines.
* */
* /**
* * This comment is OK because it starts from the second line.
* */
* /** This comment is OK because it is on the single line. */
* </pre>
* <p>
* To ensure that Javadoc content starts from the first line:
* </p>
* <pre>
* <module name="JavadocContentLocationCheck">
* <property name="location" value="first_line"/>
* </module>
* </pre>
* <p>
* This setting produces a violation for each comment not
* starting on the same line as the initial asterisks:
* </p>
* <pre>
* /** This comment is OK because it starts on the first line.
* * There may be additional text.
* */
* /**
* * This comment causes a violation because it starts on the second line.
* */
* /** This single-line comment also is OK. */
* </pre>
*
* @since 8.27
*/
@StatelessCheck
public class JavadocContentLocationCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties" file.
*/
public static final String MSG_JAVADOC_CONTENT_FIRST_LINE = "javadoc.content.first.line";
/**
* A key is pointing to the warning message text in "messages.properties" file.
*/
public static final String MSG_JAVADOC_CONTENT_SECOND_LINE = "javadoc.content.second.line";
/**
* Specify the policy on placement of the Javadoc content.
*/
private JavadocContentLocationOption location = JavadocContentLocationOption.SECOND_LINE;
@Override
public int[] getRequiredTokens() {
return new int[] {
TokenTypes.BLOCK_COMMENT_BEGIN,
};
}
@Override
public int[] getAcceptableTokens() {
return getRequiredTokens();
}
@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
}
@Override
public boolean isCommentNodesRequired() {
return true;
}
/**
* Setter to specify the policy on placement of the Javadoc content.
*
* @param value string to decode location from
* @throws IllegalArgumentException if unable to decode
*/
public void setLocation(String value) {
location = JavadocContentLocationOption.valueOf(value.trim().toUpperCase(Locale.ENGLISH));
}
@Override
public void visitToken(DetailAST ast) {
if (isMultilineComment(ast) && JavadocUtil.isJavadocComment(ast)) {
final String commentContent = JavadocUtil.getJavadocCommentContent(ast);
final int indexOfFirstNonBlankLine = findIndexOfFirstNonBlankLine(commentContent);
if (indexOfFirstNonBlankLine >= 0) {
if (location == JavadocContentLocationOption.FIRST_LINE) {
if (indexOfFirstNonBlankLine != 0) {
log(ast, MSG_JAVADOC_CONTENT_FIRST_LINE);
}
}
else if (indexOfFirstNonBlankLine != 1) {
log(ast, MSG_JAVADOC_CONTENT_SECOND_LINE);
}
}
}
}
/**
* Checks if a DetailAST of type {@code TokenTypes.BLOCK_COMMENT_BEGIN} span
* more than one line. The node always has at least one child of the type
* {@code TokenTypes.BLOCK_COMMENT_END}.
*
* @param node node to check
* @return {@code true} for multi-line comment nodes
*/
private static boolean isMultilineComment(DetailAST node) {
return !TokenUtil.areOnSameLine(node, node.getLastChild());
}
/**
* Returns the index of the first non-blank line.
* All lines consists only of asterisks and whitespaces are treated as blank.
*
* @param commentContent Javadoc content to process
* @return the index of the first non-blank line or {@code -1} if all lines are blank
*/
private static int findIndexOfFirstNonBlankLine(String commentContent) {
int lineNo = 0;
boolean noContent = true;
for (int i = 0; i < commentContent.length(); ++i) {
final char character = commentContent.charAt(i);
if (character == '\n') {
++lineNo;
}
else if (character != '*' && !Character.isWhitespace(character)) {
noContent = false;
break;
}
}
if (noContent) {
lineNo = -1;
}
return lineNo;
}
}