public class RedundantModifierCheck extends AbstractCheck
interface
declarations that are declared as static
, non public class
constructors and enum constructors, nested enum definitions that are declared
as static
.
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
and static
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.
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(); }
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private static int[] |
TOKENS_FOR_INTERFACE_MODIFIERS
An array of tokens for interface modifiers.
|
Constructor and Description |
---|
RedundantModifierCheck() |
Modifier and Type | Method and Description |
---|---|
private void |
checkClassConstructorModifiers(DetailAST classCtorAst)
Check if class constructor has proper modifiers.
|
private void |
checkEnumConstructorModifiers(DetailAST ast)
Check if enum constructor has proper modifiers.
|
private void |
checkEnumDef(DetailAST ast)
Checks whether enum has proper modifiers.
|
private void |
checkForRedundantModifier(DetailAST ast,
int modifierType)
Checks if given ast has a redundant modifier.
|
private void |
checkInterfaceModifiers(DetailAST ast)
Checks if interface has proper modifiers.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static java.util.List<DetailAST> |
getMethodAnnotationsList(DetailAST methodDef)
Gets the list of annotations on method definition.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isAnnotatedWithSafeVarargs(DetailAST methodDef)
Checks if method definition is annotated with.
|
private static boolean |
isClassProtected(DetailAST classDef)
Checks if given class ast has protected modifier.
|
private static boolean |
isClassPublic(DetailAST ast)
Checks if given class is accessible from "public" scope.
|
private static boolean |
isEnumMember(DetailAST ast)
Checks if current AST node is member of Enum.
|
private static boolean |
isInterfaceOrAnnotationMember(DetailAST ast)
Checks if current AST node is member of Interface or Annotation, not of their subnodes.
|
private void |
processAbstractMethodParameters(DetailAST ast)
Process validation of parameters for Methods with no definition.
|
private void |
processInterfaceOrAnnotation(DetailAST ast)
Do validation of interface of annotation.
|
private void |
processMethods(DetailAST ast)
Process validation of Methods.
|
private void |
processResources(DetailAST ast)
Checks if given resource has redundant modifiers.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
private static final int[] TOKENS_FOR_INTERFACE_MODIFIERS
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate void checkInterfaceModifiers(DetailAST ast)
ast
- interface to checkprivate void checkEnumConstructorModifiers(DetailAST ast)
ast
- constructor of enumprivate void checkEnumDef(DetailAST ast)
ast
- enum definition.private void processInterfaceOrAnnotation(DetailAST ast)
ast
- token ASTprivate void processMethods(DetailAST ast)
ast
- method ASTprivate void processAbstractMethodParameters(DetailAST ast)
ast
- method ASTprivate void checkClassConstructorModifiers(DetailAST classCtorAst)
classCtorAst
- class constructor astprivate void processResources(DetailAST ast)
ast
- astprivate void checkForRedundantModifier(DetailAST ast, int modifierType)
ast
- astmodifierType
- The modifier to check for.private static boolean isClassProtected(DetailAST classDef)
classDef
- class astprivate static boolean isClassPublic(DetailAST ast)
ast
- class def to checkprivate static boolean isEnumMember(DetailAST ast)
ast
- AST nodeprivate static boolean isInterfaceOrAnnotationMember(DetailAST ast)
ast
- AST nodeprivate static boolean isAnnotatedWithSafeVarargs(DetailAST methodDef)
methodDef
- method definition node