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
20 changed files
with
489 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
52 changes: 52 additions & 0 deletions
52
...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,52 @@ | ||
package org.checkstyle.suppressionxpathfilter; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck; | ||
import com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheck; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class XpathRegressionConstructorsDeclarationGroupingTest extends AbstractXpathTestSupport{ | ||
|
||
private final Class<ConstructorsDeclarationGroupingCheck> clazz = | ||
ConstructorsDeclarationGroupingCheck.class; | ||
@Override | ||
protected String getCheckName() { | ||
return clazz.getSimpleName(); | ||
} | ||
|
||
@Test | ||
public void testOne() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionConstructorsDeclarationGrouping1.java")); | ||
|
||
final DefaultConfiguration moduleConfig = createModuleConfig(clazz); | ||
|
||
final String[] expectedViolation = { | ||
"10:5: " + getCheckMessage(clazz, | ||
ConstructorsDeclarationGroupingCheck.MSG_KEY), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Arrays.asList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']]", | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']]" | ||
+ "/MODIFIERS", | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']]" | ||
+ "/OBJBLOCK/CTOR_DEF/IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping1']" | ||
|
||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...uctorsdeclarationgrouping/SuppressionXpathRegressionConstructorsDeclarationGrouping1.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 SuppressionXpathRegressionConstructorsDeclarationGrouping1 { | ||
SuppressionXpathRegressionConstructorsDeclarationGrouping1() {} | ||
|
||
SuppressionXpathRegressionConstructorsDeclarationGrouping1(int a) {} | ||
|
||
int x; | ||
|
||
SuppressionXpathRegressionConstructorsDeclarationGrouping1(String str) {} // 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
107 changes: 107 additions & 0 deletions
107
...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,107 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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 com.puppycrawl.tools.checkstyle.FileStatefulCheck; | ||
import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* <p> | ||
* Checks that overloaded constructors are grouped together. | ||
* If there is anything between overloaded constructors ( obviously, expect comments ) | ||
* then this check will give an error. | ||
* </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.15.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 if the constructor has occurred before. */ | ||
private boolean isCtorOccurred; | ||
|
||
/** Specifies the previous IDENT name. */ | ||
private String previousIdent; | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[]{ | ||
TokenTypes.CTOR_DEF, | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
final DetailAST previousSibling = ast.getPreviousSibling(); | ||
|
||
if (previousSibling != null) { | ||
final DetailAST parent = ast.getParent().getParent(); | ||
final String ident = parent.findFirstToken(TokenTypes.IDENT).getText(); | ||
|
||
if (!Objects.equals(ident, previousIdent)) { | ||
isCtorOccurred = false; | ||
previousIdent = ident; | ||
} | ||
|
||
final int siblingType = previousSibling.getType(); | ||
|
||
if (siblingType != TokenTypes.CTOR_DEF && !isCtorOccurred) { | ||
isCtorOccurred = true; | ||
} | ||
else if (siblingType != TokenTypes.CTOR_DEF) { | ||
log(ast, MSG_KEY); | ||
} | ||
} | ||
|
||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...es/com/puppycrawl/tools/checkstyle/meta/checks/coding/ConstructorsDeclarationGrouping.xml
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle-metadata> | ||
<module> | ||
<check fully-qualified-name="com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck" | ||
name="ConstructorsDeclarationGrouping" | ||
parent="com.puppycrawl.tools.checkstyle.TreeWalker"> | ||
<description><p> | ||
Checks that overloaded constructors are grouped together. | ||
If there is anything between overloaded constructors ( obviously, expect comments ) | ||
then this check will give an error. | ||
</p></description> | ||
<message-keys> | ||
<message-key key="constructors.declaration.grouping"/> | ||
</message-keys> | ||
</check> | ||
</module> | ||
</checkstyle-metadata> |
17 changes: 17 additions & 0 deletions
17
...m/puppycrawl/tools/checkstyle/meta/checks/coding/ConstructorsDeclarationGroupingCheck.xml
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<checkstyle-metadata> | ||
<module> | ||
<check fully-qualified-name="com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck" | ||
name="ConstructorsDeclarationGrouping" | ||
parent="com.puppycrawl.tools.checkstyle.TreeWalker"> | ||
<description><p> | ||
Checks that overloaded constructors are grouped together. | ||
If there is anything between overloaded constructors ( obviously, expect comments ) | ||
then this check will give an error. | ||
</p></description> | ||
<message-keys> | ||
<message-key key="constructors.declaration.grouping"/> | ||
</message-keys> | ||
</check> | ||
</module> | ||
</checkstyle-metadata> |
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
25 changes: 25 additions & 0 deletions
25
...m/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheckTest.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,25 @@ | ||
package com.puppycrawl.tools.checkstyle.checks.coding; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.puppycrawl.tools.checkstyle.checks.coding.ConstructorsDeclarationGroupingCheck.MSG_KEY; | ||
|
||
public class ConstructorsDeclarationGroupingCheckTest extends AbstractModuleTestSupport { | ||
@Override | ||
protected String getPackageLocation() { | ||
return "com/puppycrawl/tools/checkstyle/checks/coding/constructorsdeclarationgrouping"; | ||
} | ||
|
||
@Test | ||
public void testDefault() throws Exception { | ||
final String[] expected = { | ||
"23:5: " + getCheckMessage(MSG_KEY), | ||
"27:5: " + getCheckMessage(MSG_KEY), | ||
"40:9: " + getCheckMessage(MSG_KEY), | ||
"51:13: " + getCheckMessage(MSG_KEY), | ||
}; | ||
verifyWithInlineConfigParser( | ||
getPath("InputConstructorsDeclarationGrouping.java"), expected); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...e/checks/coding/constructorsdeclarationgrouping/InputConstructorsDeclarationGrouping.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,55 @@ | ||
/* | ||
ConstructorsDeclarationGrouping | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.constructorsdeclarationgrouping; | ||
|
||
public class InputConstructorsDeclarationGrouping { | ||
|
||
int a; | ||
|
||
int b; | ||
|
||
void foo() {} | ||
|
||
InputConstructorsDeclarationGrouping() {} | ||
|
||
InputConstructorsDeclarationGrouping(String a) {} | ||
|
||
void foo2() {} | ||
|
||
InputConstructorsDeclarationGrouping(int a) {} // violation | ||
|
||
int abc; | ||
|
||
InputConstructorsDeclarationGrouping(double x) {} // violation | ||
|
||
private enum Testing { | ||
|
||
one; | ||
|
||
int x; | ||
Testing() {} | ||
|
||
Testing(String f) {} | ||
|
||
String str; | ||
|
||
Testing(int x) {} // violation | ||
|
||
private abstract class Yo { | ||
int x; | ||
|
||
Yo(){} | ||
|
||
Yo(int x) {} | ||
|
||
String neko; | ||
|
||
Yo(String g) {} // violation | ||
} | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...m/puppycrawl/tools/checkstyle/checks/coding/constructorsdeclarationgrouping/Example1.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,25 @@ | ||
/*xml | ||
<module name="Checker"> | ||
<module name="TreeWalker"> | ||
<module name="ConstructorsDeclarationGrouping"/> | ||
</module> | ||
</module> | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.constructorsdeclarationgrouping; | ||
|
||
// xdoc section -- start | ||
public class Example1 { | ||
int x; | ||
|
||
void foo() {} | ||
|
||
Example1() {} | ||
|
||
Example1(int a){} | ||
|
||
void foo2() {} | ||
|
||
Example1(String str) {} // violation | ||
} | ||
// xdoc section -- end |
Oops, something went wrong.