Class ReturnCountExtendedCheck

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

    public class ReturnCountExtendedCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Checks that method/ctor "return" literal count is not greater than the given value ("maxReturnCount" property).

    Rationale:

    One return per method is a good practice as its ease understanding of method logic.

    Reasoning is that:
    • It is easier to understand control flow when you know exactly where the method returns.
    • Methods with 2-3 or many "return" statements are much more difficult to understand, debug and refactor.
    Setting up the check options will make it to ignore:
    1. Methods by name ("ignoreMethodsNames" property). Note, that the "ignoreMethodsNames" property type is a RegExp: using this property you can list the names of ignored methods separated by comma (but you can also use '|' to separate different method names in usual for RegExp style). If the violation is on a lambda, since it has no method name, you can specify the string null to ignore all lambda violations for now. It should be noted, that ignoring lambdas this way may not always be supported as it is a hack and giving all lambdas the same name. It could be changed if a better way to single out individual lambdas if found.
    2. Methods which linelength less than given value ("linesLimit" property).
    3. "return" statements which depth is greater or equal to the given value ("returnDepthLimit" property). There are few supported
      coding blocks when depth counting: "if-else", "for", "while"/"do-while" and "switch".
    4. "Empty" return statements = return statements in void methods and ctors that have not any expression ("ignoreEmptyReturns" property).
    5. Return statements, which are located in the top lines of method/ctor (you can specify the count of top method/ctor lines that will be ignored using "rowsToIgnoreCount" property).
    So, this is much improved version of the existing ReturnCountCheck.

    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_CTOR
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_LAMBDA
      A key is pointing to the warning message text in "messages.properties" file.
      static String MSG_KEY_METHOD
      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 setIgnoreEmptyReturns​(boolean ignoreEmptyReturns)
      Sets the "ignoring empty return statements in void methods and ctors and lambdas" option state.
      void setIgnoreMethodLinesCount​(int ignoreMethodLinesCount)
      Sets the maximum number of lines of which method/ctor/lambda body may consist to be skipped by check.
      void setIgnoreMethodsNames​(String... ignoreMethodNames)
      Sets the RegExp patterns for methods' names which would be ignored by check.
      void setMaxReturnCount​(int maxReturnCount)
      Sets maximum allowed "return" literals count per method/ctor/lambda.
      void setMinIgnoreReturnDepth​(int minIgnoreReturnDepth)
      Sets the minimum "return" statement depth with that will be skipped by check.
      void setTopLinesToIgnoreCount​(int topLinesToIgnoreCount)
      Sets the count of code lines on the top of each processed method/ctor that will be ignored by check.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST node)  
      • 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_METHOD

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

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

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

      • ReturnCountExtendedCheck

        public ReturnCountExtendedCheck()
        Creates the new check instance.
    • Method Detail

      • setIgnoreMethodsNames

        public void setIgnoreMethodsNames​(String... ignoreMethodNames)
        Sets the RegExp patterns for methods' names which would be ignored by check.
        Parameters:
        ignoreMethodNames - list of the RegExp patterns for methods' names which should be ignored by check
      • setMaxReturnCount

        public void setMaxReturnCount​(int maxReturnCount)
        Sets maximum allowed "return" literals count per method/ctor/lambda.
        Parameters:
        maxReturnCount - - the new "maxReturnCount" property value.
        See Also:
        maxReturnCount
      • setIgnoreMethodLinesCount

        public void setIgnoreMethodLinesCount​(int ignoreMethodLinesCount)
        Sets the maximum number of lines of which method/ctor/lambda body may consist to be skipped by check.
        Parameters:
        ignoreMethodLinesCount - - the new value of "ignoreMethodLinesCount" property.
        See Also:
        ignoreMethodLinesCount
      • setMinIgnoreReturnDepth

        public void setMinIgnoreReturnDepth​(int minIgnoreReturnDepth)
        Sets the minimum "return" statement depth with that will be skipped by check.
        Parameters:
        minIgnoreReturnDepth - - the new "minIgnoreReturnDepth" property value.
      • setIgnoreEmptyReturns

        public void setIgnoreEmptyReturns​(boolean ignoreEmptyReturns)
        Sets the "ignoring empty return statements in void methods and ctors and lambdas" option state.
        Parameters:
        ignoreEmptyReturns - the new "allowEmptyReturns" property value.
        See Also:
        ignoreEmptyReturns
      • setTopLinesToIgnoreCount

        public void setTopLinesToIgnoreCount​(int topLinesToIgnoreCount)
        Sets the count of code lines on the top of each processed method/ctor that will be ignored by check.
        Parameters:
        topLinesToIgnoreCount - the new "rowsToIgnoreCount" property value.
        See Also:
        topLinesToIgnoreCount
      • 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 node)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck