Skip to content

Commit b861547

Browse files
Zopsssrnveach
authored andcommitted
Issue #14726: new check: ConstructorsDeclarationGroupingCheck
1 parent 6bb41a4 commit b861547

35 files changed

Lines changed: 1023 additions & 3 deletions

File tree

config/checker-framework-suppressions/checker-lock-tainting-suppressions.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,16 @@
447447
</details>
448448
</checkerFrameworkError>
449449

450+
<checkerFrameworkError unstable="false">
451+
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java</fileName>
452+
<specifier>type.arguments.not.inferred</specifier>
453+
<message>Could not infer type arguments for Optional.map</message>
454+
<lineContent>.map(children::indexOf);</lineContent>
455+
<details>
456+
unsatisfiable constraint: @GuardedBy DetailAST &lt;: @GuardSatisfied Object
457+
</details>
458+
</checkerFrameworkError>
459+
450460
<checkerFrameworkError unstable="false">
451461
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java</fileName>
452462
<specifier>methodref.param</specifier>

config/checkstyle-checks.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
<module name="AvoidDoubleBraceInitialization"/>
369369
<module name="AvoidInlineConditionals"/>
370370
<module name="AvoidNoArgumentSuperConstructorCall"/>
371+
<module name="ConstructorsDeclarationGrouping"/>
371372
<module name="CovariantEquals"/>
372373
<module name="DeclarationOrder"/>
373374
<module name="DefaultComesLast"/>

config/checkstyle-non-main-files-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]avoidinlineconditionals.xml.template"/>
211211
<suppress id="propertiesMacroMustExist"
212212
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]avoidnoargumentsuperconstructorcall.xml.template"/>
213+
<suppress id="propertiesMacroMustExist"
214+
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]constructorsdeclarationgrouping.xml.template"/>
213215
<suppress id="propertiesMacroMustExist"
214216
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]covariantequals.xml.template"/>
215217
<suppress id="propertiesMacroMustExist"

config/checkstyle-resources-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@
502502
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]equalsavoidnull[\\/]InputEqualsAvoidNullIgnoreCase\.java"/>
503503
<suppress checks="FileLength"
504504
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]equalsavoidnull[\\/]InputEqualsAvoidNull\.java"/>
505+
<suppress checks="FileLength"
506+
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]constructorsdeclarationgrouping[\\/]InputConstructorsDeclarationGrouping\.java"/>
505507
<suppress checks="FileLength"
506508
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]covariantequals[\\/]InputCovariantEquals\.java"/>
507509
<suppress checks="FileLength"

config/jsoref-spellchecker/whitelist.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ config
240240
configurationloader
241241
Connell's
242242
constantname
243+
constructorsdeclarationgrouping
243244
Contextualizable
244245
contextualization
245246
contextualized

config/linkcheck-suppressions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidDoubleBraceInitializationCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
225225
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
226226
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidNoArgumentSuperConstructorCallCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
227+
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
227228
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/CovariantEqualsCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
228229
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.ScopeState.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
229230
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
@@ -951,6 +952,7 @@
951952
<a href="com/puppycrawl/tools/checkstyle/checks/coding/AvoidDoubleBraceInitializationCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/AvoidDoubleBraceInitializationCheck.html#%3Cinit%3E()</a>: doesn't exist.
952953
<a href="com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheck.html#%3Cinit%3E()</a>: doesn't exist.
953954
<a href="com/puppycrawl/tools/checkstyle/checks/coding/AvoidNoArgumentSuperConstructorCallCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/AvoidNoArgumentSuperConstructorCallCheck.html#%3Cinit%3E()</a>: doesn't exist.
955+
<a href="com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.html#%3Cinit%3E()</a>: doesn't exist.
954956
<a href="com/puppycrawl/tools/checkstyle/checks/coding/CovariantEqualsCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/CovariantEqualsCheck.html#%3Cinit%3E()</a>: doesn't exist.
955957
<a href="com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.ScopeState.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.ScopeState.html#%3Cinit%3E()</a>: doesn't exist.
956958
<a href="com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.html#%3Cinit%3E()</a>: doesn't exist.

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,6 +3264,7 @@
32643264
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheck*</param>
32653265
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheck*</param>
32663266
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidNoArgumentSuperConstructorCallCheck*</param>
3267+
<param>com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck*</param>
32673268
<param>com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheck*</param>
32683269
<param>com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck*</param>
32693270
<param>com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck*</param>
@@ -3288,6 +3289,7 @@
32883289
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheckTest</param>
32893290
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheckTest</param>
32903291
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidNoArgumentSuperConstructorCallCheckTest</param>
3292+
<param>com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheckTest</param>
32913293
<param>com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheckTest</param>
32923294
<param>com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheckTest</param>
32933295
<param>com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheckTest</param>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3+
// Copyright (C) 2001-2024 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
///////////////////////////////////////////////////////////////////////////////////////////////
19+
20+
package org.checkstyle.suppressionxpathfilter;
21+
22+
import java.io.File;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29+
import com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck;
30+
31+
public class XpathRegressionConstructorsDeclarationGroupingTest extends AbstractXpathTestSupport {
32+
33+
private final Class<ConstructorsDeclarationGroupingCheck> clazz =
34+
ConstructorsDeclarationGroupingCheck.class;
35+
36+
@Override
37+
protected String getCheckName() {
38+
return clazz.getSimpleName();
39+
}
40+
41+
@Test
42+
public void testClass() throws Exception {
43+
final File fileToProcess = new File(
44+
getPath("InputXpathConstructorsDeclarationGroupingClass.java"));
45+
46+
final DefaultConfiguration moduleConfig = createModuleConfig(clazz);
47+
48+
final String[] expectedViolation = {
49+
"10:5: " + getCheckMessage(clazz,
50+
ConstructorsDeclarationGroupingCheck.MSG_KEY, 6),
51+
};
52+
53+
final List<String> expectedXpathQueries = Arrays.asList(
54+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
56+
+ "/OBJBLOCK/CTOR_DEF[./IDENT"
57+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]",
58+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
60+
+ "/OBJBLOCK/CTOR_DEF[./IDENT"
61+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
62+
+ "/MODIFIERS",
63+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
65+
+ "/OBJBLOCK/CTOR_DEF/IDENT"
66+
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']"
67+
68+
);
69+
70+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
71+
}
72+
73+
@Test
74+
public void testEnum() throws Exception {
75+
final File fileToProcess = new File(
76+
getPath("InputXpathConstructorsDeclarationGroupingEnum.java"));
77+
78+
final DefaultConfiguration moduleConfig = createModuleConfig(clazz);
79+
80+
final String[] expectedViolation = {
81+
"12:5: " + getCheckMessage(clazz,
82+
ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
83+
};
84+
85+
final List<String> expectedXpathQueries = Arrays.asList(
86+
"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
87+
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
88+
+ "/OBJBLOCK/CTOR_DEF"
89+
+ "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]",
90+
91+
"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
92+
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
93+
+ "/OBJBLOCK/CTOR_DEF"
94+
+ "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
95+
+ "/MODIFIERS",
96+
97+
"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
98+
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
99+
+ "/OBJBLOCK/CTOR_DEF/IDENT"
100+
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']"
101+
);
102+
103+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
104+
}
105+
106+
@Test
107+
public void testRecords() throws Exception {
108+
final File fileToProcess = new File(
109+
getNonCompilablePath("InputXpathConstructorsDeclarationGroupingRecords.java"));
110+
111+
final DefaultConfiguration moduleConfig = createModuleConfig(clazz);
112+
113+
final String[] expectedViolation = {
114+
"14:5: " + getCheckMessage(clazz,
115+
ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
116+
};
117+
118+
final List<String> expectedXpathQueries = Arrays.asList(
119+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
120+
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
121+
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
122+
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]",
123+
124+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
125+
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
126+
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
127+
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]/MODIFIERS",
128+
129+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
130+
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
131+
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
132+
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]"
133+
+ "/MODIFIERS/LITERAL_PUBLIC"
134+
);
135+
136+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
137+
}
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//non-compiled with javac: Compilable with Java14
2+
3+
package org.checkstyle.suppressionxpathfilter.constructorsdeclarationgrouping;
4+
5+
public class InputXpathConstructorsDeclarationGroupingRecords {
6+
public record MyRecord(int x, int y) {
7+
8+
public MyRecord(int a) {
9+
this(a,a);
10+
}
11+
12+
void foo() {}
13+
14+
public MyRecord {} // warn
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.checkstyle.suppressionxpathfilter.constructorsdeclarationgrouping;
2+
3+
public class InputXpathConstructorsDeclarationGroupingClass {
4+
InputXpathConstructorsDeclarationGroupingClass() {}
5+
6+
InputXpathConstructorsDeclarationGroupingClass(int a) {}
7+
8+
int x;
9+
10+
InputXpathConstructorsDeclarationGroupingClass(String str) {} // warn
11+
}

0 commit comments

Comments
 (0)