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.By default the check will check the following statements:
PACKAGE_DEF,IMPORT,CLASS_DEF,INTERFACE_DEF,STATIC_INIT,INSTANCE_INIT,METHOD_DEF,CTOR_DEF,VARIABLE_DEF.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. }An example of how to configure the check with default parameters is:
<module name="EmptyLineSeparator"/>
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() {} }An example how to check empty line after
VARIABLE_DEFandMETHOD_DEF:<module name="EmptyLineSeparator"> <property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/> </module>An example how 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() {} }An example how to disallow multiple empty lines between class members:
<module name="EmptyLineSeparator"> <property name="allowMultipleEmptyLines" value="false"/> </module>An example how to disallow multiple empty line inside methods, constructors, etc.:
<module name="EmptyLineSeparator"> <property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/> </module>The check is valid only for statements that have body:
TokenTypes.CLASS_DEF,TokenTypes.INTERFACE_DEF,TokenTypes.ENUM_DEF,TokenTypes.STATIC_INIT,TokenTypes.INSTANCE_INIT,TokenTypes.METHOD_DEF,TokenTypes.CTOR_DEFExample 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 } }
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowMultipleEmptyLinesAllows multiple empty lines between class members.private booleanallowMultipleEmptyLinesInsideClassMembersAllows multiple empty lines inside class members.private booleanallowNoEmptyLineBetweenFieldsAllows no empty line between fields.static java.lang.StringMSG_MULTIPLE_LINESA key is pointing to the warning message empty.line.separator.multiple.lines in "messages.properties" file.static java.lang.StringMSG_MULTIPLE_LINES_AFTERA key is pointing to the warning message empty.line.separator.lines.after in "messages.properties" file.static java.lang.StringMSG_MULTIPLE_LINES_INSIDEA key is pointing to the warning message empty.line.separator.multiple.lines.inside in "messages.properties" file.static java.lang.StringMSG_SHOULD_BE_SEPARATEDA 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 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.int[]getRequiredTokens()The tokens that this check must be registered for.private booleanhasEmptyLine(int startLine, int endLine)Checks, whether there are empty lines within the specified line range.private booleanhasEmptyLineAfter(DetailAST token)Checks if token have empty line after.private booleanhasEmptyLineBefore(DetailAST token)Checks if a token has a empty line before.private booleanhasMultipleLinesBefore(DetailAST ast)Whether the token has not allowed multiple empty lines before.private booleanhasNotAllowedTwoEmptyLinesBefore(DetailAST token)Checks if a token has empty two previous lines and multiple empty lines is not allowed.private static booleanisClassMemberBlock(int astType)Whether the AST is a class member block.private static booleanisComment(DetailAST ast)Check if token is a comment.booleanisCommentNodesRequired()Whether comment nodes are required or not.private static booleanisPrecededByJavadoc(DetailAST token)Check if token is preceded by javadoc comment.private booleanisPrePreviousLineEmpty(DetailAST token)Checks if a token has empty pre-previous line.private static booleanisTypeField(DetailAST variableDef)If variable definition is a type field.private booleanisViolatingEmptyLineBetweenFieldsPolicy(DetailAST detailAST)Checks whether token placement violates policy of empty line between fields.private voidprocessImport(DetailAST ast, DetailAST nextToken, int astType)Process Import.private voidprocessMultipleLinesInside(DetailAST ast)Log violation in case there are multiple empty lines inside constructor, initialization block or method.private voidprocessPackage(DetailAST ast, DetailAST nextToken)Process Package.private voidprocessVariableDef(DetailAST ast, DetailAST nextToken)Process Variable.voidsetAllowMultipleEmptyLines(boolean allow)Allow multiple empty lines between class members.voidsetAllowMultipleEmptyLinesInsideClassMembers(boolean allow)Allow multiple empty lines inside class members.voidsetAllowNoEmptyLineBetweenFields(boolean allow)Allow no empty line between fields.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, 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
-
allowNoEmptyLineBetweenFields
private boolean allowNoEmptyLineBetweenFields
Allows no empty line between fields.
-
allowMultipleEmptyLines
private boolean allowMultipleEmptyLines
Allows multiple empty lines between class members.
-
allowMultipleEmptyLinesInsideClassMembers
private boolean allowMultipleEmptyLinesInsideClassMembers
Allows multiple empty lines inside class members.
-
-
Method Detail
-
setAllowNoEmptyLineBetweenFields
public final void setAllowNoEmptyLineBetweenFields(boolean allow)
Allow no empty line between fields.- Parameters:
allow- User's value.
-
setAllowMultipleEmptyLines
public void setAllowMultipleEmptyLines(boolean allow)
Allow multiple empty lines between class members.- Parameters:
allow- User's value.
-
setAllowMultipleEmptyLinesInsideClassMembers
public void setAllowMultipleEmptyLinesInsideClassMembers(boolean allow)
Allow multiple empty lines inside class members.- Parameters:
allow- User's value.
-
isCommentNodesRequired
public boolean isCommentNodesRequired()
Description copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
isCommentNodesRequiredin classAbstractCheck- Returns:
- false as a default value.
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
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.
-
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 java.util.List<java.lang.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 java.util.List<java.lang.Integer> getEmptyLinesToLog(java.util.List<java.lang.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
-
processImport
private void processImport(DetailAST ast, DetailAST nextToken, int astType)
Process Import.- Parameters:
ast- tokennextToken- next tokenastType- token Type
-
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
-
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.
-
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:
trueif found any blank line within the range,falseotherwise
-
hasEmptyLineBefore
private boolean hasEmptyLineBefore(DetailAST token)
Checks if a token has a empty line before.- Parameters:
token- token.- Returns:
- true, if token have empty line before.
-
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.
-
isComment
private static boolean isComment(DetailAST ast)
Check if token is a comment.- Parameters:
ast- ast node- Returns:
- true, if given ast is 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.
-
-