Class ExpressionOverBlockLambdaCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that expression lambdas are used instead of single-line block lambdas where possible.

Rationale: According to the OpenJDK Java Style Guidelines (and general modern Java conventions), expression lambdas are preferred over single-line block lambdas for readability and conciseness.

A single-line block lambda is a lambda whose body is a block ({...}) that fits on a single line and contains only one statement that could be written as an expression lambda.

Since:
14.1.0
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • 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:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • isSwitchRuleLambda

      private static boolean isSwitchRuleLambda(DetailAST lambda)
      Checks if the lambda is a switch rule lambda.
      Parameters:
      lambda - the lambda AST node
      Returns:
      true if the lambda is part of a switch rule
    • isSingleLineLambda

      private static boolean isSingleLineLambda(DetailAST lambda)
      Checks if a lambda is single-line.
      Parameters:
      lambda - the lambda AST node
      Returns:
      true if the lambda fits on a single line
    • getLastLambdaToken

      private static DetailAST getLastLambdaToken(DetailAST lambda)
      Gets the last token in a lambda.
      Parameters:
      lambda - the lambda AST node
      Returns:
      the last token in the lambda
    • findSingleStatement

      private static DetailAST findSingleStatement(DetailAST slist)
      Finds the single statement in a block lambda body, or returns null if there are zero or multiple statements.
      Parameters:
      slist - the SLIST node (lambda body)
      Returns:
      the single statement, or null if not exactly one
    • isConvertibleToExpressionLambda

      private static boolean isConvertibleToExpressionLambda(DetailAST statement)
      Checks if the statement in a block lambda can be rewritten as an expression lambda.
      Parameters:
      statement - the statement node
      Returns:
      true if the statement can be converted to an expression lambda
    • hasReturnExpression

      private static boolean hasReturnExpression(DetailAST literalReturn)
      Checks if a return statement has an expression (not bare return).
      Parameters:
      literalReturn - the LITERAL_RETURN node
      Returns:
      true if the return statement has an expression