EmptyLineSeparatorCheck was updated #218

This commit is contained in:
Max 2014-08-02 22:02:09 +04:00 committed by Roman Ivanov
parent 592d5d86b9
commit a5031acb33
5 changed files with 102 additions and 26 deletions

View File

@ -25,7 +25,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
*
* Checks for blank line separators after package, all import declarations,
* Checks for empty line separators after header, package, all import declarations,
* fields, constructors, methods, nested classes,
* static initializers and instance initializers.
*
@ -42,10 +42,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* </p>
*
* <p>
* Example of declarations without blank line separator:
* Example of declarations without empty line separator:
* </p>
*
* <pre>
* ///////////////////////////////////////////////////
* //HEADER
* ///////////////////////////////////////////////////
* package com.puppycrawl.tools.checkstyle.whitespace;
* import java.io.Serializable;
* class Foo
@ -63,11 +66,15 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* </pre>
*
* <p>
* Example of declarations with blank line separator
* Example of declarations with empty line separator
* that is expected by the Check by default:
* </p>
*
* <pre>
* ///////////////////////////////////////////////////
* //HEADER
* ///////////////////////////////////////////////////
*
* package com.puppycrawl.tools.checkstyle.whitespace;
*
* import java.io.Serializable;
@ -79,7 +86,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* public void foo() {}
* }
* </pre>
* <p> An example how to check blank line after
* <p> An example how to check empty line after
* {@link TokenTypes#VARIABLE_DEF VARIABLE_DEF} and
* {@link TokenTypes#METHOD_DEF METHOD_DEF}:
* </p>
@ -122,34 +129,36 @@ public class EmptyLineSeparatorCheck extends Check
final int astType = aAST.getType();
switch (astType) {
case TokenTypes.VARIABLE_DEF:
if (isTypeField(aAST) && !hasBlankLineAfter(aAST)) {
log(nextToken.getLineNo(),
"empty.line.separator", nextToken.getText());
if (isTypeField(aAST) && !hasEmptyLineAfter(aAST)) {
log(nextToken.getLineNo(), "empty.line.separator", nextToken.getText());
}
break;
case TokenTypes.IMPORT:
if (astType != nextToken.getType()
&& !hasBlankLineAfter(aAST))
if (astType != nextToken.getType() && !hasEmptyLineAfter(aAST)
|| (aAST.getLineNo() > 1 && !hasEmptyLineBefore(aAST)
&& aAST.getPreviousSibling() == null))
{
log(nextToken.getLineNo(),
"empty.line.separator", nextToken.getText());
log(nextToken.getLineNo(), "empty.line.separator", nextToken.getText());
}
break;
case TokenTypes.PACKAGE_DEF:
if (aAST.getLineNo() > 1 && !hasEmptyLineBefore(aAST)) {
log(aAST.getLineNo(), "empty.line.separator", aAST.getText());
}
default:
if (!hasBlankLineAfter(aAST)) {
log(nextToken.getLineNo(),
"empty.line.separator", nextToken.getText());
if (!hasEmptyLineAfter(aAST)) {
log(nextToken.getLineNo(), "empty.line.separator", nextToken.getText());
}
}
}
}
/**
* Checks if token have blank line after.
* Checks if token have empty line after.
* @param aToken token.
* @return if token have blank line after.
* @return true if token have empty line after.
*/
private boolean hasBlankLineAfter(DetailAST aToken)
private boolean hasEmptyLineAfter(DetailAST aToken)
{
DetailAST lastToken = aToken.getLastChild().getLastChild();
if (null == lastToken) {
@ -158,6 +167,19 @@ public class EmptyLineSeparatorCheck extends Check
return aToken.getNextSibling().getLineNo() - lastToken.getLineNo() > 1;
}
/**
* Checks if a token has a empty line before.
* @param aToken token.
* @return true, if token have empty line before.
*/
private boolean hasEmptyLineBefore(DetailAST aToken)
{
final int lineNo = aToken.getLineNo();
// [lineNo - 2] is the number of the previous line because the numbering starts from zero.
final String lineBefore = getLines()[lineNo - 2];
return lineBefore.isEmpty();
}
/**
* If variable definition is a type field.
* @param aVariableDef variable definition.

View File

@ -38,11 +38,20 @@ public class EmptyLineSeparatorCheckTest
public void testDefault() throws Exception
{
final String[] expected = {
"2: 'import' should be separated from previous statement.",
"16: 'CLASS_DEF' should be separated from previous statement.",
"19: 'VARIABLE_DEF' should be separated from previous statement.",
"58: 'INTERFACE_DEF' should be separated from previous statement.",
"20: 'import' should be separated from previous statement.",
"33: 'CLASS_DEF' should be separated from previous statement.",
"36: 'VARIABLE_DEF' should be separated from previous statement.",
"75: 'INTERFACE_DEF' should be separated from previous statement.",
};
verify(mCheckConfig, getPath("whitespace/InputEmptyLineSeparatorCheck.java"), expected);
}
@Test
public void testHeader() throws Exception
{
final String[] expected = {
"19: 'package' should be separated from previous statement.",
};
verify(mCheckConfig, getPath("whitespace/InputEmptyLineSeparatorCheckHeader.java"), expected);
}
}

View File

@ -1,4 +1,21 @@
package com.puppycrawl.tools.checkstyle.whitespace;
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -0,0 +1,21 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.whitespace;
class InputEmptyLineSeparatorCheck {}

View File

@ -1288,7 +1288,7 @@ import com.puppycrawl.tools.checkstyle.api.Check;
<section name="EmptyLineSeparator">
<subsection name="Description">
<p>
Checks for blank line separators after package, all import declarations,
Checks for empty line separators after header, package, all import declarations,
fields, constructors, methods, nested classes,
static initializers and instance initializers.
</p>
@ -1333,9 +1333,12 @@ import com.puppycrawl.tools.checkstyle.api.Check;
<subsection name="Examples">
<p>
Example of declarations without blank line separator:
Example of declarations without empty line separator:
</p>
<source>
///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.whitespace;
import java.io.Serializable;
class Foo
@ -1351,9 +1354,13 @@ class Foo
&lt;module name="EmptyLineSeparator"/&gt;
</source>
<p>
Example of declarations with blank line separator that is expected by the Check by default:
Example of declarations with empty line separator that is expected by the Check by default:
</p>
<source>
///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.whitespace;
import java.io.Serializable;
@ -1366,7 +1373,7 @@ class Foo
}
</source>
<p>
An example how to check blank line after <a
An example how to check empty line after <a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a> and <a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">METHOD_DEF</a>:
</p>