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#6847: Add UnnecessarySemicolonAfterTypeMemberDeclara…
…tionCheck
- Loading branch information
Showing
10 changed files
with
457 additions
and
0 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
59 changes: 59 additions & 0 deletions
59
...ressionxpathfilter/XpathRegressionUnnecessarySemicolonAfterTypeMemberDeclarationTest.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,59 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2019 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.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck; | ||
|
||
public class XpathRegressionUnnecessarySemicolonAfterTypeMemberDeclarationTest | ||
extends AbstractXpathTestSupport { | ||
|
||
private static final Class<UnnecessarySemicolonAfterTypeMemberDeclarationCheck> CLASS = | ||
UnnecessarySemicolonAfterTypeMemberDeclarationCheck.class; | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return CLASS.getSimpleName(); | ||
} | ||
|
||
@Test | ||
public void testOne() throws Exception { | ||
final File fileToProcess = new File(getPath( | ||
"SuppressionXpathRegressionUnnecessarySemicolonAfterTypeMemberDeclaration.java")); | ||
final DefaultConfiguration moduleConfig = createModuleConfig(CLASS); | ||
final String[] expectedViolation = { | ||
"4:20: " + getCheckMessage(CLASS, | ||
UnnecessarySemicolonAfterTypeMemberDeclarationCheck.MSG_SEMI), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList("/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionUnnecessarySemicolonAfterTypeMemberDeclaration']]" | ||
+ "/OBJBLOCK/SEMI" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...declaration/SuppressionXpathRegressionUnnecessarySemicolonAfterTypeMemberDeclaration.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,5 @@ | ||
package org.checkstyle.suppressionxpathfilter.unnecessarysemicolonaftertypememberdeclaration; | ||
|
||
public class SuppressionXpathRegressionUnnecessarySemicolonAfterTypeMemberDeclaration { | ||
void method(){}; //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
177 changes: 177 additions & 0 deletions
177
...l/tools/checkstyle/checks/coding/UnnecessarySemicolonAfterTypeMemberDeclarationCheck.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,177 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2019 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.StatelessCheck; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; | ||
|
||
/** | ||
* <p> | ||
* Checks if unnecessary semicolon is used after code type member declaration. | ||
* The check will flag the following with warnings: | ||
* </p> | ||
* <pre> | ||
* class A { | ||
* ; // standalone semicolon | ||
* {}; // semicolon after init block | ||
* static {}; // semicolon after static init block | ||
* A(){}; // semicolon after constructor definition | ||
* void method() {}; // semicolon after method definition | ||
* int field = 10;; // semicolon after field declaration | ||
* } | ||
* </pre> | ||
* <p> | ||
* To configure the check: | ||
* </p> | ||
* <pre> | ||
* <module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/> | ||
* </pre> | ||
* | ||
* @since 8.23 | ||
*/ | ||
@StatelessCheck | ||
public final class UnnecessarySemicolonAfterTypeMemberDeclarationCheck extends AbstractCheck { | ||
|
||
/** | ||
* A key is pointing to the warning message text in "messages.properties" | ||
* file. | ||
*/ | ||
public static final String MSG_SEMI = "unnecessary.semicolon"; | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[] { | ||
TokenTypes.CLASS_DEF, | ||
TokenTypes.INTERFACE_DEF, | ||
TokenTypes.ENUM_DEF, | ||
TokenTypes.ANNOTATION_DEF, | ||
TokenTypes.VARIABLE_DEF, | ||
TokenTypes.STATIC_INIT, | ||
TokenTypes.INSTANCE_INIT, | ||
TokenTypes.CTOR_DEF, | ||
TokenTypes.METHOD_DEF, | ||
TokenTypes.ENUM_CONSTANT_DEF, | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
switch (ast.getType()) { | ||
case TokenTypes.CLASS_DEF: | ||
case TokenTypes.INTERFACE_DEF: | ||
case TokenTypes.ENUM_DEF: | ||
case TokenTypes.ANNOTATION_DEF: | ||
checkTypeDefinition(ast); | ||
break; | ||
case TokenTypes.VARIABLE_DEF: | ||
checkVariableDefinition(ast); | ||
break; | ||
case TokenTypes.ENUM_CONSTANT_DEF: | ||
checkEnumConstant(ast); | ||
break; | ||
default: | ||
checkTypeMember(ast); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if type member has unnecessary semicolon. | ||
* | ||
* @param ast type member | ||
*/ | ||
private void checkTypeMember(DetailAST ast) { | ||
if (isSemicolon(ast.getNextSibling())) { | ||
log(ast.getNextSibling(), MSG_SEMI); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if type definition has unnecessary semicolon. | ||
* | ||
* @param ast type definition | ||
*/ | ||
private void checkTypeDefinition(DetailAST ast) { | ||
if (!ScopeUtil.isOuterMostType(ast) && isSemicolon(ast.getNextSibling())) { | ||
log(ast.getNextSibling(), MSG_SEMI); | ||
} | ||
final DetailAST firstMember = | ||
ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild().getNextSibling(); | ||
if (isSemicolon(firstMember) && !ScopeUtil.isInEnumBlock(firstMember)) { | ||
log(firstMember, MSG_SEMI); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if variable definition has unnecessary semicolon. | ||
* | ||
* @param ast variable definition | ||
*/ | ||
private void checkVariableDefinition(DetailAST ast) { | ||
if (isFieldDeclaration(ast) && isSemicolon(ast.getNextSibling())) { | ||
log(ast.getNextSibling(), MSG_SEMI); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if enum constant has unnecessary semicolon. | ||
* | ||
* @param ast enum constant | ||
*/ | ||
private void checkEnumConstant(DetailAST ast) { | ||
final DetailAST next = ast.getNextSibling(); | ||
if (isSemicolon(next) && isSemicolon(next.getNextSibling())) { | ||
log(next.getNextSibling(), MSG_SEMI); | ||
} | ||
} | ||
|
||
/** | ||
* Checks that {@code ast} is a semicolon. | ||
* | ||
* @param ast token to check | ||
* @return true if ast is semicolon, false otherwise | ||
*/ | ||
private static boolean isSemicolon(DetailAST ast) { | ||
return ast.getType() == TokenTypes.SEMI; | ||
} | ||
|
||
/** | ||
* Checks that {@code variableDef} is a field declaration. | ||
* | ||
* @param variableDef token to check | ||
* @return true if variableDef is a field declaration, false otherwise | ||
*/ | ||
private static boolean isFieldDeclaration(DetailAST variableDef) { | ||
return isSemicolon(variableDef.getLastChild()); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...ols/checkstyle/checks/coding/UnnecessarySemicolonAfterTypeMemberDeclarationCheckTest.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,88 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2019 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 static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.MSG_SEMI; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
public class UnnecessarySemicolonAfterTypeMemberDeclarationCheckTest | ||
extends AbstractModuleTestSupport { | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/puppycrawl/tools/checkstyle/checks/coding/" | ||
+ "unnecessarysemicolonaftertypememberdeclaration"; | ||
} | ||
|
||
@Test | ||
public void testDefault() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(UnnecessarySemicolonAfterTypeMemberDeclarationCheck.class); | ||
|
||
final String[] expected = { | ||
"7:5: " + getCheckMessage(MSG_SEMI), | ||
"9:21: " + getCheckMessage(MSG_SEMI), | ||
"11:14: " + getCheckMessage(MSG_SEMI), | ||
"13:60: " + getCheckMessage(MSG_SEMI), | ||
"15:14: " + getCheckMessage(MSG_SEMI), | ||
"17:20: " + getCheckMessage(MSG_SEMI), | ||
"19:19: " + getCheckMessage(MSG_SEMI), | ||
"21:15: " + getCheckMessage(MSG_SEMI), | ||
"23:23: " + getCheckMessage(MSG_SEMI), | ||
"25:15: " + getCheckMessage(MSG_SEMI), | ||
"28:13: " + getCheckMessage(MSG_SEMI), | ||
"40:5: " + getCheckMessage(MSG_SEMI), | ||
"43:5: " + getCheckMessage(MSG_SEMI), | ||
}; | ||
|
||
verify(checkConfig, getPath("InputUnnecessarySemicolonAfterTypeMemberDeclaration.java"), | ||
expected); | ||
} | ||
|
||
@Test | ||
public void testTokens() { | ||
final UnnecessarySemicolonAfterTypeMemberDeclarationCheck check = | ||
new UnnecessarySemicolonAfterTypeMemberDeclarationCheck(); | ||
final int[] expected = { | ||
TokenTypes.CLASS_DEF, | ||
TokenTypes.INTERFACE_DEF, | ||
TokenTypes.ENUM_DEF, | ||
TokenTypes.ANNOTATION_DEF, | ||
TokenTypes.VARIABLE_DEF, | ||
TokenTypes.STATIC_INIT, | ||
TokenTypes.INSTANCE_INIT, | ||
TokenTypes.CTOR_DEF, | ||
TokenTypes.METHOD_DEF, | ||
TokenTypes.ENUM_CONSTANT_DEF, | ||
}; | ||
Assert.assertArrayEquals("Acceptable required tokens are invalid", | ||
expected, check.getAcceptableTokens()); | ||
Assert.assertArrayEquals("Default required tokens are invalid", | ||
expected, check.getDefaultTokens()); | ||
Assert.assertArrayEquals("Required required tokens are invalid", | ||
expected, check.getRequiredTokens()); | ||
} | ||
} |
Oops, something went wrong.