Make RedundantModifier checks if enum constructor has redundant private

modifier, fixes part of #1242
This commit is contained in:
liscju 2015-07-21 23:40:34 +02:00 committed by Roman Ivanov
parent eb6de977b5
commit 2f7481ee4e
5 changed files with 83 additions and 14 deletions

View File

@ -432,7 +432,7 @@ public enum JavadocTagInfo {
* @param name the tag name
* @param type the type of tag
*/
private JavadocTagInfo(final String text, final String name,
JavadocTagInfo(final String text, final String name,
final Type type) {
this.text = text;
this.name = name;

View File

@ -51,7 +51,7 @@ public enum LineSeparatorOption {
* Creates a new <code>LineSeparatorOption</code> instance.
* @param sep the line separator, e.g. "\r\n"
*/
private LineSeparatorOption(String sep) {
LineSeparatorOption(String sep) {
lineSeparator = sep.getBytes(StandardCharsets.US_ASCII);
}

View File

@ -28,9 +28,11 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Checks for redundant modifiers in interface and annotation definitions.
* Also checks for redundant final modifiers on methods of final classes.
* Also checks for redundant final modifiers on methods of final classes
* and redundant enum constructor modifier.
*
* @author lkuehne
* @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
*/
public class RedundantModifierCheck
extends Check {
@ -56,6 +58,7 @@ public class RedundantModifierCheck
TokenTypes.VARIABLE_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.CTOR_DEF,
};
}
@ -71,23 +74,18 @@ public class RedundantModifierCheck
TokenTypes.VARIABLE_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.CTOR_DEF,
};
}
@Override
public void visitToken(DetailAST ast) {
if (TokenTypes.INTERFACE_DEF == ast.getType()) {
final DetailAST modifiers =
ast.findFirstToken(TokenTypes.MODIFIERS);
for (final int tokenType : TOKENS_FOR_INTERFACE_MODIFIERS) {
final DetailAST modifier =
modifiers.findFirstToken(tokenType);
if (modifier != null) {
log(modifier.getLineNo(), modifier.getColumnNo(),
MSG_KEY, modifier.getText());
}
}
checkInterfaceModifiers(ast);
}
else if (TokenTypes.CTOR_DEF == ast.getType()
&& isEnumMember(ast)) {
checkEnumConstructorModifiers(ast);
}
else if (isInterfaceOrAnnotationMember(ast)) {
processInterfaceOrAnnotation(ast);
@ -97,6 +95,37 @@ public class RedundantModifierCheck
}
}
/**
* Checks if interface has proper modifiers
* @param ast interface to check
*/
private void checkInterfaceModifiers(DetailAST ast) {
final DetailAST modifiers =
ast.findFirstToken(TokenTypes.MODIFIERS);
for (final int tokenType : TOKENS_FOR_INTERFACE_MODIFIERS) {
final DetailAST modifier =
modifiers.findFirstToken(tokenType);
if (modifier != null) {
log(modifier.getLineNo(), modifier.getColumnNo(),
MSG_KEY, modifier.getText());
}
}
}
/**
* Check if enum constructor has proper modifiers
* @param ast constructor of enum
*/
private void checkEnumConstructorModifiers(DetailAST ast) {
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
final DetailAST modifier = modifiers.getFirstChild();
if (modifier != null) {
log(modifier.getLineNo(), modifier.getColumnNo(),
MSG_KEY, modifier.getText());
}
}
/**
* do validation of interface of annotation
* @param ast token AST
@ -161,6 +190,16 @@ public class RedundantModifierCheck
}
}
/**
* Checks if current AST node is member of Enum
* @param ast AST node
* @return true if it is an enum member
*/
private boolean isEnumMember(DetailAST ast) {
final DetailAST parentTypeDef = ast.getParent().getParent();
return parentTypeDef.getType() == TokenTypes.ENUM_DEF;
}
/**
* Checks if current AST node is member of Interface or Annotation, not of their subnodes.
* @param ast AST node

View File

@ -82,6 +82,16 @@ public class RedundantModifierTest
expected);
}
@Test
public void testEnumConstructorIsImplicitlyPrivate() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RedundantModifierCheck.class);
final String[] expected = {
"10:5: " + getCheckMessage(MSG_KEY, "private"),
};
verify(checkConfig, getPath("InputRedundantConstructorModifier.java"), expected);
}
@Test
public void testGetAcceptableTokens() {
RedundantModifierCheck redundantModifierCheckObj = new RedundantModifierCheck();
@ -91,6 +101,7 @@ public class RedundantModifierTest
TokenTypes.VARIABLE_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.CTOR_DEF,
};
Assert.assertNotNull(actual);
Assert.assertArrayEquals(expected, actual);

View File

@ -0,0 +1,19 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2015
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
public enum InputRedundantConstructorModifier {
VAL1, VAL2;
private InputRedundantConstructorModifier() { }
InputRedundantConstructorModifier(int i) { }
InputRedundantConstructorModifier(char c) { }
}
class ProperPrivateConstructor {
private ProperPrivateConstructor() { }
}