Skip to content

Commit

Permalink
Issue checkstyle#6847: Add UnnecessarySemicolonAfterTypeMemberDeclara…
Browse files Browse the repository at this point in the history
…tionCheck
  • Loading branch information
strkkk committed Jul 19, 2019
1 parent a74affd commit 6f6da07
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ci/jsoref-spellchecker/whitelist.words
Expand Up @@ -1293,6 +1293,7 @@ uniqueproperties
Unitless
unlzma
unnecessaryparentheses
unnecessarysemicolonaftertypememberdeclaration
unnecessarysemicoloninenumeration
unnecessarysemicolonintrywithresources
Unproxyable
Expand Down
1 change: 1 addition & 0 deletions config/checkstyle_checks.xml
Expand Up @@ -361,6 +361,7 @@
<module name="SuperClone"/>
<module name="SuperFinalize"/>
<module name="UnnecessaryParentheses"/>
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
<module name="UnnecessarySemicolonInEnumeration"/>
<module name="UnnecessarySemicolonInTryWithResources"/>
<module name="VariableDeclarationUsageDistance"/>
Expand Down
@@ -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);
}
}
@@ -0,0 +1,5 @@
package org.checkstyle.suppressionxpathfilter.unnecessarysemicolonaftertypememberdeclaration;

public class SuppressionXpathRegressionUnnecessarySemicolonAfterTypeMemberDeclaration {
void method(){}; //warn
}
Expand Up @@ -537,6 +537,9 @@ private static void fillChecksFromCodingPackage() {
BASE_PACKAGE + ".checks.coding.SuperFinalizeCheck");
NAME_TO_FULL_MODULE_NAME.put("UnnecessaryParenthesesCheck",
BASE_PACKAGE + ".checks.coding.UnnecessaryParenthesesCheck");
NAME_TO_FULL_MODULE_NAME.put("UnnecessarySemicolonAfterTypeMemberDeclarationCheck",
BASE_PACKAGE
+ ".checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck");
NAME_TO_FULL_MODULE_NAME.put("UnnecessarySemicolonInEnumerationCheck",
BASE_PACKAGE + ".checks.coding.UnnecessarySemicolonInEnumerationCheck");
NAME_TO_FULL_MODULE_NAME.put("UnnecessarySemicolonInTryWithResourcesCheck",
Expand Down
@@ -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>
* &lt;module name=&quot;UnnecessarySemicolonAfterTypeMemberDeclaration&quot;/&gt;
* </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());
}
}
@@ -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());
}
}

0 comments on commit 6f6da07

Please sign in to comment.