Class ChildBlockLengthCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class ChildBlockLengthCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    This check detects the child blocks, which length is more then 80% of parent block length.

    Supported keywords are used to detect blocks:
    "if", "else", "for", "switch", "do", "while", "try", "catch".

    Rationale:

    Length of child block that is more then 80% of parent block is usually hard to read in case child block is long(few display screens). Such child blocks should be refactored or moved to separate method.

    Since:
    1.8.0
    Author:
    Daniil Yaroslavtsev
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      A key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setBlockTypes​(String... blockTypes)
      Sets allowed types of blocks to be checked.
      void setIgnoreBlockLinesCount​(int ignoreBlockLinesCount)
      Sets the maximum linelength of blocks that will be ignored by check.
      void setMaxChildBlockPercentage​(int maxChildBlockPercentage)
      Sets the maximum percentage ratio between child and parent block.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • 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.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ChildBlockLengthCheck

        public ChildBlockLengthCheck()
    • Method Detail

      • setBlockTypes

        public void setBlockTypes​(String... blockTypes)
        Sets allowed types of blocks to be checked. Supported block types: LITERAL_IF, LITERAL_SWITCH, LITERAL_FOR, LITERAL_DO, LITERAL_WHILE, LITERAL_TRY, LITERAL_ELSE, LITERAL_CATCH.
        Parameters:
        blockTypes - - DetailAST tokenTypes that are related to the types which are allowed by user in check preferences.
      • setMaxChildBlockPercentage

        public void setMaxChildBlockPercentage​(int maxChildBlockPercentage)
        Sets the maximum percentage ratio between child and parent block. (sets "maxChildBlockPercentage" option value)
        Parameters:
        maxChildBlockPercentage - the new "maxChildBlockPercentage" option value.
      • setIgnoreBlockLinesCount

        public void setIgnoreBlockLinesCount​(int ignoreBlockLinesCount)
        Sets the maximum linelength of blocks that will be ignored by check.
        Parameters:
        ignoreBlockLinesCount - the maximum linelength of the block to be ignored.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck