Class DescendantTokenCheck
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.DescendantTokenCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
Checks for restricted tokens beneath other tokens.
WARNING: This is a very powerful and flexible check, but, at the same time, it is low-level and very implementation-dependent because its results depend on the grammar we use to build abstract syntax trees. Thus, we recommend using other checks when they provide the desired functionality. Essentially, this check just works on the level of an abstract syntax tree and knows nothing about language structures.
-
Property
limitedTokens
- Specify set of tokens with limited occurrences as descendants. Type isjava.lang.String[]
. Validation type istokenTypesSet
. Default value is""
. -
Property
maximumDepth
- Specify the maximum depth for descendant counts. Type isint
. Default value is2147483647
. -
Property
maximumMessage
- Define the violation message when the maximum count is exceeded. Type isjava.lang.String
. Default value isnull
. -
Property
maximumNumber
- Specify a maximum count for descendants. Type isint
. Default value is2147483647
. -
Property
minimumDepth
- Specify the minimum depth for descendant counts. Type isint
. Default value is0
. -
Property
minimumMessage
- Define the violation message when the minimum count is not reached. Type isjava.lang.String
. Default value isnull
. -
Property
minimumNumber
- Specify a minimum count for descendants. Type isint
. Default value is0
. -
Property
sumTokenCounts
- Control whether the number of tokens found should be calculated from the sum of the individual token counts. Type isboolean
. Default value isfalse
. -
Property
tokens
- tokens to check Type isanyTokenTypesSet
. Default value is""
.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
descendant.token.max
-
descendant.token.min
-
descendant.token.sum.max
-
descendant.token.sum.min
- Since:
- 3.2
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]
Counts of descendant tokens.private int[]
Specify set of tokens with limited occurrences as descendants.private int
Specify the maximum depth for descendant counts.private String
Define the violation message when the maximum count is exceeded.private int
Specify a maximum count for descendants.private int
Specify the minimum depth for descendant counts.private String
Define the violation message when the minimum count is not reached.private int
Specify a minimum count for descendants.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.static final String
A key is pointing to the warning message text in "messages.properties" file.private boolean
Control whether the number of tokens found should be calculated from the sum of the individual token counts. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
countTokens
(DetailAST ast, int depth) Counts the number of occurrences of descendant tokens.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 void
logAsSeparated
(DetailAST ast) Log violations for each Token.private void
logAsTotal
(DetailAST ast) Log validation as one violation.void
setLimitedTokens
(String... limitedTokensParam) Setter to specify set of tokens with limited occurrences as descendants.void
setMaximumDepth
(int maximumDepth) Setter to specify the maximum depth for descendant counts.void
setMaximumMessage
(String message) Setter to define the violation message when the maximum count is exceeded.void
setMaximumNumber
(int maximumNumber) Setter to specify a maximum count for descendants.void
setMinimumDepth
(int minimumDepth) Setter to specify the minimum depth for descendant counts.void
setMinimumMessage
(String message) Setter to define the violation message when the minimum count is not reached.void
setMinimumNumber
(int minimumNumber) Setter to specify a minimum count for descendants.void
setSumTokenCounts
(boolean sum) Setter to control whether the number of tokens found should be calculated from the sum of the individual token counts.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 Details
-
MSG_KEY_MIN
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_MAX
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_SUM_MIN
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_SUM_MAX
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
minimumDepth
Specify the minimum depth for descendant counts. -
maximumDepth
Specify the maximum depth for descendant counts. -
minimumNumber
Specify a minimum count for descendants. -
maximumNumber
Specify a maximum count for descendants. -
sumTokenCounts
Control whether the number of tokens found should be calculated from the sum of the individual token counts. -
limitedTokens
Specify set of tokens with limited occurrences as descendants. -
minimumMessage
Define the violation message when the minimum count is not reached. -
maximumMessage
Define the violation message when the maximum count is exceeded. -
counts
Counts of descendant tokens. Indexed by (token ID - 1) for performance.
-
-
Constructor Details
-
DescendantTokenCheck
public DescendantTokenCheck()
-
-
Method Details
-
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:
-
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:
-
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:
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
logAsSeparated
Log violations for each Token.- Parameters:
ast
- token
-
logAsTotal
Log validation as one violation.- Parameters:
ast
- current token
-
countTokens
Counts the number of occurrences of descendant tokens.- Parameters:
ast
- the root token for descendants.depth
- the maximum depth of the counted descendants.
-
setLimitedTokens
Setter to specify set of tokens with limited occurrences as descendants.- Parameters:
limitedTokensParam
- tokens to ignore.- Since:
- 3.2
-
setMinimumDepth
Setter to specify the minimum depth for descendant counts.- Parameters:
minimumDepth
- the minimum depth for descendant counts.- Since:
- 3.2
-
setMaximumDepth
Setter to specify the maximum depth for descendant counts.- Parameters:
maximumDepth
- the maximum depth for descendant counts.- Since:
- 3.2
-
setMinimumNumber
Setter to specify a minimum count for descendants.- Parameters:
minimumNumber
- the minimum count for descendants.- Since:
- 3.2
-
setMaximumNumber
Setter to specify a maximum count for descendants.- Parameters:
maximumNumber
- the maximum count for descendants.- Since:
- 3.2
-
setMinimumMessage
Setter to define the violation message when the minimum count is not reached.- Parameters:
message
- the violation message for minimum count not reached. Used as aMessageFormat
pattern with arguments- {0} - token count
- {1} - minimum number
- {2} - name of token
- {3} - name of limited token
- Since:
- 3.2
-
setMaximumMessage
Setter to define the violation message when the maximum count is exceeded.- Parameters:
message
- the violation message for maximum count exceeded. Used as aMessageFormat
pattern with arguments- {0} - token count
- {1} - maximum number
- {2} - name of token
- {3} - name of limited token
- Since:
- 3.2
-
setSumTokenCounts
Setter to control whether the number of tokens found should be calculated from the sum of the individual token counts.- Parameters:
sum
- whether to use the sum.- Since:
- 5.0
-