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} - Specify the set of access modifiers used to determine which
43
+ * fields should be checked. This includes both explicitly declared modifiers and implicit ones,
44
+ * such as package-private for fields without an explicit modifier.
45
+ * It also accounts for special cases where fields have implicit modifiers,
46
+ * such as {@code public static final} for interface fields and {@code public static}
47
+ * for enum constants. Only fields matching the specified modifiers will be analyzed.
48
+ * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]}.
49
+ * Default value is {@code public, protected, package, private}.
43
50
* </li>
44
51
* <li>
45
52
* Property {@code ignoreNamePattern} - Specify the regexp to define variable names to ignore.
46
53
* Type is {@code java.util.regex.Pattern}.
47
54
* Default value is {@code null}.
48
55
* </li>
49
56
* <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
57
* Property {@code tokens} - tokens to check
56
58
* Type is {@code java.lang.String[]}.
57
59
* Validation type is {@code tokenSet}.
@@ -84,35 +86,40 @@ public class JavadocVariableCheck
84
86
* A key is pointing to the warning message text in "messages.properties"
85
87
* file.
86
88
*/
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
89
92
- /** Specify the visibility scope where Javadoc comments are not checked. */
93
- private Scope excludeScope ;
90
+ public static final String MSG_JAVADOC_MISSING = "javadoc.missing" ;
91
+ /**
92
+ * Specify the set of access modifiers used to determine which fields should be checked.
93
+ * This includes both explicitly declared modifiers and implicit ones, such as package-private
94
+ * for fields without an explicit modifier. It also accounts for special cases where fields
95
+ * have implicit modifiers, such as {@code public static final} for interface fields and
96
+ * {@code public static} for enum constants.
97
+ * Only fields matching the specified modifiers will be analyzed.
98
+ */
99
+ private AccessModifierOption [] accessModifiers = {
100
+ AccessModifierOption .PUBLIC ,
101
+ AccessModifierOption .PROTECTED ,
102
+ AccessModifierOption .PACKAGE ,
103
+ AccessModifierOption .PRIVATE ,
104
+ };
94
105
95
106
/** Specify the regexp to define variable names to ignore. */
96
107
private Pattern ignoreNamePattern ;
97
108
98
109
/**
99
- * Setter to specify the visibility scope where Javadoc comments are checked.
110
+ * Setter to specify the set of access modifiers used to determine which fields should be
111
+ * checked. This includes both explicitly declared modifiers and implicit ones, such as
112
+ * package-private for fields without an explicit modifier. It also accounts for special
113
+ * cases where fields have implicit modifiers, such as {@code public static final}
114
+ * for interface fields and {@code public static} for enum constants.
115
+ * Only fields matching the specified modifiers will be analyzed.
100
116
*
101
- * @param scope a scope .
102
- * @since 3 .0
117
+ * @param accessModifiers access modifiers of fields to check .
118
+ * @since 10.22 .0
103
119
*/
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 ;
120
+ public void setAccessModifiers (AccessModifierOption ... accessModifiers ) {
121
+ this .accessModifiers =
122
+ UnmodifiableCollectionUtil .copyOfArray (accessModifiers , accessModifiers .length );
116
123
}
117
124
118
125
/**
@@ -176,6 +183,17 @@ private boolean isIgnored(DetailAST ast) {
176
183
|| "serialVersionUID" .equals (name );
177
184
}
178
185
186
+ /**
187
+ * Checks whether a method has the correct access modifier to be checked.
188
+ *
189
+ * @param accessModifier the access modifier of the method.
190
+ * @return whether the method matches the expected access modifier.
191
+ */
192
+ private boolean matchAccessModifiers (AccessModifierOption accessModifier ) {
193
+ return Arrays .stream (accessModifiers )
194
+ .anyMatch (modifier -> modifier == accessModifier );
195
+ }
196
+
179
197
/**
180
198
* Whether we should check this node.
181
199
*
@@ -185,14 +203,16 @@ private boolean isIgnored(DetailAST ast) {
185
203
private boolean shouldCheck (final DetailAST ast ) {
186
204
boolean result = false ;
187
205
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 ));
206
+ try {
207
+ final AccessModifierOption accessModifier =
208
+ CheckUtil .getAccessModifierFromModifiersToken (ast );
209
+ result = matchAccessModifiers (accessModifier );
210
+ }
211
+ catch (IllegalArgumentException ex ) {
212
+ // In case of ENUM_CONSTANT_DEF
213
+ result = true ;
214
+ }
194
215
}
195
216
return result ;
196
217
}
197
-
198
218
}
0 commit comments