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 same type sibling 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.
allowNoEmptyLineBetweenFields
- Allow no empty line between fields.
Type is boolean
.
Default value is false
.
allowMultipleEmptyLines
- Allow multiple empty lines between class members.
Type is boolean
.
Default value is true
.
allowMultipleEmptyLinesInsideClassMembers
- Allow multiple
empty lines inside class members.
Type is boolean
.
Default value is true
.
tokens
- tokens to check
Type is java.lang.String[]
.
Validation type is tokenSet
.
Default value is:
PACKAGE_DEF,
IMPORT,
STATIC_IMPORT,
CLASS_DEF,
INTERFACE_DEF,
ENUM_DEF,
STATIC_INIT,
INSTANCE_INIT,
METHOD_DEF,
CTOR_DEF,
VARIABLE_DEF,
RECORD_DEF,
COMPACT_CTOR_DEF.
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 Foo { // violation, 'CLASS_DEF' should be separated from previous line. public static final int FOO_CONST = 1; public void foo() {} // violation, 'METHOD_DEF' should be separated from previous line. }
Example of declarations with empty line separator that is expected by the Check by default:
/////////////////////////////////////////////////// //HEADER /////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.whitespace; import java.io.Serializable; class Foo { public static final int FOO_CONST = 1; public void foo() {} }
To check empty line before VARIABLE_DEF and METHOD_DEF:
<module name="EmptyLineSeparator"> <property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/> </module>
To allow no empty line between fields:
<module name="EmptyLineSeparator"> <property name="allowNoEmptyLineBetweenFields" value="true"/> </module>
Example:
class Foo { int field1; // ok double field2; // ok long field3, field4 = 10L, field5; // ok }
Example of declarations with multiple empty lines between class members (allowed by default):
/////////////////////////////////////////////////// //HEADER /////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.whitespace; import java.io.Serializable; class Foo { public static final int FOO_CONST = 1; public void foo() {} // OK }
To disallow multiple empty lines between class members:
<module name="EmptyLineSeparator"> <property name="allowMultipleEmptyLines" value="false"/> </module>
/////////////////////////////////////////////////// //HEADER /////////////////////////////////////////////////// package com.checkstyle.whitespace; // violation, 'package' has more than 1 empty lines before. import java.io.Serializable; // violation, 'import' has more than 1 empty lines before. class Foo { // violation, 'CLASS_DEF' has more than 1 empty lines before. public static final int FOO_CONST = 1; public void foo() {} // violation, 'METHOD_DEF' has more than 1 empty lines before. }
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.puppycrawl.tools.checkstyle.whitespace; class Foo { public void foo() { System.out.println(1); // violation, There is more than 1 empty line one after another // in previous line. } }
To disallow multiple empty lines between class members:
<module name="EmptyLineSeparator"> <property name="allowMultipleEmptyLines" value="false"/> </module>
Example:
package com.puppycrawl.tools.checkstyle.whitespace; class Test { private int k; private static void foo() {} // violation, 'METHOD_DEF' has more than 1 empty lines before. }
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
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
private boolean |
allowMultipleEmptyLines
Allow multiple empty lines between class members.
|
private boolean |
allowMultipleEmptyLinesInsideClassMembers
Allow multiple empty lines inside class members.
|
private boolean |
allowNoEmptyLineBetweenFields
Allow no empty line between fields.
|
static java.lang.String |
MSG_MULTIPLE_LINES
A key is pointing to the warning message empty.line.separator.multiple.lines
in "messages.properties"
file.
|
static java.lang.String |
MSG_MULTIPLE_LINES_AFTER
A key is pointing to the warning message empty.line.separator.lines.after
in "messages.properties" file.
|
static java.lang.String |
MSG_MULTIPLE_LINES_INSIDE
A key is pointing to the warning message empty.line.separator.multiple.lines.inside
in "messages.properties" file.
|
static java.lang.String |
MSG_SHOULD_BE_SEPARATED
A key is pointing to the warning message empty.line.separator in "messages.properties"
file.
|
Constructor and Description |
---|
EmptyLineSeparatorCheck() |
Modifier and Type | Method and Description |
---|---|
private void |
checkCommentInModifiers(DetailAST packageDef)
Checks that packageDef token is separated from comment in modifiers.
|
private void |
checkComments(DetailAST token)
Check if group of comments located right before token has more than one previous empty line.
|
private void |
checkCommentsInsideToken(DetailAST token)
Check if group of comments located at the start of token has more than one previous empty
line.
|
private void |
checkToken(DetailAST ast,
DetailAST nextToken)
Checks that token and next token are separated.
|
private static java.util.Optional<DetailAST> |
findCommentUnder(DetailAST packageDef)
Finds comment in next sibling of given packageDef.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private java.util.List<java.lang.Integer> |
getEmptyLines(DetailAST ast)
Get list of empty lines.
|
private static java.util.List<java.lang.Integer> |
getEmptyLinesToLog(java.util.List<java.lang.Integer> emptyLines)
Get list of empty lines to log.
|
private static DetailAST |
getLastElementBeforeEmptyLines(DetailAST ast,
int line)
Returns the element after which empty lines exist.
|
private static java.util.Optional<DetailAST> |
getPostFixNode(DetailAST ast)
Gets postfix Node from AST if present.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static DetailAST |
getViolationAstForPackage(DetailAST ast)
Gets the Ast on which violation is to be given for package declaration.
|
private boolean |
hasEmptyLine(int startLine,
int endLine)
Checks, whether there are empty lines within the specified line range.
|
private boolean |
hasEmptyLineAfter(DetailAST token)
Checks if token have empty line after.
|
private boolean |
hasEmptyLineBefore(DetailAST token)
Checks if a token has a empty line before.
|
private boolean |
hasMultipleLinesBefore(DetailAST ast)
Whether the token has not allowed multiple empty lines before.
|
private boolean |
hasNotAllowedTwoEmptyLinesBefore(DetailAST token)
Checks if a token has empty two previous lines and multiple empty lines is not allowed.
|
private static boolean |
isClassMemberBlock(int astType)
Whether the AST is a class member block.
|
private boolean |
isCommentInBeginningOfLine(DetailAST comment)
Check if token is comment, which starting in beginning of line.
|
boolean |
isCommentNodesRequired()
Whether comment nodes are required or not.
|
private static boolean |
isLineEmptyAfterPackage(DetailAST ast)
Checks if there is another element at next line of package declaration.
|
private static boolean |
isPrecededByJavadoc(DetailAST token)
Check if token is preceded by javadoc comment.
|
private boolean |
isPrePreviousLineEmpty(DetailAST token)
Checks if a token has empty pre-previous line.
|
private static boolean |
isTypeField(DetailAST variableDef)
If variable definition is a type field.
|
private boolean |
isViolatingEmptyLineBetweenFieldsPolicy(DetailAST detailAST)
Checks whether token placement violates policy of empty line between fields.
|
private void |
processImport(DetailAST ast,
DetailAST nextToken)
Process Import.
|
private void |
processMultipleLinesInside(DetailAST ast)
Log violation in case there are multiple empty lines inside constructor,
initialization block or method.
|
private void |
processPackage(DetailAST ast,
DetailAST nextToken)
Process Package.
|
private void |
processVariableDef(DetailAST ast,
DetailAST nextToken)
Process Variable.
|
void |
setAllowMultipleEmptyLines(boolean allow)
Setter to allow multiple empty lines between class members.
|
void |
setAllowMultipleEmptyLinesInsideClassMembers(boolean allow)
Setter to allow multiple empty lines inside class members.
|
void |
setAllowNoEmptyLineBetweenFields(boolean allow)
Setter to allow no empty line between fields.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final java.lang.String MSG_SHOULD_BE_SEPARATED
public static final java.lang.String MSG_MULTIPLE_LINES
public static final java.lang.String MSG_MULTIPLE_LINES_AFTER
public static final java.lang.String MSG_MULTIPLE_LINES_INSIDE
private boolean allowNoEmptyLineBetweenFields
private boolean allowMultipleEmptyLines
private boolean allowMultipleEmptyLinesInsideClassMembers
public EmptyLineSeparatorCheck()
public final void setAllowNoEmptyLineBetweenFields(boolean allow)
allow
- User's value.public void setAllowMultipleEmptyLines(boolean allow)
allow
- User's value.public void setAllowMultipleEmptyLinesInsideClassMembers(boolean allow)
allow
- User's value.public boolean isCommentNodesRequired()
AbstractCheck
isCommentNodesRequired
in class AbstractCheck
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate void checkToken(DetailAST ast, DetailAST nextToken)
ast
- token to validatenextToken
- next sibling of the tokenprivate void checkCommentInModifiers(DetailAST packageDef)
packageDef
- package def tokenprivate void processMultipleLinesInside(DetailAST ast)
ast
- the ast to check.private static DetailAST getLastElementBeforeEmptyLines(DetailAST ast, int line)
ast
- the ast to check.line
- the empty line which gives violation.private static java.util.Optional<DetailAST> getPostFixNode(DetailAST ast)
ast
- the AST used to get postfix Node.private static boolean isClassMemberBlock(int astType)
astType
- the AST to check.private java.util.List<java.lang.Integer> getEmptyLines(DetailAST ast)
ast
- the ast to check.private static java.util.List<java.lang.Integer> getEmptyLinesToLog(java.util.List<java.lang.Integer> emptyLines)
emptyLines
- list of empty lines.private boolean hasMultipleLinesBefore(DetailAST ast)
ast
- the ast to check.private void processPackage(DetailAST ast, DetailAST nextToken)
ast
- tokennextToken
- next tokenprivate static boolean isLineEmptyAfterPackage(DetailAST ast)
ast
- Package ast.private static DetailAST getViolationAstForPackage(DetailAST ast)
ast
- Package ast.private void processImport(DetailAST ast, DetailAST nextToken)
ast
- tokennextToken
- next tokenprivate void processVariableDef(DetailAST ast, DetailAST nextToken)
ast
- tokennextToken
- next Tokenprivate boolean isViolatingEmptyLineBetweenFieldsPolicy(DetailAST detailAST)
detailAST
- token to be analyzedprivate boolean hasNotAllowedTwoEmptyLinesBefore(DetailAST token)
token
- DetailAST tokenprivate void checkComments(DetailAST token)
token
- DetailAST tokenprivate void checkCommentsInsideToken(DetailAST token)
token
- DetailAST tokenprivate boolean isPrePreviousLineEmpty(DetailAST token)
token
- DetailAST token.private boolean hasEmptyLineAfter(DetailAST token)
token
- token.private static java.util.Optional<DetailAST> findCommentUnder(DetailAST packageDef)
packageDef
- token to checkprivate boolean hasEmptyLine(int startLine, int endLine)
startLine
- number of the first line in the rangeendLine
- number of the second line in the rangetrue
if found any blank line within the range, false
otherwiseprivate boolean hasEmptyLineBefore(DetailAST token)
token
- token.private boolean isCommentInBeginningOfLine(DetailAST comment)
comment
- comment token for check.private static boolean isPrecededByJavadoc(DetailAST token)
token
- token for check.private static boolean isTypeField(DetailAST variableDef)
variableDef
- variable definition.Copyright © 2001-2021. All Rights Reserved.