Skip to content

Commit 06c7bd7

Browse files
committed
Issue checkstyle#9280: New property 'accessModifiers' as substitution of 'scope' and 'excludeScope' in JavadocVariableCheck
1 parent f56bd4c commit 06c7bd7

27 files changed

+442
-436
lines changed

.ci/validation.sh

+13-5
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,17 @@ no-error-pmd)
216216
echo "CS_version: ${CS_POM_VERSION}"
217217
mvn -e --no-transfer-progress clean install -Pno-validations
218218
echo "Checkout target sources ..."
219-
checkout_from "https://github.com/pmd/build-tools.git"
219+
# checkout_from "https://github.com/pmd/build-tools.git"
220+
checkout_from "https://github.com/kkoutsilis/build-tools.git"
220221
cd .ci-temp/build-tools/
222+
git ls-remote
223+
git checkout "66d""ed33c74662cb3da612f3d34a5ae""fa""a629b443"
221224
mvn -e --no-transfer-progress install
222225
cd ..
223-
git clone https://github.com/pmd/pmd.git
226+
git clone https://github.com/kkoutsilis/pmd.git
224227
cd pmd
228+
git ls-remote
229+
git checkout "fa6a862ac8278906d7bcf21852f6552d27a46a73"
225230
./mvnw verify --show-version --errors --batch-mode --no-transfer-progress \
226231
-DskipTests \
227232
-Dmaven.javadoc.skip=true \
@@ -279,8 +284,10 @@ no-error-xwiki)
279284
echo "version:${CS_POM_VERSION} antlr4:${ANTLR4_VERSION}"
280285
mvn -e --no-transfer-progress clean install -Pno-validations
281286
echo "Checkout target sources ..."
282-
checkout_from "https://github.com/xwiki/xwiki-commons.git"
287+
checkout_from "https://github.com/kkoutsilis/xwiki-commons"
283288
cd .ci-temp/xwiki-commons
289+
git ls-remote
290+
git checkout "00b68b71a34d184a1c3b42520bff""afc61151ca32"
284291
# Build custom Checkstyle rules
285292
mvn -e --no-transfer-progress -f \
286293
xwiki-commons-tools/xwiki-commons-tool-verification-resources/pom.xml \
@@ -311,8 +318,10 @@ no-error-xwiki)
311318
cd ..
312319
removeFolderWithProtectedFiles xwiki-rendering
313320
cd ..
314-
checkout_from https://github.com/xwiki/xwiki-platform.git
321+
checkout_from https://github.com/kkoutsilis/xwiki-platform.git
315322
cd .ci-temp/xwiki-platform
323+
git ls-remote
324+
git checkout "6c99a70983ac""b371015f231ae""cee6bd20270fb5b"
316325
# Validate xwiki-platform
317326
mvn -e --no-transfer-progress checkstyle:check@default -Dcheckstyle.version="${CS_POM_VERSION}"
318327
cd ..
@@ -758,7 +767,6 @@ no-error-spring-cloud-gcp)
758767
echo "Checkout target sources ..."
759768
checkout_from https://github.com/googlecloudplatform/spring-cloud-gcp
760769
cd .ci-temp/spring-cloud-gcp
761-
git checkout "7c99f37087ac8f""eb""db""f7e185375a217b744895a4"
762770
mvn -e --no-transfer-progress checkstyle:check@checkstyle-validation \
763771
-Dmaven-checkstyle-plugin.version=3.1.1 \
764772
-Dpuppycrawl-tools-checkstyle.version="${CS_POM_VERSION}"

config/checker-framework-suppressions/checker-nullness-optional-interning-suppressions.xml

-7
Original file line numberDiff line numberDiff line change
@@ -4272,13 +4272,6 @@
42724272
<lineContent>private Pattern versionFormat;</lineContent>
42734273
</checkerFrameworkError>
42744274

4275-
<checkerFrameworkError unstable="false">
4276-
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java</fileName>
4277-
<specifier>initialization.field.uninitialized</specifier>
4278-
<message>the default constructor does not initialize field excludeScope</message>
4279-
<lineContent>private Scope excludeScope;</lineContent>
4280-
</checkerFrameworkError>
4281-
42824275
<checkerFrameworkError unstable="false">
42834276
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java</fileName>
42844277
<specifier>initialization.field.uninitialized</specifier>

config/jsoref-spellchecker/whitelist.words

