public class DescendantTokenCheck extends AbstractCheck
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.
limitedTokens - Specify set of tokens with limited occurrences as descendants.
Type is java.lang.String[].
Validation type is tokenTypesSet.
Default value is "".
minimumDepth - Specify the minimum depth for descendant counts.
Type is int.
Default value is 0.
maximumDepth - Specify the maximum depth for descendant counts.
Type is int.
Default value is 2147483647.
minimumNumber - Specify a minimum count for descendants.
Type is int.
Default value is 0.
maximumNumber - Specify a maximum count for descendants.
Type is int.
Default value is 2147483647.
sumTokenCounts - Control whether the number of tokens found
should be calculated from the sum of the individual token counts.
Type is boolean.
Default value is false.
minimumMessage - Define the violation message
when the minimum count is not reached.
Type is java.lang.String.
Default value is null.
maximumMessage - Define the violation message
when the maximum count is exceeded.
Type is java.lang.String.
Default value is null.
tokens - tokens to check
Type is anyTokenTypesSet.
Default value is "".
To configure the check to produce a violation on a switch statement with no default case:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_SWITCH"/> <property name="maximumDepth" value="2"/> <property name="limitedTokens" value="LITERAL_DEFAULT"/> <property name="minimumNumber" value="1"/> </module>
To configure the check to produce a violation on a condition in for
which performs no check:
<module name="DescendantToken"> <property name="tokens" value="FOR_CONDITION"/> <property name="limitedTokens" value="EXPR"/> <property name="minimumNumber" value="1"/> </module>
To configure the check to produce a violation on comparing this with
null(i.e. this == null and this != null):
<module name="DescendantToken"> <property name="tokens" value="EQUAL,NOT_EQUAL"/> <property name="limitedTokens" value="LITERAL_THIS,LITERAL_NULL"/> <property name="maximumNumber" value="1"/> <property name="maximumDepth" value="1"/> <property name="sumTokenCounts" value="true"/> </module>
To configure the check to produce a violation on a String literal equality check:
<module name="DescendantToken"> <property name="tokens" value="EQUAL,NOT_EQUAL"/> <property name="limitedTokens" value="STRING_LITERAL"/> <property name="maximumNumber" value="0"/> <property name="maximumDepth" value="1"/> </module>
To configure the check to produce a violation on an assert statement that may have side effects (formatted for browser display):
<module name="DescendantToken">
<property name="tokens" value="LITERAL_ASSERT"/>
<property name="limitedTokens" value="ASSIGN,DEC,INC,POST_DEC,
POST_INC,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,DIV_ASSIGN,MOD_ASSIGN,
BSR_ASSIGN,SR_ASSIGN,SL_ASSIGN,BAND_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,
METHOD_CALL"/>
<property name="maximumNumber" value="0"/>
</module>
To configure the check to produce a violation on an initializer in for
performs no setup (where a while statement could be used instead):
<module name="DescendantToken"> <property name="tokens" value="FOR_INIT"/> <property name="limitedTokens" value="EXPR"/> <property name="minimumNumber" value="1"/> </module>
To configure the check to produce a violation on a switch that is nested in another switch:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_SWITCH"/> <property name="limitedTokens" value="LITERAL_SWITCH"/> <property name="maximumNumber" value="0"/> <property name="minimumDepth" value="1"/> </module>
To configure the check to produce a violation on a return statement from within a catch or finally block:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_FINALLY,LITERAL_CATCH"/> <property name="limitedTokens" value="LITERAL_RETURN"/> <property name="maximumNumber" value="0"/> </module>
To configure the check to produce a violation on a try statement within a catch or finally block:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_CATCH,LITERAL_FINALLY"/> <property name="limitedTokens" value="LITERAL_TRY"/> <property name="maximumNumber" value="0"/> </module>
To configure the check to produce a violation on a switch with too many cases:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_SWITCH"/> <property name="limitedTokens" value="LITERAL_CASE"/> <property name="maximumDepth" value="2"/> <property name="maximumNumber" value="10"/> </module>
To configure the check to produce a violation on a method with too many local variables:
<module name="DescendantToken"> <property name="tokens" value="METHOD_DEF"/> <property name="limitedTokens" value="VARIABLE_DEF"/> <property name="maximumDepth" value="2"/> <property name="maximumNumber" value="10"/> </module>
To configure the check to produce a violation on a method with too many returns:
<module name="DescendantToken"> <property name="tokens" value="METHOD_DEF"/> <property name="limitedTokens" value="LITERAL_RETURN"/> <property name="maximumNumber" value="3"/> </module>
To configure the check to produce a violation on an interface with too many fields:
<module name="DescendantToken"> <property name="tokens" value="INTERFACE_DEF"/> <property name="limitedTokens" value="VARIABLE_DEF"/> <property name="maximumDepth" value="2"/> <property name="maximumNumber" value="0"/> </module>
To configure the check to produce a violation on a method which throws too many exceptions:
<module name="DescendantToken"> <property name="tokens" value="LITERAL_THROWS"/> <property name="limitedTokens" value="IDENT"/> <property name="maximumNumber" value="1"/> </module>
To configure the check to produce a violation on a method with too many expressions:
<module name="DescendantToken"> <property name="tokens" value="METHOD_DEF"/> <property name="limitedTokens" value="EXPR"/> <property name="maximumNumber" value="200"/> </module>
To configure the check to produce a violation on an empty statement:
<module name="DescendantToken">
<property name="tokens" value="EMPTY_STAT"/>
<property name="limitedTokens" value="EMPTY_STAT"/>
<property name="maximumNumber" value="0"/>
<property name="maximumDepth" value="0"/>
<property name="maximumMessage"
value="Empty statement is not allowed."/>
</module>
To configure the check to produce a violation on a class with too many fields:
<module name="DescendantToken"> <property name="tokens" value="CLASS_DEF"/> <property name="limitedTokens" value="VARIABLE_DEF"/> <property name="maximumDepth" value="2"/> <property name="maximumNumber" value="10"/> </module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
descendant.token.max
descendant.token.min
descendant.token.sum.max
descendant.token.sum.min
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private int[] |
counts
Counts of descendant tokens.
|
private int[] |
limitedTokens
Specify set of tokens with limited occurrences as descendants.
|
private int |
maximumDepth
Specify the maximum depth for descendant counts.
|
private java.lang.String |
maximumMessage
Define the violation message when the maximum count is exceeded.
|
private int |
maximumNumber
Specify a maximum count for descendants.
|
private int |
minimumDepth
Specify the minimum depth for descendant counts.
|
private java.lang.String |
minimumMessage
Define the violation message when the minimum count is not reached.
|
private int |
minimumNumber
Specify a minimum count for descendants.
|
static java.lang.String |
MSG_KEY_MAX
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_MIN
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_SUM_MAX
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_KEY_SUM_MIN
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
sumTokenCounts
Control whether the number of tokens found should be calculated from
the sum of the individual token counts.
|
| Constructor and Description |
|---|
DescendantTokenCheck() |
| Modifier and Type | Method and Description |
|---|---|
private void |
countTokens(DetailAST ast,
int depth)
Counts the number of occurrences of descendant tokens.
|
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 |
logAsSeparated(DetailAST ast)
Log violations for each Token.
|
private void |
logAsTotal(DetailAST ast)
Log validation as one violation.
|
void |
setLimitedTokens(java.lang.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(java.lang.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(java.lang.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.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY_MIN
public static final java.lang.String MSG_KEY_MAX
public static final java.lang.String MSG_KEY_SUM_MIN
public static final java.lang.String MSG_KEY_SUM_MAX
private int minimumDepth
private int maximumDepth
private int minimumNumber
private int maximumNumber
private boolean sumTokenCounts
private int[] limitedTokens
private java.lang.String minimumMessage
private java.lang.String maximumMessage
private int[] counts
public DescendantTokenCheck()
public int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void logAsSeparated(DetailAST ast)
ast - tokenprivate void logAsTotal(DetailAST ast)
ast - current tokenprivate void countTokens(DetailAST ast, int depth)
ast - the root token for descendants.depth - the maximum depth of the counted descendants.public void setLimitedTokens(java.lang.String... limitedTokensParam)
limitedTokensParam - - list of tokens to ignore.public void setMinimumDepth(int minimumDepth)
minimumDepth - the minimum depth for descendant counts.public void setMaximumDepth(int maximumDepth)
maximumDepth - the maximum depth for descendant counts.public void setMinimumNumber(int minimumNumber)
minimumNumber - the minimum count for descendants.public void setMaximumNumber(int maximumNumber)
maximumNumber - the maximum count for descendants.public void setMinimumMessage(java.lang.String message)
message - the violation message for minimum count not reached.
Used as a MessageFormat pattern with arguments
public void setMaximumMessage(java.lang.String message)
message - the violation message for maximum count exceeded.
Used as a MessageFormat pattern with arguments
public void setSumTokenCounts(boolean sum)
sum - whether to use the sum.Copyright © 2001-2022. All Rights Reserved.