Class EmptyLineSeparatorCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- 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 after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.
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.
-
Property
allowNoEmptyLineBetweenFields
- Allow no empty line between fields. Type isboolean
. Default value isfalse
. -
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
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.
To configure the default check:
<module name="EmptyLineSeparator"/>
Example of declarations without empty line separator:
/////////////////////////////////////////////////// //HEADER /////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.whitespace; import java.io.Serializable; class Foo { public static final int FOO_CONST = 1; public void foo() {} //should be separated from previous statement. }
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 after 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 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() {} //should be separated from previous statement. }
To disallow multiple empty lines between class members:
<module name="EmptyLineSeparator"> <property name="allowMultipleEmptyLines" value="false"/> </module>
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 since method has 2 empty lines subsequently } }
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, before this like there two empty lines }
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.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description 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 Summary
Constructors Constructor Description EmptyLineSeparatorCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.boolean
isCommentNodesRequired()
Whether comment nodes are required or not.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, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, 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.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_SHOULD_BE_SEPARATED
public static final java.lang.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 java.lang.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 java.lang.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 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.- See Also:
- Constant Field Values
-
-
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.
-
setAllowMultipleEmptyLinesInsideClassMembers
public void setAllowMultipleEmptyLinesInsideClassMembers(boolean allow)
Setter to allow multiple empty lines inside class members.- Parameters:
allow
- User's value.
-
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
-
-