Issue #1530: Detect nested enums marked as static in RedundantModifier

check
This commit is contained in:
Vladislav Lisetskiy 2015-09-03 00:15:26 +03:00 committed by Roman Ivanov
parent 860e442d53
commit f66dcdbd94
4 changed files with 63 additions and 5 deletions

View File

@ -31,7 +31,8 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Checks for redundant modifiers in interface and annotation definitions.
* Checks for non public class constructor and enum constructor redundant modifier.
* Also checks for redundant final modifiers on methods of final classes
* Checks for redundant final modifiers on methods of final classes.
* Checks for redundant static modifiers on nested enums.
*
* <p>Examples:</p>
*
@ -74,6 +75,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*
* @author lkuehne
* @author <a href="mailto:piotr.listkiewicz@gmail.com">liscju</a>
* @author Vladislav Lisetskiy
*/
public class RedundantModifierCheck
extends Check {
@ -128,6 +130,9 @@ public class RedundantModifierCheck
checkClassConstructorModifiers(ast);
}
}
else if (ast.getType() == TokenTypes.ENUM_DEF) {
checkEnumDef(ast);
}
else if (isInterfaceOrAnnotationMember(ast)) {
processInterfaceOrAnnotation(ast);
}
@ -167,6 +172,24 @@ public class RedundantModifierCheck
}
}
/**
* Checks whether enum has proper modifiers.
* @param ast enum definition.
*/
private void checkEnumDef(DetailAST ast) {
if (isInterfaceOrAnnotationMember(ast)) {
processInterfaceOrAnnotation(ast);
}
else if (ast.getParent() != null) {
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
final DetailAST staticModifier = modifiers.findFirstToken(TokenTypes.LITERAL_STATIC);
if (staticModifier != null) {
log(staticModifier.getLineNo(), staticModifier.getColumnNo(),
MSG_KEY, staticModifier.getText());
}
}
}
/**
* Do validation of interface of annotation.
* @param ast token AST

View File

@ -137,4 +137,17 @@ public class RedundantModifierTest
int[] expected = ArrayUtils.EMPTY_INT_ARRAY;
Assert.assertArrayEquals(expected, actual);
}
@Test
public void testNestedStaticEnum() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RedundantModifierCheck.class);
final String[] expected = {
"4:5: " + getCheckMessage(MSG_KEY, "static"),
"8:9: " + getCheckMessage(MSG_KEY, "static"),
"12:9: " + getCheckMessage(MSG_KEY, "static"),
};
verify(checkConfig, getPath("InputRedundantStatic"
+ "ModifierInNestedEnum.java"), expected);
}
}

View File

@ -0,0 +1,14 @@
package com.puppycrawl.tools.checkstyle;
public class InputRedundantStaticModifierInNestedEnum {
static enum NestedEnumWithRedundantStatic {} // violation
enum CorrectNestedEnum {
VAL;
static enum NestedEnumWithRedundantStatic {} // violation
}
interface NestedInterface {
static enum NestedEnumWithRedundantStatic {} // violation
}
}

View File

@ -98,13 +98,17 @@
Checks for redundant modifiers in:
</p>
<ol>
<li>interface and annotation definitions,</li>
<li>the final modifier on methods of final classes, and </li>
<li>Interface and annotation definitions.</li>
<li>Final modifier on methods of final classes.</li>
<li>
inner <code>interface</code> declarations that are declared
Inner <code>interface</code> declarations that are declared
as <code>static</code>.
</li>
<li>Class constructors.</li>
<li>
Nested <code>enum</code> definitions that are declared
as <code>static</code>.
</li>
<li>class constructors</li>
</ol>
<p>
Rationale: The Java Language Specification strongly
@ -134,6 +138,10 @@
modifier on the method of a final class is redundant.
</p>
<p>
Nested <code>enum</code> types are always static by default.
</p>
<p>
Public modifier for constructors in non-public non-protected classes
is always obsolete: