diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java index 0d84a0edf..99e9ccada 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java @@ -26,6 +26,7 @@ import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; 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 @@ -53,13 +54,7 @@ public class RedundantModifierCheck @Override public int[] getDefaultTokens() { - return new int[] { - TokenTypes.METHOD_DEF, - TokenTypes.VARIABLE_DEF, - TokenTypes.ANNOTATION_FIELD_DEF, - TokenTypes.INTERFACE_DEF, - TokenTypes.CTOR_DEF, - }; + return getAcceptableTokens(); } @Override @@ -75,6 +70,8 @@ public class RedundantModifierCheck TokenTypes.ANNOTATION_FIELD_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.CTOR_DEF, + TokenTypes.CLASS_DEF, + TokenTypes.ENUM_DEF, }; } @@ -206,9 +203,11 @@ public class RedundantModifierCheck * @return true or false */ private static boolean isInterfaceOrAnnotationMember(DetailAST ast) { - final DetailAST parentTypeDef = ast.getParent().getParent(); - return parentTypeDef.getType() == TokenTypes.INTERFACE_DEF - || parentTypeDef.getType() == TokenTypes.ANNOTATION_DEF; + final DetailAST parentTypeDef = + ast.getParent() != null ? ast.getParent().getParent() : null; + return parentTypeDef != null + && (parentTypeDef.getType() == TokenTypes.INTERFACE_DEF + || parentTypeDef.getType() == TokenTypes.ANNOTATION_DEF); } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java index e34c01de9..eb82b7fe9 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java @@ -92,6 +92,17 @@ public class RedundantModifierTest verify(checkConfig, getPath("InputRedundantConstructorModifier.java"), expected); } + @Test + public void testInnerTypeInInterfaceIsImplicitlyStatic() throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(RedundantModifierCheck.class); + final String[] expected = { + "8:5: " + getCheckMessage(MSG_KEY, "static"), + "12:5: " + getCheckMessage(MSG_KEY, "static"), + }; + verify(checkConfig, getPath("InputRedundantStaticModifierInInnerTypeOfInterface.java"), expected); + } + @Test public void testGetAcceptableTokens() { RedundantModifierCheck redundantModifierCheckObj = new RedundantModifierCheck(); @@ -102,6 +113,8 @@ public class RedundantModifierTest TokenTypes.ANNOTATION_FIELD_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.CTOR_DEF, + TokenTypes.CLASS_DEF, + TokenTypes.ENUM_DEF, }; Assert.assertNotNull(actual); Assert.assertArrayEquals(expected, actual); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/InputRedundantStaticModifierInInnerTypeOfInterface.java b/src/test/resources/com/puppycrawl/tools/checkstyle/InputRedundantStaticModifierInInnerTypeOfInterface.java new file mode 100644 index 000000000..eab97cabe --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/InputRedundantStaticModifierInInnerTypeOfInterface.java @@ -0,0 +1,15 @@ +//////////////////////////////////////////////////////////////////////////////// +// Test case file for checkstyle. +// Created: 2015 +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle; + +public interface InputRedundantStaticModifierInInnerTypeOfInterface { + static class MyInnerClass { } + + class MyInnerClass2 { } + + static enum MyInnerEnum { } + + enum MyInnerEnum2 { } +}