Class EmptyLineSeparatorCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.whitespace.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.
-
Property
allowMultipleEmptyLines
- Allow multiple empty lines between class members. Type isboolean
. Default value istrue
. -
Property
allowMultipleEmptyLinesInsideClassMembers
- Allow multiple empty lines inside class members. Type isboolean
. Default value istrue
. -
Property
allowNoEmptyLineBetweenFields
- Allow no empty line between fields. Type isboolean
. Default value isfalse
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. 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.
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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field 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 String
MSG_MULTIPLE_LINES
A key is pointing to the warning message empty.line.separator.multiple.lines in "messages.properties" file.static String
MSG_MULTIPLE_LINES_AFTER
A key is pointing to the warning message empty.line.separator.lines.after in "messages.properties" file.static String
MSG_MULTIPLE_LINES_INSIDE
A key is pointing to the warning message empty.line.separator.multiple.lines.inside in "messages.properties" file.static String
MSG_SHOULD_BE_SEPARATED
A key is pointing to the warning message empty.line.separator in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description EmptyLineSeparatorCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 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 List<Integer>
getEmptyLines(DetailAST ast)
Get list of empty lines.private static List<Integer>
getEmptyLinesToLog(Iterable<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 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 an 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.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_SHOULD_BE_SEPARATED
public static final String MSG_SHOULD_BE_SEPARATED
A key is pointing to the warning message empty.line.separator in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_MULTIPLE_LINES
public static final String MSG_MULTIPLE_LINES
A key is pointing to the warning message empty.line.separator.multiple.lines in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_MULTIPLE_LINES_AFTER
public static final String MSG_MULTIPLE_LINES_AFTER
A key is pointing to the warning message empty.line.separator.lines.after in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_MULTIPLE_LINES_INSIDE
public static final String MSG_MULTIPLE_LINES_INSIDE
A key is pointing to the warning message empty.line.separator.multiple.lines.inside in "messages.properties" file.- See Also:
- Constant Field Values
-
allowNoEmptyLineBetweenFields
private boolean allowNoEmptyLineBetweenFields
Allow no empty line between fields.
-
allowMultipleEmptyLines
private boolean allowMultipleEmptyLines
Allow multiple empty lines between class members.
-
allowMultipleEmptyLinesInsideClassMembers
private boolean allowMultipleEmptyLinesInsideClassMembers
Allow multiple empty lines inside class members.
-
-
Constructor Detail
-
EmptyLineSeparatorCheck
public EmptyLineSeparatorCheck()
-
-
Method Detail
-
setAllowNoEmptyLineBetweenFields
public final void setAllowNoEmptyLineBetweenFields(boolean allow)
Setter to allow no empty line between fields.- Parameters:
allow
- User's value.- Since:
- 5.8
-
setAllowMultipleEmptyLines
public void setAllowMultipleEmptyLines(boolean allow)
Setter to allow multiple empty lines between class members.- Parameters:
allow
- User's value.- Since:
- 6.3
-
setAllowMultipleEmptyLinesInsideClassMembers
public void setAllowMultipleEmptyLinesInsideClassMembers(boolean allow)
Setter to allow multiple empty lines inside class members.- Parameters:
allow
- User's value.- Since:
- 6.18
-
isCommentNodesRequired
public boolean isCommentNodesRequired()
Description copied from class:AbstractCheck
Whether comment nodes are required or not.- Overrides:
isCommentNodesRequired
in classAbstractCheck
- Returns:
- false as a default value.
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
checkToken
private void checkToken(DetailAST ast, DetailAST nextToken)
Checks that token and next token are separated.- Parameters:
ast
- token to validatenextToken
- 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<DetailAST> getPostFixNode(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<Integer> getEmptyLines(DetailAST ast)
Get list of empty lines.- Parameters:
ast
- the ast to check.- Returns:
- list of line numbers for empty lines.
-
getEmptyLinesToLog
private static List<Integer> getEmptyLinesToLog(Iterable<Integer> emptyLines)
Get list of empty lines to log.- Parameters:
emptyLines
- list of empty lines.- Returns:
- list of empty lines to log.
-
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.
-
processPackage
private void processPackage(DetailAST ast, DetailAST nextToken)
Process Package.- Parameters:
ast
- tokennextToken
- next token
-
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.
-
processImport
private void processImport(DetailAST ast, DetailAST nextToken)
Process Import.- Parameters:
ast
- tokennextToken
- next token
-
processVariableDef
private void processVariableDef(DetailAST ast, DetailAST nextToken)
Process Variable.- Parameters:
ast
- tokennextToken
- next Token
-
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<DetailAST> findCommentUnder(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 rangeendLine
- 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.
-
-