Make RedundantModifier checks if inner types declared in interface has redundant

static modifier, fixes part of #1242
This commit is contained in:
liscju 2015-07-25 10:37:03 +02:00 committed by Roman Ivanov
parent 7cbf72f187
commit f768f75438
3 changed files with 37 additions and 10 deletions

View File

@ -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);
}
/**

View File

@ -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);

View File

@ -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 { }
}