Class InterfaceMemberImpliedModifierCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.modifier.InterfaceMemberImpliedModifierCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class InterfaceMemberImpliedModifierCheck extends AbstractCheck
Checks for implicit modifiers on interface members and nested types.This check is effectively the opposite of RedundantModifier. It checks the modifiers on interface members, ensuring that certain modifiers are explicitly specified even though they are actually redundant.
Methods in interfaces are
public
by default, however from Java 9 they can also beprivate
. This check provides the ability to enforce thatpublic
is explicitly coded and not implicitly added by the compiler.From Java 8, there are three types of methods in interfaces - static methods marked with
static
, default methods marked withdefault
and abstract methods which do not have to be marked with anything. From Java 9, there are also private methods marked withprivate
. This check provides the ability to enforce thatabstract
is explicitly coded and not implicitly added by the compiler.Fields in interfaces are always
public static final
and as such the compiler does not require these modifiers. This check provides the ability to enforce that these modifiers are explicitly coded and not implicitly added by the compiler.Nested types within an interface are always
public static
and as such the compiler does not require thepublic static
modifiers. This check provides the ability to enforce that thepublic
andstatic
modifiers are explicitly coded and not implicitly added by the compiler.public interface AddressFactory { // check enforces code contains "public static final" public static final String UNKNOWN = "Unknown"; String OTHER = "Other"; // violation // check enforces code contains "public" or "private" public static AddressFactory instance(); // check enforces code contains "public abstract" public abstract Address createAddress(String addressLine, String city); List<Address> findAddresses(String city); // violation // check enforces default methods are explicitly declared "public" public default Address createAddress(String city) { return createAddress(UNKNOWN, city); } default Address createOtherAddress() { // violation return createAddress(OTHER, OTHER); } }
Rationale for this check: Methods, fields and nested types are treated differently depending on whether they are part of an interface or part of a class. For example, by default methods are package-scoped on classes, but public in interfaces. However, from Java 8 onwards, interfaces have changed to be much more like abstract classes. Interfaces now have static and instance methods with code. Developers should not have to remember which modifiers are required and which are implied. This check allows the simpler alternative approach to be adopted where the implied modifiers must always be coded explicitly.
-
Property
violateImpliedAbstractMethod
- Control whether to enforce thatabstract
is explicitly coded on interface methods. Type isboolean
. Default value istrue
. -
Property
violateImpliedFinalField
- Control whether to enforce thatfinal
is explicitly coded on interface fields. Type isboolean
. Default value istrue
. -
Property
violateImpliedPublicField
- Control whether to enforce thatpublic
is explicitly coded on interface fields. Type isboolean
. Default value istrue
. -
Property
violateImpliedPublicMethod
- Control whether to enforce thatpublic
is explicitly coded on interface methods. Type isboolean
. Default value istrue
. -
Property
violateImpliedPublicNested
- Control whether to enforce thatpublic
is explicitly coded on interface nested types. Type isboolean
. Default value istrue
. -
Property
violateImpliedStaticField
- Control whether to enforce thatstatic
is explicitly coded on interface fields. Type isboolean
. Default value istrue
. -
Property
violateImpliedStaticNested
- Control whether to enforce thatstatic
is explicitly coded on interface nested types. Type isboolean
. Default value istrue
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
interface.implied.modifier
- Since:
- 8.12
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private static String
ABSTRACT_KEYWORD
Name for 'abstract' keyword.private static String
FINAL_KEYWORD
Name for 'final' keyword.static String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.private static String
PUBLIC_ACCESS_MODIFIER
Name for 'public' access modifier.private static String
STATIC_KEYWORD
Name for 'static' keyword.private boolean
violateImpliedAbstractMethod
Control whether to enforce thatabstract
is explicitly coded on interface methods.private boolean
violateImpliedFinalField
Control whether to enforce thatfinal
is explicitly coded on interface fields.private boolean
violateImpliedPublicField
Control whether to enforce thatpublic
is explicitly coded on interface fields.private boolean
violateImpliedPublicMethod
Control whether to enforce thatpublic
is explicitly coded on interface methods.private boolean
violateImpliedPublicNested
Control whether to enforce thatpublic
is explicitly coded on interface nested types.private boolean
violateImpliedStaticField
Control whether to enforce thatstatic
is explicitly coded on interface fields.private boolean
violateImpliedStaticNested
Control whether to enforce thatstatic
is explicitly coded on interface nested types.
-
Constructor Summary
Constructors Constructor Description InterfaceMemberImpliedModifierCheck()
-
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.private void
processField(DetailAST ast)
Check field in interface.private void
processMethod(DetailAST ast)
Check method in interface.private void
processNestedType(DetailAST ast)
Check nested types in interface.void
setViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod)
Setter to control whether to enforce thatabstract
is explicitly coded on interface methods.void
setViolateImpliedFinalField(boolean violateImpliedFinalField)
Setter to control whether to enforce thatfinal
is explicitly coded on interface fields.void
setViolateImpliedPublicField(boolean violateImpliedPublicField)
Setter to control whether to enforce thatpublic
is explicitly coded on interface fields.void
setViolateImpliedPublicMethod(boolean violateImpliedPublicMethod)
Setter to control whether to enforce thatpublic
is explicitly coded on interface methods.void
setViolateImpliedPublicNested(boolean violateImpliedPublicNested)
Setter to control whether to enforce thatpublic
is explicitly coded on interface nested types.void
setViolateImpliedStaticField(boolean violateImpliedStaticField)
Setter to control whether to enforce thatstatic
is explicitly coded on interface fields.void
setViolateImpliedStaticNested(boolean violateImpliedStaticNested)
Setter to control whether to enforce thatstatic
is explicitly coded on interface nested types.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, 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.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
PUBLIC_ACCESS_MODIFIER
private static final String PUBLIC_ACCESS_MODIFIER
Name for 'public' access modifier.- See Also:
- Constant Field Values
-
ABSTRACT_KEYWORD
private static final String ABSTRACT_KEYWORD
Name for 'abstract' keyword.- See Also:
- Constant Field Values
-
STATIC_KEYWORD
private static final String STATIC_KEYWORD
Name for 'static' keyword.- See Also:
- Constant Field Values
-
FINAL_KEYWORD
private static final String FINAL_KEYWORD
Name for 'final' keyword.- See Also:
- Constant Field Values
-
violateImpliedPublicField
private boolean violateImpliedPublicField
Control whether to enforce thatpublic
is explicitly coded on interface fields.
-
violateImpliedStaticField
private boolean violateImpliedStaticField
Control whether to enforce thatstatic
is explicitly coded on interface fields.
-
violateImpliedFinalField
private boolean violateImpliedFinalField
Control whether to enforce thatfinal
is explicitly coded on interface fields.
-
violateImpliedPublicMethod
private boolean violateImpliedPublicMethod
Control whether to enforce thatpublic
is explicitly coded on interface methods.
-
violateImpliedAbstractMethod
private boolean violateImpliedAbstractMethod
Control whether to enforce thatabstract
is explicitly coded on interface methods.
-
violateImpliedPublicNested
private boolean violateImpliedPublicNested
Control whether to enforce thatpublic
is explicitly coded on interface nested types.
-
violateImpliedStaticNested
private boolean violateImpliedStaticNested
Control whether to enforce thatstatic
is explicitly coded on interface nested types.
-
-
Constructor Detail
-
InterfaceMemberImpliedModifierCheck
public InterfaceMemberImpliedModifierCheck()
-
-
Method Detail
-
setViolateImpliedPublicField
public void setViolateImpliedPublicField(boolean violateImpliedPublicField)
Setter to control whether to enforce thatpublic
is explicitly coded on interface fields.- Parameters:
violateImpliedPublicField
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedStaticField
public void setViolateImpliedStaticField(boolean violateImpliedStaticField)
Setter to control whether to enforce thatstatic
is explicitly coded on interface fields.- Parameters:
violateImpliedStaticField
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedFinalField
public void setViolateImpliedFinalField(boolean violateImpliedFinalField)
Setter to control whether to enforce thatfinal
is explicitly coded on interface fields.- Parameters:
violateImpliedFinalField
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedPublicMethod
public void setViolateImpliedPublicMethod(boolean violateImpliedPublicMethod)
Setter to control whether to enforce thatpublic
is explicitly coded on interface methods.- Parameters:
violateImpliedPublicMethod
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedAbstractMethod
public void setViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod)
Setter to control whether to enforce thatabstract
is explicitly coded on interface methods.- Parameters:
violateImpliedAbstractMethod
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedPublicNested
public void setViolateImpliedPublicNested(boolean violateImpliedPublicNested)
Setter to control whether to enforce thatpublic
is explicitly coded on interface nested types.- Parameters:
violateImpliedPublicNested
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
setViolateImpliedStaticNested
public void setViolateImpliedStaticNested(boolean violateImpliedStaticNested)
Setter to control whether to enforce thatstatic
is explicitly coded on interface nested types.- Parameters:
violateImpliedStaticNested
- True to perform the check, false to turn the check off.- Since:
- 8.12
-
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
-
processMethod
private void processMethod(DetailAST ast)
Check method in interface.- Parameters:
ast
- the method AST
-
processField
private void processField(DetailAST ast)
Check field in interface.- Parameters:
ast
- the field AST
-
processNestedType
private void processNestedType(DetailAST ast)
Check nested types in interface.- Parameters:
ast
- the nested type AST
-
-