-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
MissingJavadocMethodCheck.java
469 lines (435 loc) · 14.9 KB
/
MissingJavadocMethodCheck.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
////////////////////////////////////////////////////////////////////////////////
// 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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.Scope;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
/**
* <p>
* Checks for missing Javadoc comments for a method or constructor. The scope to verify is
* specified using the {@code Scope} class and defaults to {@code Scope.PUBLIC}. To verify
* another scope, set property scope to a different
* <a href="https://checkstyle.org/property_types.html#Scope">scope</a>.
* </p>
* <p>
* Javadoc is not required on a method that is tagged with the {@code @Override} annotation.
* However under Java 5 it is not possible to mark a method required for an interface (this
* was <i>corrected</i> under Java 6). Hence Checkstyle supports using the convention of using
* a single {@code {@inheritDoc}} tag instead of all the other tags.
* </p>
* <p>
* For getters and setters for the property {@code allowMissingPropertyJavadoc}, the methods must
* match exactly the structures below.
* </p>
* <pre>
* public void setNumber(final int number)
* {
* mNumber = number;
* }
*
* public int getNumber()
* {
* return mNumber;
* }
*
* public boolean isSomething()
* {
* return false;
* }
* </pre>
* <ul>
* <li>
* Property {@code minLineCount} - Control the minimal amount of lines in method to allow no
* documentation. Default value is {@code -1}.
* </li>
* <li>
* Property {@code allowedAnnotations} - Configure the list of annotations that allow missed
* documentation. Default value is {@code Override}.
* </li>
* <li>
* Property {@code scope} - Specify the visibility scope where Javadoc comments are checked.
* Default value is {@code public}.
* </li>
* <li>
* Property {@code excludeScope} - Specify the visibility scope where Javadoc comments are
* not checked. Default value is {@code null}.
* </li>
* <li>
* Property {@code allowMissingPropertyJavadoc} - Control whether to allow missing Javadoc on
* accessor methods for properties (setters and getters). Default value is {@code false}.
* </li>
* <li>
* Property {@code ignoreMethodNamesRegex} - ignore method whose names are matching specified
* regex. Default value is {@code null}.
* </li>
* <li>
* Property {@code tokens} - tokens to check Default value is:
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
* METHOD_DEF</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">
* CTOR_DEF</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">
* ANNOTATION_FIELD_DEF</a>.
* </li>
* </ul>
* <p>
* To configure the default check:
* </p>
* <pre>
* <module name="MissingJavadocMethod"/>
* </pre>
* <p>
* Example:
* </p>
* <pre>
* public class Test {
* public Test() {} // violation, missing javadoc for constructor
* public void test() {} // violation, missing javadoc for method
* /**
* * Some description here.
* */
* public void test2() {} // OK
*
* @Override
* public String toString() { // OK
* return "Some string";
* }
*
* private void test1() {} // OK
* protected void test2() {} // OK
* void test3() {} // OK
* }
* </pre>
*
* <p>
* To configure the check for {@code private} scope:
* </p>
* <pre>
* <module name="MissingJavadocMethod">
* <property name="scope" value="private"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* private void test1() {} // violation, the private method is missing javadoc
* }
* </pre>
*
* <p>
* To configure the check for methods which are in {@code private}, but not in {@code protected}
* scope:
* </p>
* <pre>
* <module name="MissingJavadocMethod">
* <property name="scope" value="private"/>
* <property name="excludeScope" value="protected"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* private void test1() {} // violation, the private method is missing javadoc
* /**
* * Some description here
* */
* private void test1() {} // OK
* protected void test2() {} // OK
* }
* </pre>
*
* <p>
* To configure the check for ignoring methods named {@code foo(),foo1(),foo2()}, etc.:
* </p>
* <pre>
* <module name="MissingJavadocMethod">
* <property name="ignoreMethodNamesRegex" value="^foo.*$"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* public void test1() {} // violation, method is missing javadoc
* public void foo() {} // OK
* public void foobar() {} // OK
* }
* </pre>
*
* <p>
* To configure the check for ignoring missing javadoc for accessor methods:
* </p>
* <pre>
* <module name="MissingJavadocMethod">
* <property name="allowMissingPropertyJavadoc" value="true"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* private String text;
*
* public void test() {} // violation, method is missing javadoc
* public String getText() { return text; } // OK
* public void setText(String text) { this.text = text; } // OK
* }
* </pre>
*
* <p>
* To configure the check with annotations that allow missed documentation:
* </p>
* <pre>
* <module name="MissingJavadocMethod">
* <property name="allowedAnnotations" value="Override,Deprecated"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* public void test() {} // violation, method is missing javadoc
* @Override
* public void test1() {} // OK
* @Deprecated
* public void test2() {} // OK
* @SuppressWarnings
* public void test3() {} // violation, method is missing javadoc
* /**
* * Some description here.
* */
* @SuppressWarnings
* public void test4() {} // OK
* }
* </pre>
*
* @since 8.21
*/
@FileStatefulCheck
public class MissingJavadocMethodCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_JAVADOC_MISSING = "javadoc.missing";
/** Default value of minimal amount of lines in method to allow no documentation.*/
private static final int DEFAULT_MIN_LINE_COUNT = -1;
/** Specify the visibility scope where Javadoc comments are checked. */
private Scope scope = Scope.PUBLIC;
/** Specify the visibility scope where Javadoc comments are not checked. */
private Scope excludeScope;
/** Control the minimal amount of lines in method to allow no documentation.*/
private int minLineCount = DEFAULT_MIN_LINE_COUNT;
/**
* Control whether to allow missing Javadoc on accessor methods for
* properties (setters and getters).
*/
private boolean allowMissingPropertyJavadoc;
/** Ignore method whose names are matching specified regex. */
private Pattern ignoreMethodNamesRegex;
/** Configure the list of annotations that allow missed documentation. */
private List<String> allowedAnnotations = Collections.singletonList("Override");
/**
* Setter to configure the list of annotations that allow missed documentation.
*
* @param userAnnotations user's value.
*/
public void setAllowedAnnotations(String... userAnnotations) {
allowedAnnotations = Arrays.asList(userAnnotations);
}
/**
* Setter to ignore method whose names are matching specified regex.
*
* @param pattern a pattern.
*/
public void setIgnoreMethodNamesRegex(Pattern pattern) {
ignoreMethodNamesRegex = pattern;
}
/**
* Setter to control the minimal amount of lines in method to allow no documentation.
*
* @param value user's value.
*/
public void setMinLineCount(int value) {
minLineCount = value;
}
/**
* Setter to control whether to allow missing Javadoc on accessor methods for properties
* (setters and getters).
*
* @param flag a {@code Boolean} value
*/
public void setAllowMissingPropertyJavadoc(final boolean flag) {
allowMissingPropertyJavadoc = flag;
}
/**
* Setter to specify the visibility scope where Javadoc comments are checked.
*
* @param scope a scope.
*/
public void setScope(Scope scope) {
this.scope = scope;
}
/**
* Setter to specify the visibility scope where Javadoc comments are not checked.
*
* @param excludeScope a scope.
*/
public void setExcludeScope(Scope excludeScope) {
this.excludeScope = excludeScope;
}
@Override
public final int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
}
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
}
@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
};
}
@Override
public final void visitToken(DetailAST ast) {
final Scope theScope = calculateScope(ast);
if (shouldCheck(ast, theScope)) {
final FileContents contents = getFileContents();
final TextBlock textBlock = contents.getJavadocBefore(ast.getLineNo());
if (textBlock == null && !isMissingJavadocAllowed(ast)) {
log(ast, MSG_JAVADOC_MISSING);
}
}
}
/**
* Some javadoc.
*
* @param methodDef Some javadoc.
* @return Some javadoc.
*/
private static int getMethodsNumberOfLine(DetailAST methodDef) {
final int numberOfLines;
final DetailAST lcurly = methodDef.getLastChild();
final DetailAST rcurly = lcurly.getLastChild();
if (lcurly.getFirstChild() == rcurly) {
numberOfLines = 1;
}
else {
numberOfLines = rcurly.getLineNo() - lcurly.getLineNo() - 1;
}
return numberOfLines;
}
/**
* Checks if a missing Javadoc is allowed by the check's configuration.
*
* @param ast the tree node for the method or constructor.
* @return True if this method or constructor doesn't need Javadoc.
*/
private boolean isMissingJavadocAllowed(final DetailAST ast) {
return allowMissingPropertyJavadoc
&& (CheckUtil.isSetterMethod(ast) || CheckUtil.isGetterMethod(ast))
|| matchesSkipRegex(ast)
|| isContentsAllowMissingJavadoc(ast);
}
/**
* Checks if the Javadoc can be missing if the method or constructor is
* below the minimum line count or has a special annotation.
*
* @param ast the tree node for the method or constructor.
* @return True if this method or constructor doesn't need Javadoc.
*/
private boolean isContentsAllowMissingJavadoc(DetailAST ast) {
return (ast.getType() == TokenTypes.METHOD_DEF || ast.getType() == TokenTypes.CTOR_DEF)
&& (getMethodsNumberOfLine(ast) <= minLineCount
|| AnnotationUtil.containsAnnotation(ast, allowedAnnotations));
}
/**
* Checks if the given method name matches the regex. In that case
* we skip enforcement of javadoc for this method
*
* @param methodDef {@link TokenTypes#METHOD_DEF METHOD_DEF}
* @return true if given method name matches the regex.
*/
private boolean matchesSkipRegex(DetailAST methodDef) {
boolean result = false;
if (ignoreMethodNamesRegex != null) {
final DetailAST ident = methodDef.findFirstToken(TokenTypes.IDENT);
final String methodName = ident.getText();
final Matcher matcher = ignoreMethodNamesRegex.matcher(methodName);
if (matcher.matches()) {
result = true;
}
}
return result;
}
/**
* Whether we should check this node.
*
* @param ast a given node.
* @param nodeScope the scope of the node.
* @return whether we should check a given node.
*/
private boolean shouldCheck(final DetailAST ast, final Scope nodeScope) {
final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
return (excludeScope == null
|| nodeScope != excludeScope
&& surroundingScope != excludeScope)
&& nodeScope.isIn(scope)
&& surroundingScope.isIn(scope);
}
/**
* Returns the scope for the method/constructor at the specified AST. If
* the method is in an interface or annotation block, the scope is assumed
* to be public.
*
* @param ast the token of the method/constructor
* @return the scope of the method/constructor
*/
private static Scope calculateScope(final DetailAST ast) {
final Scope scope;
if (ScopeUtil.isInAnnotationBlock(ast)) {
scope = Scope.PUBLIC;
}
else {
final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS);
final Scope modifiersScope = ScopeUtil.getScopeFromMods(mods);
if (modifiersScope == Scope.PACKAGE && ScopeUtil.isInInterfaceBlock(ast)) {
scope = Scope.PUBLIC;
}
else {
scope = modifiersScope;
}
}
return scope;
}
}