+1
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ KDoc
755755
keygen
756756
keyname
757757
keyscan
758+
kkoutsilis
758759
konstantinos
759760
Kordas
760761
Kotlin

src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java

+41-38
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,37 @@
1919

2020
package com.puppycrawl.tools.checkstyle.checks.javadoc;
2121

22+
import java.util.Arrays;
2223
import java.util.regex.Pattern;
2324

2425
import com.puppycrawl.tools.checkstyle.StatelessCheck;
2526
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
2627
import com.puppycrawl.tools.checkstyle.api.DetailAST;
2728
import com.puppycrawl.tools.checkstyle.api.FileContents;
28-
import com.puppycrawl.tools.checkstyle.api.Scope;
2929
import com.puppycrawl.tools.checkstyle.api.TextBlock;
3030
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31+
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
32+
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
3133
import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
34+
import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
3235

3336
/**
3437
* <div>
3538
* Checks that a variable has a Javadoc comment. Ignores {@code serialVersionUID} fields.
3639
* </div>
3740
* <ul>
3841
* <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}.
4346
* </li>
4447
* <li>
4548
* Property {@code ignoreNamePattern} - Specify the regexp to define variable names to ignore.
4649
* Type is {@code java.util.regex.Pattern}.
4750
* Default value is {@code null}.
4851
* </li>
4952
* <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>
5553
* Property {@code tokens} - tokens to check
5654
* Type is {@code java.lang.String[]}.
5755
* Validation type is {@code tokenSet}.
@@ -84,35 +82,28 @@ public class JavadocVariableCheck
8482
* A key is pointing to the warning message text in "messages.properties"
8583
* file.
8684
*/
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;
9185

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+
};
9494

9595
/** Specify the regexp to define variable names to ignore. */
9696
private Pattern ignoreNamePattern;
9797

9898
/**
99-
* Setter to specify the visibility scope where Javadoc comments are checked.
99+
* Setter to access modifiers of methods where parameters are checked.
100100
*
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
103103
*/
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);
116107
}
117108

118109
/**
@@ -176,6 +167,17 @@ private boolean isIgnored(DetailAST ast) {
176167
|| "serialVersionUID".equals(name);
177168
}
178169

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+
179181
/**
180182
* Whether we should check this node.
181183
*
@@ -185,14 +187,15 @@ private boolean isIgnored(DetailAST ast) {
185187
private boolean shouldCheck(final DetailAST ast) {
186188
boolean result = false;
187189
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+
}
194198
}
195199
return result;
196200
}
197-
198201
}

src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/javadoc/JavadocVariableCheck.xml

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
Checks that a variable has a Javadoc comment. Ignores {@code serialVersionUID} fields.
99
&lt;/div&gt;</description>
1010
<properties>
11-
<property name="excludeScope" type="com.puppycrawl.tools.checkstyle.api.Scope">
12-
<description>Specify the visibility scope where Javadoc
13-
comments are not checked.</description>
11+
<property default-value="public, protected, package, private"
12+
name="accessModifiers"
13+
type="com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]">
14+
<description>Access modifiers of methods where parameters are
15+
checked.</description>
1416
</property>
1517
<property name="ignoreNamePattern" type="java.util.regex.Pattern">
1618
<description>Specify the regexp to define variable names to ignore.</description>
1719
</property>
18-
<property default-value="private"
19-
name="scope"
20-
type="com.puppycrawl.tools.checkstyle.api.Scope">
21-
<description>Specify the visibility scope where Javadoc comments are checked.</description>
22-
</property>
2320
<property default-value="ENUM_CONSTANT_DEF"
2421
name="tokens"
2522
type="java.lang.String[]"

src/site/xdoc/checks/javadoc/javadocvariable.xml

+24-33
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
<th>since</th>
2727
</tr>
2828
<tr>
29-
<td>excludeScope</td>
30-
<td>Specify the visibility scope where Javadoc comments are not checked.</td>
31-
<td><a href="../../property_types.html#Scope">Scope</a></td>
32-
<td><code>null</code></td>
33-
<td>3.4</td>
29+
<td>accessModifiers</td>
30+
<td>Access modifiers of methods where parameters are checked.</td>
31+
<td><a href="../../property_types.html#AccessModifierOption.5B.5D">AccessModifierOption[]</a></td>
32+
<td><code>public, protected, package, private</code></td>
33+
<td>10.22.0</td>
3434
</tr>
3535
<tr>
3636
<td>ignoreNamePattern</td>
@@ -39,13 +39,6 @@
3939
<td><code>null</code></td>
4040
<td>5.8</td>
4141
</tr>
42-
<tr>
43-
<td>scope</td>
44-
<td>Specify the visibility scope where Javadoc comments are checked.</td>
45-
<td><a href="../../property_types.html#Scope">Scope</a></td>
46-
<td><code>private</code></td>
47-
<td>3.0</td>
48-
</tr>
4942
<tr>
5043
<td>tokens</td>
5144
<td>tokens to check</td>
@@ -78,33 +71,33 @@
7871
</code></pre></div>
7972
<p id="Example1-code">
8073
By default, this setting will report a violation if
81-
there is no javadoc for any scope member.
74+
there is no javadoc for a member with any access modifier.
8275
</p>
8376
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
8477
public class Example1 {
85-
private int a; // violation
78+
private int a; // violation, 'Missing a Javadoc comment'
8679

8780
/**
8881
* Some description here
8982
*/
9083
private int b;
91-
protected int c; // violation
92-
public int d; // violation
93-
/*package*/ int e; // violation
84+
protected int c; // violation, 'Missing a Javadoc comment'
85+
public int d; // violation, 'Missing a Javadoc comment'
86+
/*package*/ int e; // violation, 'Missing a Javadoc comment'
9487

