com.puppycrawl.tools.checkstyle.checks

Class DescendantTokenCheck

public class DescendantTokenCheck extends Check

Checks for restricted tokens beneath other tokens.

Examples of how to configure the check:

 <!-- 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>

 <!-- Switch with no default -->
 <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>

 <!-- Assert statement may have side effects -->
 <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>

 <!-- Initialiser in for performs no setup - use while instead? -->
 <module name="DescendantToken">
     <property name="tokens" value="FOR_INIT"/>
     <property name="limitedTokens" value="EXPR"/>
     <property name="minimumNumber" value="1"/>
 </module>

 <!-- Condition in for performs no check -->
 <module name="DescendantToken">
     <property name="tokens" value="FOR_CONDITION"/>
     <property name="limitedTokens" value="EXPR"/>
     <property name="minimumNumber" value="1"/>
 </module>

 <!-- Switch within 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>

 <!-- Return 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>

 <!-- Try within 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>

 <!-- Too many cases within a switch -->
 <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>

 <!-- Too many local variables within a method -->
 <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>

 <!-- Too many returns from within a method -->
 <module name="DescendantToken">
     <property name="tokens" value="METHOD_DEF"/>
     <property name="limitedTokens" value="LITERAL_RETURN"/>
     <property name="maximumNumber" value="3"/>
 </module>

 <!-- Too many fields within an interface -->
 <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>

 <!-- Limit the number of exceptions a method can throw -->
 <module name="DescendantToken">
     <property name="tokens" value="LITERAL_THROWS"/>
     <property name="limitedTokens" value="IDENT"/>
     <property name="maximumNumber" value="1"/>
 </module>

 <!-- Limit the number of expressions in a method -->
 <module name="DescendantToken">
     <property name="tokens" value="METHOD_DEF"/>
     <property name="limitedTokens" value="EXPR"/>
     <property name="maximumNumber" value="200"/>
 </module>

 <!-- Disallow empty statements -->
 <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>

 <!-- Too many fields within a class -->
 <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>
 

Author: Tim Tyler <tim@tt1.org> Rick Giles

Method Summary
int[]getAcceptableTokens()
{@inheritDoc}
int[]getDefaultTokens()
{@inheritDoc}
voidsetLimitedTokens(String[] aLimitedTokens)
Sets the tokens which occurance as descendant is limited.
voidsetMaximumDepth(int aMaximumDepth)
Sets the maximum depth for descendant counts.
voidsetMaximumMessage(String aMessage)
Sets the error message for maximum count exceeded.
voidsetMaximumNumber(int aMaximumNumber)
Sets a maximum count for descendants.
voidsetMinimumDepth(int aMinimumDepth)
Sets the mimimum depth for descendant counts.
voidsetMinimumMessage(String aMessage)
Sets the error message for minimum count not reached.
voidsetMinimumNumber(int aMinimumNumber)
Sets a minimum count for descendants.
voidvisitToken(DetailAST aAST)
{@inheritDoc}

Method Detail

getAcceptableTokens

public int[] getAcceptableTokens()
{@inheritDoc}

getDefaultTokens

public int[] getDefaultTokens()
{@inheritDoc}

setLimitedTokens

public void setLimitedTokens(String[] aLimitedTokens)
Sets the tokens which occurance as descendant is limited.

Parameters: aLimitedTokens - list of tokens to ignore.

setMaximumDepth

public void setMaximumDepth(int aMaximumDepth)
Sets the maximum depth for descendant counts.

Parameters: aMaximumDepth the maximum depth for descendant counts.

setMaximumMessage

public void setMaximumMessage(String aMessage)
Sets the error message for maximum count exceeded.

Parameters: aMessage the error message for maximum count exceeded. Used as a MessageFormat pattern with arguments

setMaximumNumber

public void setMaximumNumber(int aMaximumNumber)
Sets a maximum count for descendants.

Parameters: aMaximumNumber the maximum count for descendants.

setMinimumDepth

public void setMinimumDepth(int aMinimumDepth)
Sets the mimimum depth for descendant counts.

Parameters: aMinimumDepth the mimimum depth for descendant counts.

setMinimumMessage

public void setMinimumMessage(String aMessage)
Sets the error message for minimum count not reached.

Parameters: aMessage the error message for minimum count not reached. Used as a MessageFormat pattern with arguments

setMinimumNumber

public void setMinimumNumber(int aMinimumNumber)
Sets a minimum count for descendants.

Parameters: aMinimumNumber the minimum count for descendants.

visitToken

public void visitToken(DetailAST aAST)
{@inheritDoc}