Class EmptyLineSeparatorCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class EmptyLineSeparatorCheck
    extends AbstractCheck

    Checks for empty line separators before package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.

    Checks for empty line separators before not only statements but implementation and documentation comments and blocks as well.

    ATTENTION: empty line separator is required between token siblings, not after line where token is found. If token does not have a sibling of the same type, then empty line is required at its end (for example for CLASS_DEF it is after '}'). Also, trailing comments are skipped.

    ATTENTION: violations from multiple empty lines cannot be suppressed via XPath: #8179.

    To configure the default check:

     <module name="EmptyLineSeparator"/>
     

    Example of declarations without empty line separator:

     ///////////////////////////////////////////////////
     //HEADER
     ///////////////////////////////////////////////////
     package com.whitespace; // violation , 'package' should be separated from previous line
     import java.io.Serializable; // violation , 'import' should be separated from previous line
    
     class FirstClass {
    
       int var1 = 1;
       int var2 = 2; // violation , 'VARIABLE_DEF' should be separated from previous line
    
    
       int var3 = 3;
    
    
       void method1() {}
       void method2() { // violation , 'METHOD_DEF' should be separated from previous line
          int var4 = 4;
    
    
          int var5 = 5;
       }
     }
     

    To check empty line before VARIABLE_DEF and METHOD_DEF:

       <module name="EmptyLineSeparator">
       <property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/>
       </module>
     
     ///////////////////////////////////////////////////
     //HEADER
     ///////////////////////////////////////////////////
     package com.whitespace;
     import java.io.Serializable;
    
     class FirstClass {
    
       int var1 = 1;
       int var2 = 2; // violation , 'VARIABLE_DEF' should be separated from previous line
    
    
       int var3 = 3;
    
    
       void method1() {}
        void method2() { // violation , 'METHOD_DEF' should be separated from previous line
          int var4 = 4;
    
    
          int var5 = 5;
       }
     }
     

    To allow no empty line between fields:

     <module name="EmptyLineSeparator">
       <property name="allowNoEmptyLineBetweenFields" value="true"/>
     </module>
     

    Example:

     ///////////////////////////////////////////////////
     //HEADER
     ///////////////////////////////////////////////////
     package com.whitespace; // violation , 'package' should be separated from previous line
     import java.io.Serializable; // violation , 'import' should be separated from previous line
    
     class FirstClass {
    
       int var1 = 1;
       int var2 = 2;
    
    
       int var3 = 3;
    
    
       void method1() {}
        void method2() { // violation , 'METHOD_DEF' should be separated from previous line
          int var4 = 4;
    
    
          int var5 = 5;
       }
     }
     

    To disallow multiple empty lines between class members:

     <module name="EmptyLineSeparator">
       <property name="allowMultipleEmptyLines" value="false"/>
     </module>
     
     ///////////////////////////////////////////////////
     //HEADER
     ///////////////////////////////////////////////////
     package com.whitespace; // violation , 'package' should be separated from previous line
     import java.io.Serializable; // violation , 'import' should be separated from previous line
    
     class FirstClass {
    
       int var1 = 1;
       int var2 = 2; // violation , 'VARIABLE_DEF' should be separated from previous line
    
    
       int var3 = 3; // violation , 'VARIABLE_DEF' has more than 1 empty lines before
    
    
       void method1() {} // violation , 'METHOD_DEF' has more than 1 empty lines before
        void method2() { // violation , 'METHOD_DEF' should be separated from previous line
          int var4 = 4;
    
    
          int var5 = 5;
       }
     }
     

    To disallow multiple empty lines inside constructor, initialization block and method:

     <module name="EmptyLineSeparator">
       <property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
     </module>
     

    The check is valid only for statements that have body: CLASS_DEF, INTERFACE_DEF, ENUM_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF.

    Example of declarations with multiple empty lines inside method:

     ///////////////////////////////////////////////////
     //HEADER
     ///////////////////////////////////////////////////
     package com.whitespace; // violation , 'package' should be separated from previous line
     import java.io.Serializable; // violation , 'import' should be separated from previous line
    
     class FirstClass {
    
       int var1 = 1;
       int var2 = 2; // violation , 'VARIABLE_DEF' should be separated from previous line
    
    
       int var3 = 3;
    
    
       void method1() {}
       void method2() { // violation , 'METHOD_DEF' should be separated from previous line
          int var4 = 4; // violation , There is more than 1 empty line after this line
    
    
          int var5 = 5;
       }
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • empty.line.separator
    • empty.line.separator.multiple.lines
    • empty.line.separator.multiple.lines.after
    • empty.line.separator.multiple.lines.inside
    Since:
    5.8
    • Method Detail

      • setAllowNoEmptyLineBetweenFields

        public final void setAllowNoEmptyLineBetweenFields​(boolean allow)
        Setter to allow no empty line between fields.
        Parameters:
        allow - User's value.
      • setAllowMultipleEmptyLines

        public void setAllowMultipleEmptyLines​(boolean allow)
        Setter to allow multiple empty lines between class members.
        Parameters:
        allow - User's value.
      • 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
      • checkToken

        private void checkToken​(DetailAST ast,
                                DetailAST nextToken)
        Checks that token and next token are separated.
        Parameters:
        ast - token to validate
        nextToken - next sibling of the token
      • checkCommentInModifiers

        private void checkCommentInModifiers​(DetailAST packageDef)
        Checks that packageDef token is separated from comment in modifiers.
        Parameters:
        packageDef - package def token
      • processMultipleLinesInside

        private void processMultipleLinesInside​(DetailAST ast)
        Log violation in case there are multiple empty lines inside constructor, initialization block or method.
        Parameters:
        ast - the ast to check.
      • getLastElementBeforeEmptyLines

        private static DetailAST getLastElementBeforeEmptyLines​(DetailAST ast,
                                                                int line)
        Returns the element after which empty lines exist.
        Parameters:
        ast - the ast to check.
        line - the empty line which gives violation.
        Returns:
        The DetailAST after which empty lines are present.
      • getPostFixNode

        private static Optional<DetailASTgetPostFixNode​(DetailAST ast)
        Gets postfix Node from AST if present.
        Parameters:
        ast - the AST used to get postfix Node.
        Returns:
        Optional postfix node.
      • isClassMemberBlock

        private static boolean isClassMemberBlock​(int astType)
        Whether the AST is a class member block.
        Parameters:
        astType - the AST to check.
        Returns:
        true if the AST is a class member block.
      • getEmptyLines

        private List<IntegergetEmptyLines​(DetailAST ast)
        Get list of empty lines.
        Parameters:
        ast - the ast to check.
        Returns:
        list of line numbers for empty lines.
      • hasMultipleLinesBefore

        private boolean hasMultipleLinesBefore​(DetailAST ast)
        Whether the token has not allowed multiple empty lines before.
        Parameters:
        ast - the ast to check.
        Returns:
        true if the token has not allowed multiple empty lines before.
      • isLineEmptyAfterPackage

        private static boolean isLineEmptyAfterPackage​(DetailAST ast)
        Checks if there is another element at next line of package declaration.
        Parameters:
        ast - Package ast.
        Returns:
        true, if there is an element.
      • getViolationAstForPackage

        private static DetailAST getViolationAstForPackage​(DetailAST ast)
        Gets the Ast on which violation is to be given for package declaration.
        Parameters:
        ast - Package ast.
        Returns:
        Violation ast.
      • isViolatingEmptyLineBetweenFieldsPolicy

        private boolean isViolatingEmptyLineBetweenFieldsPolicy​(DetailAST detailAST)
        Checks whether token placement violates policy of empty line between fields.
        Parameters:
        detailAST - token to be analyzed
        Returns:
        true if policy is violated and warning should be raised; false otherwise
      • hasNotAllowedTwoEmptyLinesBefore

        private boolean hasNotAllowedTwoEmptyLinesBefore​(DetailAST token)
        Checks if a token has empty two previous lines and multiple empty lines is not allowed.
        Parameters:
        token - DetailAST token
        Returns:
        true, if token has empty two lines before and allowMultipleEmptyLines is false
      • checkComments

        private void checkComments​(DetailAST token)
        Check if group of comments located right before token has more than one previous empty line.
        Parameters:
        token - DetailAST token
      • checkCommentsInsideToken

        private void checkCommentsInsideToken​(DetailAST token)
        Check if group of comments located at the start of token has more than one previous empty line.
        Parameters:
        token - DetailAST token
      • isPrePreviousLineEmpty

        private boolean isPrePreviousLineEmpty​(DetailAST token)
        Checks if a token has empty pre-previous line.
        Parameters:
        token - DetailAST token.
        Returns:
        true, if token has empty lines before.
      • hasEmptyLineAfter

        private boolean hasEmptyLineAfter​(DetailAST token)
        Checks if token have empty line after.
        Parameters:
        token - token.
        Returns:
        true if token have empty line after.
      • findCommentUnder

        private static Optional<DetailASTfindCommentUnder​(DetailAST packageDef)
        Finds comment in next sibling of given packageDef.
        Parameters:
        packageDef - token to check
        Returns:
        comment under the token
      • hasEmptyLine

        private boolean hasEmptyLine​(int startLine,
                                     int endLine)
        Checks, whether there are empty lines within the specified line range. Line numbering is started from 1 for parameter values
        Parameters:
        startLine - number of the first line in the range
        endLine - number of the second line in the range
        Returns:
        true if found any blank line within the range, false otherwise
      • hasEmptyLineBefore

        private boolean hasEmptyLineBefore​(DetailAST token)
        Checks if a token has an empty line before.
        Parameters:
        token - token.
        Returns:
        true, if token have empty line before.
      • isCommentInBeginningOfLine

        private boolean isCommentInBeginningOfLine​(DetailAST comment)
        Check if token is comment, which starting in beginning of line.
        Parameters:
        comment - comment token for check.
        Returns:
        true, if token is comment, which starting in beginning of line.
      • isPrecededByJavadoc

        private static boolean isPrecededByJavadoc​(DetailAST token)
        Check if token is preceded by javadoc comment.
        Parameters:
        token - token for check.
        Returns:
        true, if token is preceded by javadoc comment.
      • isTypeField

        private static boolean isTypeField​(DetailAST variableDef)
        If variable definition is a type field.
        Parameters:
        variableDef - variable definition.
        Returns:
        true variable definition is a type field.