forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#14726: new check: ConstructorsDeclarationGroupingCheck
- Loading branch information
Showing
33 changed files
with
857 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...checkstyle/suppressionxpathfilter/XpathRegressionConstructorsDeclarationGroupingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ter/constructorsdeclarationgrouping/InputXpathConstructorsDeclarationGroupingRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//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 | ||
|
||
public MyRecord(int x, int y, int z) { | ||
this(x+y, z); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ilter/constructorsdeclarationgrouping/InputXpathConstructorsDeclarationGroupingClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
13 changes: 13 additions & 0 deletions
13
...filter/constructorsdeclarationgrouping/InputXpathConstructorsDeclarationGroupingEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...a/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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 com.puppycrawl.tools.checkstyle.checks.coding; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.puppycrawl.tools.checkstyle.FileStatefulCheck; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* <p> | ||
* Checks that all constructors are grouped together. | ||
* If there is any code between the constructors | ||
* then this check will give an error. | ||
* Comments between constructors are ignored. | ||
* </p> | ||
* <p> | ||
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | ||
* </p> | ||
* <p> | ||
* Violation Message Keys: | ||
* </p> | ||
* <ul> | ||
* <li> | ||
* {@code constructors.declaration.grouping} | ||
* </li> | ||
* </ul> | ||
* | ||
* @since 10.16.0 | ||
*/ | ||
|
||
@FileStatefulCheck | ||
public class ConstructorsDeclarationGroupingCheck extends AbstractCheck { | ||
|
||
/** | ||
* A key is pointing to the warning message text in "messages.properties" | ||
* file. | ||
*/ | ||
public static final String MSG_KEY = "constructors.declaration.grouping"; | ||
|
||
/** | ||
* Specifies different Object Blocks scope. | ||
*/ | ||
private final Map<DetailAST, Integer> allObjBlocks = new HashMap<>(); | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[] { | ||
TokenTypes.CTOR_DEF, | ||
TokenTypes.COMPACT_CTOR_DEF, | ||
}; | ||
} | ||
|
||
@Override | ||
public void beginTree(DetailAST rootAst) { | ||
allObjBlocks.clear(); | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
final DetailAST currObjBlock = ast.getParent(); | ||
final Integer previousCtorLineNo = allObjBlocks.get(currObjBlock); | ||
|
||
if (previousCtorLineNo != null) { | ||
final DetailAST previousSibling = ast.getPreviousSibling(); | ||
final int siblingType = previousSibling.getType(); | ||
final boolean isCtor = siblingType == TokenTypes.CTOR_DEF; | ||
final boolean isCompactCtor = siblingType == TokenTypes.COMPACT_CTOR_DEF; | ||
|
||
if (!isCtor && !isCompactCtor) { | ||
log(ast, MSG_KEY, previousCtorLineNo); | ||
} | ||
|
||
allObjBlocks.put(currObjBlock, ast.getLineNo()); | ||
} | ||
else { | ||
allObjBlocks.put(currObjBlock, ast.getLineNo()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.