Class ModuleDirectiveOrderCheck
- All Implemented Interfaces:
Configurable,Contextualizable
The default configuration enforces
Google Java Style Guide, Section 3.5.1: all requires directives
first, then exports, opens, uses and provides,
each kind in a single block, with a single blank line between blocks. Blank
lines are what delimit blocks, so blank lines between directives of the same
kind are also violations.
All forms of requires (plain, transitive, static)
belong to a single block, and the order of directives inside a block is not
validated.
Directive kinds that are not listed in the order property are not
validated.
- Since:
- 14.1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionDefault order of directive kinds.static final StringA key pointing to the warning message text in "messages.properties" file.static final StringA key pointing to the warning message text in "messages.properties" file.static final StringA key pointing to the warning message text in "messages.properties" file.static final StringA key pointing to the warning message text in "messages.properties" file.Specify directive kinds in the order their blocks must appear inside the module declaration.Valid values for entries of theorderproperty.private booleanControl whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newModuleDirectiveOrderCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate intcountBlankLinesBetweenDirectives(DetailAST previous, DetailAST directive) Counts the blank lines between the end of the previous directive and the start of the given directive.int[]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 booleanprecedesInOrder(String kind, String previousKind) Checks whether the given kind precedes the other kind in theorderproperty.voidSetter to specify directive kinds in the order their blocks must appear inside the module declaration.voidsetValidateBlockSeparation(boolean validateBlockSeparation) Setter to control whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block.private voidvalidateDirectivePlacement(DetailAST directive, DetailAST previous, Set<String> seenKinds) Validates the placement of a directive relative to the previous directive of the module.voidvisitToken(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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_ORDER
A key pointing to the warning message text in "messages.properties" file. Emitted when a directive block appears after a block that it should precede.- See Also:
-
MSG_GROUPING
A key pointing to the warning message text in "messages.properties" file. Emitted when directives of one kind are interleaved with directives of another kind.- See Also:
-
MSG_SEPARATED_INTERNALLY
A key pointing to the warning message text in "messages.properties" file. Emitted when directives of the same kind are separated by blank lines.- See Also:
-
MSG_SEPARATION
A key pointing to the warning message text in "messages.properties" file. Emitted when a directive block is not separated from the previous block by exactly one blank line.- See Also:
-
DEFAULT_ORDER
Default order of directive kinds. -
VALID_KINDS
Valid values for entries of theorderproperty. -
order
Specify directive kinds in the order their blocks must appear inside the module declaration. -
validateBlockSeparation
Control whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block.
-
-
Constructor Details
-
ModuleDirectiveOrderCheck
public ModuleDirectiveOrderCheck()Creates a newModuleDirectiveOrderCheckinstance.
-
-
Method Details
-
setOrder
Setter to specify directive kinds in the order their blocks must appear inside the module declaration.- Parameters:
order- the order of directive kinds.- Throws:
IllegalArgumentException- when an element of order is not a directive kind.- Since:
- 14.1.0
-
setValidateBlockSeparation
Setter to control whether blank line separation is validated: exactly one blank line between directive blocks and no blank lines inside a block.- Parameters:
validateBlockSeparation- the value to set.- Since:
- 14.1.0
-
getDefaultTokens
Description 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 classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description 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 classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
validateDirectivePlacement
private void validateDirectivePlacement(DetailAST directive, DetailAST previous, Set<String> seenKinds) Validates the placement of a directive relative to the previous directive of the module.A directive of the same kind as the previous one continues the current block and must not be separated from it by blank lines. Otherwise the directive starts a new block, which must not repeat an earlier kind, must not belong before the previous block, and must be separated from it by exactly one blank line. Blank line requirements are validated only when
validateBlockSeparationis enabled.- Parameters:
directive- the directive to validate.previous- the directive before the given one.seenKinds- kinds of all directives seen before the given one.
-
precedesInOrder
Checks whether the given kind precedes the other kind in theorderproperty.- Parameters:
kind- the kind of the directive being validated.previousKind- the kind of the previous directive.- Returns:
- true if
kindprecedespreviousKindin the configured order.
-
countBlankLinesBetweenDirectives
Counts the blank lines between the end of the previous directive and the start of the given directive.- Parameters:
previous- the directive before the given one.directive- the directive to count blank lines before.- Returns:
- the number of blank lines between the two directives.
-