Class NeedBracesCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class NeedBracesCheck
    extends AbstractCheck
    Checks for braces around code blocks.
    • Property allowEmptyLoopBody - Allow loops with empty bodies. Type is boolean. Default value is false.
    • Property allowSingleLineStatement - Allow single-line statements without braces. Type is boolean. Default value is false.
    • Property tokens - tokens to check Type is java.lang.String[]. Validation type is tokenSet. Default value is: LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE.

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • needBraces
    Since:
    3.0
    • Method Detail

      • setAllowSingleLineStatement

        public void setAllowSingleLineStatement​(boolean allowSingleLineStatement)
        Setter to allow single-line statements without braces.
        Parameters:
        allowSingleLineStatement - Check's option for skipping single-line statements
        Since:
        6.5
      • setAllowEmptyLoopBody

        public void setAllowEmptyLoopBody​(boolean allowEmptyLoopBody)
        Setter to allow loops with empty bodies.
        Parameters:
        allowEmptyLoopBody - Check's option for allowing loops with empty body.
        Since:
        6.12.1
      • 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
      • isEmptyLoopBodyAllowed

        private boolean isEmptyLoopBodyAllowed​(DetailAST ast)
        Checks if current loop has empty body and can be skipped by this check.
        Parameters:
        ast - for, while statements.
        Returns:
        true if current loop can be skipped by check.
      • hasUnbracedStatements

        private static boolean hasUnbracedStatements​(DetailAST ast)
        Checks if switch member (case, default statements) has statements without curly braces.
        Parameters:
        ast - case, default statements.
        Returns:
        true if switch member has unbraced statements, false otherwise.
      • isSkipStatement

        private boolean isSkipStatement​(DetailAST statement)
        Checks if current statement can be skipped by "need braces" warning.
        Parameters:
        statement - if, for, while, do-while, lambda, else, case, default statements.
        Returns:
        true if current statement can be skipped by Check.
      • isSingleLineStatement

        private static boolean isSingleLineStatement​(DetailAST statement)
        Checks if current statement is single-line statement, e.g.:

        if (obj.isValid()) return true;

        while (obj.isValid()) return true;

        Parameters:
        statement - if, for, while, do-while, lambda, else, case, default statements.
        Returns:
        true if current statement is single-line statement.
      • isSingleLineWhile

        private static boolean isSingleLineWhile​(DetailAST literalWhile)
        Checks if current while statement is single-line statement, e.g.:

        while (obj.isValid()) return true;

        Parameters:
        literalWhile - while statement.
        Returns:
        true if current while statement is single-line statement.
      • isSingleLineDoWhile

        private static boolean isSingleLineDoWhile​(DetailAST literalDo)
        Checks if current do-while statement is single-line statement, e.g.:

        do this.notify(); while (o != null);

        Parameters:
        literalDo - do-while statement.
        Returns:
        true if current do-while statement is single-line statement.
      • isSingleLineFor

        private static boolean isSingleLineFor​(DetailAST literalFor)
        Checks if current for statement is single-line statement, e.g.:

        for (int i = 0; ; ) this.notify();

        Parameters:
        literalFor - for statement.
        Returns:
        true if current for statement is single-line statement.
      • isSingleLineIf

        private static boolean isSingleLineIf​(DetailAST literalIf)
        Checks if current if statement is single-line statement, e.g.:

        if (obj.isValid()) return true;

        Parameters:
        literalIf - if statement.
        Returns:
        true if current if statement is single-line statement.
      • isSingleLineLambda

        private static boolean isSingleLineLambda​(DetailAST lambda)
        Checks if current lambda statement is single-line statement, e.g.:

        Runnable r = () -> System.out.println("Hello, world!");

        Parameters:
        lambda - lambda statement.
        Returns:
        true if current lambda statement is single-line statement.
      • getLastLambdaToken

        private static DetailAST getLastLambdaToken​(DetailAST lambda)
        Looks for the last token in lambda.
        Parameters:
        lambda - token to check.
        Returns:
        last token in lambda
      • isInSwitchRule

        private static boolean isInSwitchRule​(DetailAST ast)
        Checks if current ast's parent is a switch rule, e.g.:

        case 1 -> monthString = "January";

        Parameters:
        ast - the ast to check.
        Returns:
        true if current ast belongs to a switch rule.
      • isSingleLineSwitchMember

        private static boolean isSingleLineSwitchMember​(DetailAST statement)
        Checks if switch member (case or default statement) in a switch rule or case group is on a single-line.
        Parameters:
        statement - case statement or default statement.
        Returns:
        true if current switch member is single-line statement.
      • isSingleLineCaseGroup

        private static boolean isSingleLineCaseGroup​(DetailAST ast)
        Checks if switch member in case group (case or default statement) is single-line statement, e.g.:

        case 1: System.out.println("case one"); break; case 2: System.out.println("case two"); break; case 3: ; default: System.out.println("default"); break;

        Parameters:
        ast - case statement or default statement.
        Returns:
        true if current switch member is single-line statement.
      • isSingleLineSwitchRule

        private static boolean isSingleLineSwitchRule​(DetailAST ast)
        Checks if switch member in switch rule (case or default statement) is single-line statement, e.g.:

        case 1 -> System.out.println("case one"); case 2 -> System.out.println("case two"); default -> System.out.println("default");

        Parameters:
        ast - case statement or default statement.
        Returns:
        true if current switch label is single-line statement.
      • isSingleLineElse

        private static boolean isSingleLineElse​(DetailAST literalElse)
        Checks if current else statement is single-line statement, e.g.:

        else doSomeStuff();

        Parameters:
        literalElse - else statement.
        Returns:
        true if current else statement is single-line statement.