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 be
private
. This check provides the ability to enforce that public
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 with default
and abstract methods which do not
have to be marked with anything. From Java 9, there are also private methods marked with
private
. This check provides the ability to enforce that abstract
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 the public static
modifiers. This check provides the ability to
enforce that the public
and static
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.
violateImpliedPublicField
- Control whether to enforce that public
is explicitly coded on interface fields.
Type is boolean
.
Default value is true
.
violateImpliedStaticField
- Control whether to enforce that static
is explicitly coded on interface fields.
Type is boolean
.
Default value is true
.
violateImpliedFinalField
- Control whether to enforce that final
is explicitly coded on interface fields.
Type is boolean
.
Default value is true
.
violateImpliedPublicMethod
- Control whether to enforce that public
is explicitly coded on interface methods.
Type is boolean
.
Default value is true
.
violateImpliedAbstractMethod
- Control whether to enforce that abstract
is explicitly coded on interface methods.
Type is boolean
.
Default value is true
.
violateImpliedPublicNested
- Control whether to enforce that public
is explicitly coded on interface nested types.
Type is boolean
.
Default value is true
.
violateImpliedStaticNested
- Control whether to enforce that static
is explicitly coded on interface nested types.
Type is boolean
.
Default value is true
.
This example checks that all implicit modifiers on methods, fields and nested types are explicitly specified in interfaces.
Configuration:
<module name="InterfaceMemberImpliedModifier"/>
Code:
public interface AddressFactory { public static final String UNKNOWN = "Unknown"; // valid String OTHER = "Other"; // violation public static AddressFactory instance(); // valid public abstract Address createAddress(String addressLine, String city); // valid List<Address> findAddresses(String city); // violation interface Address { // violation String getCity(); // violation } }
This example checks that all implicit modifiers on methods and fields are explicitly specified, but nested types do not need to be.
Configuration:
<module name="InterfaceMemberImpliedModifier"> <property name="violateImpliedPublicNested" value="false"/> <property name="violateImpliedStaticNested" value="false"/> </module>
Code:
public interface RoadFeature { String STOP = "Stop"; // violation enum Lights { // valid because of configured properties RED, YELLOW, GREEN; } }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
interface.implied.modifier
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
|
Constructor and Description |
---|
InterfaceMemberImpliedModifierCheck() |
Modifier and Type | Method and 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 |
setViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod)
Setter to control whether to enforce that
abstract is explicitly coded
on interface methods. |
void |
setViolateImpliedFinalField(boolean violateImpliedFinalField)
Setter to control whether to enforce that
final is explicitly coded
on interface fields. |
void |
setViolateImpliedPublicField(boolean violateImpliedPublicField)
Setter to control whether to enforce that
public is explicitly coded
on interface fields. |
void |
setViolateImpliedPublicMethod(boolean violateImpliedPublicMethod)
Setter to control whether to enforce that
public is explicitly coded
on interface methods. |
void |
setViolateImpliedPublicNested(boolean violateImpliedPublicNested)
Setter to control whether to enforce that
public is explicitly coded
on interface nested types. |
void |
setViolateImpliedStaticField(boolean violateImpliedStaticField)
Setter to control whether to enforce that
static is explicitly coded
on interface fields. |
void |
setViolateImpliedStaticNested(boolean violateImpliedStaticNested)
Setter to control whether to enforce that
static is explicitly coded
on interface nested types. |
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public InterfaceMemberImpliedModifierCheck()
public void setViolateImpliedPublicField(boolean violateImpliedPublicField)
public
is explicitly coded
on interface fields.violateImpliedPublicField
- True to perform the check, false to turn the check off.public void setViolateImpliedStaticField(boolean violateImpliedStaticField)
static
is explicitly coded
on interface fields.violateImpliedStaticField
- True to perform the check, false to turn the check off.public void setViolateImpliedFinalField(boolean violateImpliedFinalField)
final
is explicitly coded
on interface fields.violateImpliedFinalField
- True to perform the check, false to turn the check off.public void setViolateImpliedPublicMethod(boolean violateImpliedPublicMethod)
public
is explicitly coded
on interface methods.violateImpliedPublicMethod
- True to perform the check, false to turn the check off.public void setViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod)
abstract
is explicitly coded
on interface methods.violateImpliedAbstractMethod
- True to perform the check, false to turn the check off.public void setViolateImpliedPublicNested(boolean violateImpliedPublicNested)
public
is explicitly coded
on interface nested types.violateImpliedPublicNested
- True to perform the check, false to turn the check off.public void setViolateImpliedStaticNested(boolean violateImpliedStaticNested)
static
is explicitly coded
on interface nested types.violateImpliedStaticNested
- True to perform the check, false to turn the check off.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 processCopyright © 2001–2020. All rights reserved.