Class LambdaBodyLengthCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class LambdaBodyLengthCheck
    extends AbstractCheck

    Checks lambda body length.

    Rationale: Similar to anonymous inner classes, if lambda body becomes very long it is hard to understand and to see the flow of the method where the lambda is defined. Therefore, long lambda body should usually be extracted to method.

    • Property max - Specify the maximum number of lines allowed. Type is int. Default value is 10.

    To configure the check to accept lambda bodies with up to 10 lines:

     <module name="LambdaBodyLength"/>
     

    Example:

     class Test {
       Runnable r = () -> { // violation, 11 lines
           System.out.println(2); // line 2 of lambda
           System.out.println(3);
           System.out.println(4);
           System.out.println(5);
           System.out.println(6);
           System.out.println(7);
           System.out.println(8);
           System.out.println(9);
           System.out.println(10);
       }; // line 11
    
       Runnable r2 = () -> // violation, 11 lines
           "someString".concat("1") // line 1 of lambda
                       .concat("2")
                       .concat("3")
                       .concat("4")
                       .concat("5")
                       .concat("6")
                       .concat("7")
                       .concat("8")
                       .concat("9")
                       .concat("10")
                       .concat("11"); // line 11
    
       Runnable r3 = () -> { // ok, 10 lines
           System.out.println(2); // line 2 of lambda
           System.out.println(3);
           System.out.println(4);
           System.out.println(5);
           System.out.println(6);
           System.out.println(7);
           System.out.println(8);
           System.out.println(9);
       }; // line 10
     }
     

    To configure the check to accept lambda bodies with max 5 lines:

     <module name="LambdaBodyLength">
       <property name="max" value="5"/>
     </module>
     

    Example:

     class Test {
       Runnable r = () -> { // violation, 6 lines
           System.out.println(2); // line 2 of lambda
           System.out.println(3);
           System.out.println(4);
           System.out.println(5);
       };
    
       Runnable r2 = () -> // violation, 6 lines
           "someString".concat("1")
                       .concat("2")
                       .concat("3")
                       .concat("4")
                       .concat("5")
                       .concat("6");
    
       Runnable r3 = () -> { // ok, 5 lines
           System.out.println(2);
           System.out.println(3);
           System.out.println(4);
       };
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • maxLen.lambdaBody
    Since:
    8.37
    • Field Detail

      • MSG_KEY

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

        private int max
        Specify the maximum number of lines allowed.
    • Method Detail

      • setMax

        public void setMax​(int length)
        Setter to specify the maximum number of lines allowed.
        Parameters:
        length - the maximum length of lambda body.
      • getAcceptableTokens

        public int[] 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 class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • getLength

        private static int getLength​(DetailAST ast)
        Get length of lambda body.
        Parameters:
        ast - lambda body node.
        Returns:
        length of lambda body.
      • getLastNodeLineNumber

        private static int getLastNodeLineNumber​(DetailAST lambdaBody)
        Get last child node in the tree line number.
        Parameters:
        lambdaBody - lambda body node.
        Returns:
        last child node in the tree line number.
      • getFirstNodeLineNumber

        private static int getFirstNodeLineNumber​(DetailAST lambdaBody)
        Get first child node in the tree line number.
        Parameters:
        lambdaBody - lambda body node.
        Returns:
        first child node in the tree line number.