Class LeftCurlyCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class LeftCurlyCheck
    extends AbstractCheck

    Checks for the placement of left curly braces ('{') for code blocks.

    To configure the check:

     <module name="LeftCurly"/>
     
     class Test
     { // Violation - '{' should be on the previous line
       private interface TestInterface
       { // Violation - '{' should be on the previous line
       }
    
       private
       class
       MyClass { // OK
       }
    
       enum Colors {RED, // OK
         BLUE,
         GREEN;
       }
     }
     

    To configure the check to apply the nl policy to type blocks:

     <module name="LeftCurly">
       <property name="option" value="nl"/>
       <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
     </module>
     
     class Test
     { // OK
       private interface TestInterface
       { // OK
       }
    
       private
       class
       MyClass { // Violation - '{' should be on a new line
       }
    
       enum Colors {RED, // OK
         BLUE,
         GREEN;
       }
     }
     

    To configure the check to apply the nlow policy to type blocks:

     <module name="LeftCurly">
       <property name="option" value="nlow"/>
       <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
     </module>
     
     class Test
     { // Violation - '{' should be on the previous line
       private interface TestInterface { // OK
       }
    
       private
       class
       MyClass { // Violation - '{' should be on a new line
       }
    
       enum Colors {RED, // OK
         BLUE,
         GREEN;
       }
     }
     

    An example of how to configure the check to validate enum definitions:

     <module name="LeftCurly">
       <property name="ignoreEnums" value="false"/>
     </module>
     
     class Test
     { // Violation - '{' should be on the previous line
       private interface TestInterface
       { // Violation - '{' should be on the previous line
       }
    
       private
       class
       MyClass { // OK
       }
    
       enum Colors {RED, // Violation - '{' should have line break after
       BLUE,
       GREEN;
       }
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • line.break.after
    • line.new
    • line.previous
    Since:
    3.0
    • Method Detail

      • setOption

        public void setOption​(String optionStr)
        Setter to specify the policy on placement of a left curly brace ('{').
        Parameters:
        optionStr - string to decode option from
        Throws:
        IllegalArgumentException - if unable to decode
      • setIgnoreEnums

        public void setIgnoreEnums​(boolean ignoreEnums)
        Setter to allow to ignore enums when left curly brace policy is EOL.
        Parameters:
        ignoreEnums - check's option for ignoring enums.
      • 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
      • getBraceFromSwitchMember

        private static DetailAST getBraceFromSwitchMember​(DetailAST ast)
        Gets the brace of a switch statement/ expression member.
        Parameters:
        ast - DetailAST.
        Returns:
        DetailAST if the first child is TokenTypes.SLIST, null otherwise.
      • getBraceAsFirstChild

        private static DetailAST getBraceAsFirstChild​(DetailAST ast)
        Gets a SLIST if it is the first child of the AST.
        Parameters:
        ast - DetailAST.
        Returns:
        DetailAST if the first child is TokenTypes.SLIST, null otherwise.
      • skipModifierAnnotations

        private static DetailAST skipModifierAnnotations​(DetailAST ast)
        Skip all TokenTypes.ANNOTATIONs to the first non-annotation.
        Parameters:
        ast - DetailAST.
        Returns:
        DetailAST.
      • findLastAnnotation

        private static DetailAST findLastAnnotation​(DetailAST modifiers)
        Find the last token of type TokenTypes.ANNOTATION under the given set of modifiers.
        Parameters:
        modifiers - DetailAST.
        Returns:
        DetailAST or null if there are no annotations.
      • verifyBrace

        private void verifyBrace​(DetailAST brace,
                                 DetailAST startToken)
        Verifies that a specified left curly brace is placed correctly according to policy.
        Parameters:
        brace - token for left curly brace
        startToken - token for start of expression
      • validateEol

        private void validateEol​(DetailAST brace,
                                 String braceLine)
        Validate EOL case.
        Parameters:
        brace - brace AST
        braceLine - line content
      • validateNewLinePosition

        private void validateNewLinePosition​(DetailAST brace,
                                             DetailAST startToken,
                                             String braceLine)
        Validate token on new Line position.
        Parameters:
        brace - brace AST
        startToken - start Token
        braceLine - content of line with Brace
      • hasLineBreakAfter

        private boolean hasLineBreakAfter​(DetailAST leftCurly)
        Checks if left curly has line break after.
        Parameters:
        leftCurly - Left curly token.
        Returns:
        True, left curly has line break after.