Class RedundantModifierCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class RedundantModifierCheck extends AbstractCheck
Checks for redundant modifiers.
Rationale: The Java Language Specification strongly discourages the usage of
public
andabstract
for method declarations in interface definitions as a matter of style.The check validates:
- Interface and annotation definitions.
- Final modifier on methods of final and anonymous classes.
-
Inner
interface
declarations that are declared asstatic
. - Class constructors.
-
Nested
enum
definitions that are declared asstatic
.
Interfaces by definition are abstract so the
abstract
modifier on the interface is redundant.Classes inside of interfaces by definition are public and static, so the
public
andstatic
modifiers on the inner classes are redundant. On the other hand, classes inside of interfaces can be abstract or non abstract. So,abstract
modifier is allowed.Fields in interfaces and annotations are automatically public, static and final, so these modifiers are redundant as well.
As annotations are a form of interface, their fields are also automatically public, static and final just as their annotation fields are automatically public and abstract.
Enums by definition are static implicit subclasses of java.lang.Enum<E>. So, the
static
modifier on the enums is redundant. In addition, if enum is inside of interface,public
modifier is also redundant.Enums can also contain abstract methods and methods which can be overridden by the declared enumeration fields. See the following example:
public enum EnumClass { FIELD_1, FIELD_2 { @Override public final void method1() {} // violation expected }; public void method1() {} public final void method2() {} // no violation expected }
Since these methods can be overridden in these situations, the final methods are not marked as redundant even though they can't be extended by other classes/enums.
Nested
enum
types are always static by default.Final classes by definition cannot be extended so the
final
modifier on the method of a final class is redundant.Public modifier for constructors in non-public non-protected classes is always obsolete:
public class PublicClass { public PublicClass() {} // OK } class PackagePrivateClass { public PackagePrivateClass() {} // violation expected }
There is no violation in the following example, because removing public modifier from ProtectedInnerClass constructor will make this code not compiling:
package a; public class ClassExample { protected class ProtectedInnerClass { public ProtectedInnerClass () {} } } package b; import a.ClassExample; public class ClassExtending extends ClassExample { ProtectedInnerClass pc = new ProtectedInnerClass(); }
-
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. Default value is: METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, CTOR_DEF, CLASS_DEF, ENUM_DEF, RESOURCE.
To configure the check:
<module name="RedundantModifier"/>
To configure the check to check only methods and not variables:
<module name="RedundantModifier"> <property name="tokens" value="METHOD_DEF"/> </module>
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
redundantModifier
- Since:
- 3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description RedundantModifierCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
-