forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
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
32 changed files
with
649 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
105 changes: 105 additions & 0 deletions
105
...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,105 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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 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, 6), | ||
}; | ||
|
||
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); | ||
} | ||
|
||
@Test | ||
public void testTwo() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionConstructorsDeclarationGrouping2.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='SuppressionXpathRegressionConstructorsDeclarationGrouping2']]" | ||
+ "/OBJBLOCK/CTOR_DEF" | ||
+ "[./IDENT[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping2']]", | ||
|
||
"/COMPILATION_UNIT/ENUM_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping2']]" | ||
+ "/OBJBLOCK/CTOR_DEF" | ||
+ "[./IDENT[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping2']]" | ||
+ "/MODIFIERS", | ||
|
||
"/COMPILATION_UNIT/ENUM_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping2']]" | ||
+ "/OBJBLOCK/CTOR_DEF/IDENT" | ||
+ "[@text='SuppressionXpathRegressionConstructorsDeclarationGrouping2']" | ||
); | ||
|
||
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 | ||
} |
13 changes: 13 additions & 0 deletions
13
...uctorsdeclarationgrouping/SuppressionXpathRegressionConstructorsDeclarationGrouping2.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 SuppressionXpathRegressionConstructorsDeclarationGrouping2 { | ||
ONE; | ||
|
||
SuppressionXpathRegressionConstructorsDeclarationGrouping2() {} | ||
|
||
SuppressionXpathRegressionConstructorsDeclarationGrouping2(String str) {} | ||
|
||
int f; | ||
|
||
SuppressionXpathRegressionConstructorsDeclarationGrouping2(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
104 changes: 104 additions & 0 deletions
104
...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,104 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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 overloaded constructors are grouped together. | ||
* If there is anything between overloaded constructors ( 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.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 parent's scope. | ||
*/ | ||
private final Map<DetailAST, Integer> parents = 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 visitToken(DetailAST ast) { | ||
final DetailAST parent = ast.getParent().getParent(); | ||
final Integer previousCtorLineNo = parents.get(parent); | ||
|
||
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); | ||
} | ||
|
||
parents.put(parent, ast.getLineNo()); | ||
} | ||
else { | ||
parents.put(parent, 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
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.