diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java index 329853c92..a0060ce6b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java @@ -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; *

* *

- * Example of declarations without blank line separator: + * Example of declarations without empty line separator: *

* *
+ * ///////////////////////////////////////////////////
+ * //HEADER
+ * ///////////////////////////////////////////////////
  * package com.puppycrawl.tools.checkstyle.whitespace;
  * import java.io.Serializable;
  * class Foo
@@ -63,11 +66,15 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
  * 
* *

- * Example of declarations with blank line separator + * 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;
@@ -79,7 +86,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
  *     public void foo() {}
  * }
  * 
- *

An example how to check blank line after + *

An example how to check empty line after * {@link TokenTypes#VARIABLE_DEF VARIABLE_DEF} and * {@link TokenTypes#METHOD_DEF METHOD_DEF}: *

@@ -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. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheckTest.java index ba5bc05cd..844201c2a 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheckTest.java @@ -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); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheck.java b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheck.java index 1f5792619..642cf0058 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheck.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheck.java @@ -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; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheckHeader.java b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheckHeader.java new file mode 100644 index 000000000..532198293 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheckHeader.java @@ -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 {} diff --git a/src/xdocs/config_whitespace.xml b/src/xdocs/config_whitespace.xml index f5faa7321..6748762fd 100644 --- a/src/xdocs/config_whitespace.xml +++ b/src/xdocs/config_whitespace.xml @@ -1288,7 +1288,7 @@ import com.puppycrawl.tools.checkstyle.api.Check;

- 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.

@@ -1333,9 +1333,12 @@ import com.puppycrawl.tools.checkstyle.api.Check;

- Example of declarations without blank line separator: + Example of declarations without empty line separator:

+/////////////////////////////////////////////////// +//HEADER +/////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.whitespace; import java.io.Serializable; class Foo @@ -1351,9 +1354,13 @@ class Foo <module name="EmptyLineSeparator"/>

- 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:

+/////////////////////////////////////////////////// +//HEADER +/////////////////////////////////////////////////// + package com.puppycrawl.tools.checkstyle.whitespace; import java.io.Serializable; @@ -1366,7 +1373,7 @@ class Foo }

- An example how to check blank line after VARIABLE_DEF and METHOD_DEF: