Skip to content

Commit

Permalink
Issue checkstyle#14726: new check: ConstructorsDeclarationGroupingCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopsss authored and rnveach committed May 20, 2024
1 parent 6bb41a4 commit b861547
Show file tree
Hide file tree
Showing 35 changed files with 1,023 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java</fileName>
<specifier>type.arguments.not.inferred</specifier>
<message>Could not infer type arguments for Optional.map</message>
<lineContent>.map(children::indexOf);</lineContent>
<details>
unsatisfiable constraint: @GuardedBy DetailAST &lt;: @GuardSatisfied Object
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java</fileName>
<specifier>methodref.param</specifier>
Expand Down
1 change: 1 addition & 0 deletions config/checkstyle-checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
<module name="AvoidDoubleBraceInitialization"/>
<module name="AvoidInlineConditionals"/>
<module name="AvoidNoArgumentSuperConstructorCall"/>
<module name="ConstructorsDeclarationGrouping"/>
<module name="CovariantEquals"/>
<module name="DeclarationOrder"/>
<module name="DefaultComesLast"/>
Expand Down
2 changes: 2 additions & 0 deletions config/checkstyle-non-main-files-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]avoidinlineconditionals.xml.template"/>
<suppress id="propertiesMacroMustExist"
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]avoidnoargumentsuperconstructorcall.xml.template"/>
<suppress id="propertiesMacroMustExist"
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]constructorsdeclarationgrouping.xml.template"/>
<suppress id="propertiesMacroMustExist"
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]covariantequals.xml.template"/>
<suppress id="propertiesMacroMustExist"
Expand Down
2 changes: 2 additions & 0 deletions config/checkstyle-resources-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]equalsavoidnull[\\/]InputEqualsAvoidNullIgnoreCase\.java"/>
<suppress checks="FileLength"
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]equalsavoidnull[\\/]InputEqualsAvoidNull\.java"/>
<suppress checks="FileLength"
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]constructorsdeclarationgrouping[\\/]InputConstructorsDeclarationGrouping\.java"/>
<suppress checks="FileLength"
files="[\\/]test[\\/]resources[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]checks[\\/]coding[\\/]covariantequals[\\/]InputCovariantEquals\.java"/>
<suppress checks="FileLength"
Expand Down
1 change: 1 addition & 0 deletions config/jsoref-spellchecker/whitelist.words
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ config
configurationloader
Connell's
constantname
constructorsdeclarationgrouping
Contextualizable
contextualization
contextualized
Expand Down
2 changes: 2 additions & 0 deletions config/linkcheck-suppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidDoubleBraceInitializationCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/AvoidNoArgumentSuperConstructorCallCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/CovariantEqualsCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.ScopeState.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
Expand Down Expand Up @@ -951,6 +952,7 @@
<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.
<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.
<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.
<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.
<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.
<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.
<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.
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,7 @@
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidNoArgumentSuperConstructorCallCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck*</param>
Expand All @@ -3288,6 +3289,7 @@
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidDoubleBraceInitializationCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.AvoidNoArgumentSuperConstructorCallCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheckTest</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2024 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 org.checkstyle.suppressionxpathfilter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck;

public class XpathRegressionConstructorsDeclarationGroupingTest extends AbstractXpathTestSupport {

private final Class<ConstructorsDeclarationGroupingCheck> clazz =
ConstructorsDeclarationGroupingCheck.class;

@Override
protected String getCheckName() {
return clazz.getSimpleName();
}

@Test
public void testClass() throws Exception {
final File fileToProcess = new File(
getPath("InputXpathConstructorsDeclarationGroupingClass.java"));

final DefaultConfiguration moduleConfig = createModuleConfig(clazz);

final String[] expectedViolation = {
"10:5: " + getCheckMessage(clazz,
ConstructorsDeclarationGroupingCheck.MSG_KEY, 6),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]",
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
+ "/OBJBLOCK/CTOR_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
+ "/MODIFIERS",
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
+ "/OBJBLOCK/CTOR_DEF/IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingClass']"

);

runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
}

@Test
public void testEnum() throws Exception {
final File fileToProcess = new File(
getPath("InputXpathConstructorsDeclarationGroupingEnum.java"));

final DefaultConfiguration moduleConfig = createModuleConfig(clazz);

final String[] expectedViolation = {
"12:5: " + getCheckMessage(clazz,
ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
+ "/OBJBLOCK/CTOR_DEF"
+ "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]",

"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
+ "/OBJBLOCK/CTOR_DEF"
+ "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
+ "/MODIFIERS",

"/COMPILATION_UNIT/ENUM_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
+ "/OBJBLOCK/CTOR_DEF/IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingEnum']"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
}

@Test
public void testRecords() throws Exception {
final File fileToProcess = new File(
getNonCompilablePath("InputXpathConstructorsDeclarationGroupingRecords.java"));

final DefaultConfiguration moduleConfig = createModuleConfig(clazz);

final String[] expectedViolation = {
"14:5: " + getCheckMessage(clazz,
ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]/MODIFIERS",

"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
+ "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
+ "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]"
+ "/MODIFIERS/LITERAL_PUBLIC"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//non-compiled with javac: Compilable with Java14

package org.checkstyle.suppressionxpathfilter.constructorsdeclarationgrouping;

public class InputXpathConstructorsDeclarationGroupingRecords {
public record MyRecord(int x, int y) {

public MyRecord(int a) {
this(a,a);
}

void foo() {}

public MyRecord {} // warn
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.checkstyle.suppressionxpathfilter.constructorsdeclarationgrouping;

public class InputXpathConstructorsDeclarationGroupingClass {
InputXpathConstructorsDeclarationGroupingClass() {}

InputXpathConstructorsDeclarationGroupingClass(int a) {}

int x;

InputXpathConstructorsDeclarationGroupingClass(String str) {} // warn
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.checkstyle.suppressionxpathfilter.constructorsdeclarationgrouping;

public enum InputXpathConstructorsDeclarationGroupingEnum {
ONE;

InputXpathConstructorsDeclarationGroupingEnum() {}

InputXpathConstructorsDeclarationGroupingEnum(String str) {}

int f;

InputXpathConstructorsDeclarationGroupingEnum(int x) {} // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ private static void fillChecksFromCodingPackage() {
BASE_PACKAGE + ".checks.coding.AvoidInlineConditionalsCheck");
NAME_TO_FULL_MODULE_NAME.put("AvoidNoArgumentSuperConstructorCallCheck",
BASE_PACKAGE + ".checks.coding.AvoidNoArgumentSuperConstructorCallCheck");
NAME_TO_FULL_MODULE_NAME.put("ConstructorsDeclarationGroupingCheck",
BASE_PACKAGE + ".checks.coding.ConstructorsDeclarationGroupingCheck");
NAME_TO_FULL_MODULE_NAME.put("CovariantEqualsCheck",
BASE_PACKAGE + ".checks.coding.CovariantEqualsCheck");
NAME_TO_FULL_MODULE_NAME.put("DeclarationOrderCheck",
Expand Down
Loading

0 comments on commit b861547

Please sign in to comment.