9588
}
9689
</code></pre></div>
9790

9891
<p id="Example2-config">
99-
To configure the check for <code>public</code>
100-
scope:
92+
To configure the check <code>public</code>
93+
access modifier:
10194
</p>
10295

10396
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
10497
&lt;module name="Checker"&gt;
10598
&lt;module name="TreeWalker"&gt;
10699
&lt;module name="JavadocVariable"&gt;
107-
&lt;property name="scope" value="public"/&gt;
100+
&lt;property name="accessModifiers" value="public"/&gt;
108101
&lt;/module&gt;
109102
&lt;/module&gt;
110103
&lt;/module&gt;
@@ -122,42 +115,40 @@ public class Example2 {
122115
*/
123116
private int b;
124117
protected int c;
125-
public int d; // violation
118+
public int d; // violation, 'Missing a Javadoc comment'
126119
/*package*/ int e;
127120
}
128121
</code></pre></div>
129122

130123
<p id="Example3-config">
131-
To configure the check for members which are in <code>private</code>, but not in
132-
<code>protected</code> scope:
124+
To configure the check for members which are in <code>private</code> or
125+
<code>package</code> access modifier:
133126
</p>
134127

135128
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
136129
&lt;module name="Checker"&gt;
137130
&lt;module name="TreeWalker"&gt;
138131
&lt;module name="JavadocVariable"&gt;
139-
&lt;property name="scope" value="private"/&gt;
140-
&lt;property name="excludeScope" value="protected"/&gt;
132+
&lt;property name="accessModifiers" value="private,package"/&gt;
141133
&lt;/module&gt;
142134
&lt;/module&gt;
143135
&lt;/module&gt;
144136
</code></pre></div>
145137
<p id="Example3-code">
146138
This setting will report a violation if there is no
147-
javadoc for <code>private</code> member and
148-
ignores <code>protected</code> member.
139+
javadoc for <code>private</code> or <code>package</code> member.
149140
</p>
150141
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
151142
public class Example3 {
152-
private int a; // violation
143+
private int a; // violation, 'Missing a Javadoc comment'
153144

154145
/**
155146
* Some description here
156147
*/
157148
private int b;
158149
protected int c;
159150
public int d;
160-
/*package*/ int e; // violation
151+
/*package*/ int e; // violation, 'Missing a Javadoc comment'
161152
}
162153
</code></pre></div>
163154
<p id="Example4-config">
@@ -181,15 +172,15 @@ public class Example3 {
181172
</p>
182173
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
183174
public class Example4 {
184-
private int a; // violation
175+
private int a; // violation, 'Missing a Javadoc comment'
185176

186177
/**
187178
* Some description here
188179
*/
189180
private int b;
190-
protected int c; // violation
191-
public int d; // violation
192-
/*package*/ int e; // violation
181+
protected int c; // violation, 'Missing a Javadoc comment'
182+
public int d; // violation, 'Missing a Javadoc comment'
183+
/*package*/ int e; // violation, 'Missing a Javadoc comment'
193184
}
194185
</code></pre></div>
195186
</subsection>

0 commit comments

Comments
 (0)