Class InterfaceMemberImpliedModifierCheck
- All Implemented Interfaces:
- Configurable,- Contextualizable
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.
- Since:
- 8.12
- 
Nested Class SummaryNested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanAbstractAutomaticBean.OutputStreamOptions
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate static final StringName for 'abstract' keyword.private static final StringName for 'final' keyword.static final StringA key is pointing to the warning message text in "messages.properties" file.private static final StringName for 'public' access modifier.private static final StringName for 'static' keyword.private booleanControl whether to enforce thatabstractis explicitly coded on interface methods.private booleanControl whether to enforce thatfinalis explicitly coded on interface fields.private booleanControl whether to enforce thatpublicis explicitly coded on interface fields.private booleanControl whether to enforce thatpublicis explicitly coded on interface methods.private booleanControl whether to enforce thatpublicis explicitly coded on interface nested types.private booleanControl whether to enforce thatstaticis explicitly coded on interface fields.private booleanControl whether to enforce thatstaticis explicitly coded on interface nested types.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private voidprocessField(DetailAST ast) Check field in interface.private voidprocessMethod(DetailAST ast) Check method in interface.private voidCheck nested types in interface.voidsetViolateImpliedAbstractMethod(boolean violateImpliedAbstractMethod) Setter to control whether to enforce thatabstractis explicitly coded on interface methods.voidsetViolateImpliedFinalField(boolean violateImpliedFinalField) Setter to control whether to enforce thatfinalis explicitly coded on interface fields.voidsetViolateImpliedPublicField(boolean violateImpliedPublicField) Setter to control whether to enforce thatpublicis explicitly coded on interface fields.voidsetViolateImpliedPublicMethod(boolean violateImpliedPublicMethod) Setter to control whether to enforce thatpublicis explicitly coded on interface methods.voidsetViolateImpliedPublicNested(boolean violateImpliedPublicNested) Setter to control whether to enforce thatpublicis explicitly coded on interface nested types.voidsetViolateImpliedStaticField(boolean violateImpliedStaticField) Setter to control whether to enforce thatstaticis explicitly coded on interface fields.voidsetViolateImpliedStaticNested(boolean violateImpliedStaticNested) Setter to control whether to enforce thatstaticis explicitly coded on interface nested types.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheckbeginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanconfigure, contextualize, getConfiguration, setupChild
- 
Field Details- 
MSG_KEYA key is pointing to the warning message text in "messages.properties" file.- See Also:
 
- 
PUBLIC_ACCESS_MODIFIERName for 'public' access modifier.- See Also:
 
- 
ABSTRACT_KEYWORDName for 'abstract' keyword.- See Also:
 
- 
STATIC_KEYWORDName for 'static' keyword.- See Also:
 
- 
FINAL_KEYWORDName for 'final' keyword.- See Also:
 
- 
violateImpliedPublicFieldControl whether to enforce thatpublicis explicitly coded on interface fields.
- 
violateImpliedStaticFieldControl whether to enforce thatstaticis explicitly coded on interface fields.
- 
violateImpliedFinalFieldControl whether to enforce thatfinalis explicitly coded on interface fields.
- 
violateImpliedPublicMethodControl whether to enforce thatpublicis explicitly coded on interface methods.
- 
violateImpliedAbstractMethodControl whether to enforce thatabstractis explicitly coded on interface methods.
- 
violateImpliedPublicNestedControl whether to enforce thatpublicis explicitly coded on interface nested types.
- 
violateImpliedStaticNestedControl whether to enforce thatstaticis explicitly coded on interface nested types.
 
- 
- 
Constructor Details- 
InterfaceMemberImpliedModifierCheckpublic InterfaceMemberImpliedModifierCheck()
 
- 
- 
Method Details- 
setViolateImpliedPublicFieldSetter to control whether to enforce thatpublicis explicitly coded on interface fields.- Parameters:
- violateImpliedPublicField- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedStaticFieldSetter to control whether to enforce thatstaticis explicitly coded on interface fields.- Parameters:
- violateImpliedStaticField- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedFinalFieldSetter to control whether to enforce thatfinalis explicitly coded on interface fields.- Parameters:
- violateImpliedFinalField- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedPublicMethodSetter to control whether to enforce thatpublicis explicitly coded on interface methods.- Parameters:
- violateImpliedPublicMethod- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedAbstractMethodSetter to control whether to enforce thatabstractis explicitly coded on interface methods.- Parameters:
- violateImpliedAbstractMethod- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedPublicNestedSetter to control whether to enforce thatpublicis explicitly coded on interface nested types.- Parameters:
- violateImpliedPublicNested- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
setViolateImpliedStaticNestedSetter to control whether to enforce thatstaticis explicitly coded on interface nested types.- Parameters:
- violateImpliedStaticNested- True to perform the check, false to turn the check off.
- Since:
- 8.12
 
- 
getDefaultTokensDescription copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
- getDefaultTokensin class- AbstractCheck
- Returns:
- the default tokens
- See Also:
 
- 
getRequiredTokensDescription copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
- getRequiredTokensin class- AbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
 
- 
getAcceptableTokensDescription copied from class:AbstractCheckThe 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:
- getAcceptableTokensin class- AbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
 
- 
visitTokenDescription copied from class:AbstractCheckCalled to process a token.- Overrides:
- visitTokenin class- AbstractCheck
- Parameters:
- ast- the token to process
 
- 
processMethodCheck method in interface.- Parameters:
- ast- the method AST
 
- 
processFieldCheck field in interface.- Parameters:
- ast- the field AST
 
- 
processNestedTypeCheck nested types in interface.- Parameters:
- ast- the nested type AST
 
 
-