19
19
20
20
package com .puppycrawl .tools .checkstyle .checks .javadoc ;
21
21
22
+ import java .util .Arrays ;
22
23
import java .util .regex .Pattern ;
23
24
24
25
import com .puppycrawl .tools .checkstyle .StatelessCheck ;
25
26
import com .puppycrawl .tools .checkstyle .api .AbstractCheck ;
26
27
import com .puppycrawl .tools .checkstyle .api .DetailAST ;
27
28
import com .puppycrawl .tools .checkstyle .api .FileContents ;
28
- import com .puppycrawl .tools .checkstyle .api .Scope ;
29
29
import com .puppycrawl .tools .checkstyle .api .TextBlock ;
30
30
import com .puppycrawl .tools .checkstyle .api .TokenTypes ;
31
+ import com .puppycrawl .tools .checkstyle .checks .naming .AccessModifierOption ;
32
+ import com .puppycrawl .tools .checkstyle .utils .CheckUtil ;
31
33
import com .puppycrawl .tools .checkstyle .utils .ScopeUtil ;
34
+ import com .puppycrawl .tools .checkstyle .utils .UnmodifiableCollectionUtil ;
32
35
33
36
/**
34
37
* <div>
35
38
* Checks that a variable has a Javadoc comment. Ignores {@code serialVersionUID} fields.
36
39
* </div>
37
40
* <ul>
38
41
* <li>
39
- * Property {@code excludeScope } - Specify the visibility scope where Javadoc
40
- * comments are not checked.
41
- * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope }.
42
- * Default value is {@code null }.
42
+ * Property {@code accessModifiers } - Access modifiers of methods where parameters are
43
+ * checked.
44
+ * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[] }.
45
+ * Default value is {@code public, protected, package, private }.
43
46
* </li>
44
47
* <li>
45
48
* Property {@code ignoreNamePattern} - Specify the regexp to define variable names to ignore.
46
49
* Type is {@code java.util.regex.Pattern}.
47
50
* Default value is {@code null}.
48
51
* </li>
49
52
* <li>
50
- * Property {@code scope} - Specify the visibility scope where Javadoc comments are checked.
51
- * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope}.
52
- * Default value is {@code private}.
53
- * </li>
54
- * <li>
55
53
* Property {@code tokens} - tokens to check
56
54
* Type is {@code java.lang.String[]}.
57
55
* Validation type is {@code tokenSet}.
@@ -84,35 +82,28 @@ public class JavadocVariableCheck
84
82
* A key is pointing to the warning message text in "messages.properties"
85
83
* file.
86
84
*/
87
- public static final String MSG_JAVADOC_MISSING = "javadoc.missing" ;
88
-
89
- /** Specify the visibility scope where Javadoc comments are checked. */
90
- private Scope scope = Scope .PRIVATE ;
91
85
92
- /** Specify the visibility scope where Javadoc comments are not checked. */
93
- private Scope excludeScope ;
86
+ public static final String MSG_JAVADOC_MISSING = "javadoc.missing" ;
87
+ /** Access modifiers of methods where parameters are checked. */
88
+ private AccessModifierOption [] accessModifiers = {
89
+ AccessModifierOption .PUBLIC ,
90
+ AccessModifierOption .PROTECTED ,
91
+ AccessModifierOption .PACKAGE ,
92
+ AccessModifierOption .PRIVATE ,
93
+ };
94
94
95
95
/** Specify the regexp to define variable names to ignore. */
96
96
private Pattern ignoreNamePattern ;
97
97
98
98
/**
99
- * Setter to specify the visibility scope where Javadoc comments are checked.
99
+ * Setter to access modifiers of methods where parameters are checked.
100
100
*
101
- * @param scope a scope .
102
- * @since 3 .0
101
+ * @param accessModifiers access modifiers of methods which should be checked .
102
+ * @since 10.22 .0
103
103
*/
104
- public void setScope (Scope scope ) {
105
- this .scope = scope ;
106
- }
107
-
108
- /**
109
- * Setter to specify the visibility scope where Javadoc comments are not checked.
110
- *
111
- * @param excludeScope a scope.
112
- * @since 3.4
113
- */
114
- public void setExcludeScope (Scope excludeScope ) {
115
- this .excludeScope = excludeScope ;
104
+ public void setAccessModifiers (AccessModifierOption ... accessModifiers ) {
105
+ this .accessModifiers =
106
+ UnmodifiableCollectionUtil .copyOfArray (accessModifiers , accessModifiers .length );
116
107
}
117
108
118
109
/**
@@ -176,6 +167,17 @@ private boolean isIgnored(DetailAST ast) {
176
167
|| "serialVersionUID" .equals (name );
177
168
}
178
169
170
+ /**
171
+ * Checks whether a method has the correct access modifier to be checked.
172
+ *
173
+ * @param accessModifier the access modifier of the method.
174
+ * @return whether the method matches the expected access modifier.
175
+ */
176
+ private boolean matchAccessModifiers (final AccessModifierOption accessModifier ) {
177
+ return Arrays .stream (accessModifiers )
178
+ .anyMatch (modifier -> modifier == accessModifier );
179
+ }
180
+
179
181
/**
180
182
* Whether we should check this node.
181
183
*
@@ -185,14 +187,15 @@ private boolean isIgnored(DetailAST ast) {
185
187
private boolean shouldCheck (final DetailAST ast ) {
186
188
boolean result = false ;
187
189
if (!ScopeUtil .isInCodeBlock (ast ) && !isIgnored (ast )) {
188
- final Scope customScope = ScopeUtil .getScope (ast );
189
- final Scope surroundingScope = ScopeUtil .getSurroundingScope (ast );
190
- result = customScope .isIn (scope ) && surroundingScope .isIn (scope )
191
- && (excludeScope == null
192
- || !customScope .isIn (excludeScope )
193
- || !surroundingScope .isIn (excludeScope ));
190
+ try {
191
+ final AccessModifierOption accessModifier =
192
+ CheckUtil .getAccessModifierFromModifiersToken (ast );
193
+ result = matchAccessModifiers (accessModifier );
194
+ }
195
+ catch (IllegalArgumentException ex ) {
196
+ result = false ;
197
+ }
194
198
}
195
199
return result ;
196
200
}
197
-
198
201
}
0 commit comments