From b9f232ffe08853fbfd0239d53d683d4fe5c6b817 Mon Sep 17 00:00:00 2001 From: liscju Date: Fri, 6 Mar 2015 12:55:43 +0100 Subject: [PATCH] Fix bug with multiple imports, part of issue #706 --- .../whitespace/EmptyLineSeparatorCheck.java | 23 +++++++++++++++---- .../EmptyLineSeparatorCheckTest.java | 11 +++++++++ ...LineSeparatorMultipleImportEmptyClass.java | 8 +++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java 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 f26a17a88..f722799c6 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 @@ -243,8 +243,7 @@ public class EmptyLineSeparatorCheck extends Check nextToken.getText()); } } - if (!allowMultipleEmptyLines && isTypeField(ast) - && isPrePreviousLineEmpty(ast)) + if (isTypeField(ast) && hasNotAllowedTwoEmptyLinesBefore(ast)) { log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText()); } @@ -256,7 +255,7 @@ public class EmptyLineSeparatorCheck extends Check { log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText()); } - if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) { + if (hasNotAllowedTwoEmptyLinesBefore(ast)) { log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText()); } break; @@ -264,20 +263,31 @@ public class EmptyLineSeparatorCheck extends Check if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) { log(ast.getLineNo(), MSG_SHOULD_BE_SEPARATED, ast.getText()); } - if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) { + if (hasNotAllowedTwoEmptyLinesBefore(ast)) { log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText()); } default: if (nextToken.getType() != TokenTypes.RCURLY && !hasEmptyLineAfter(ast)) { log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText()); } - if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) { + if (hasNotAllowedTwoEmptyLinesBefore(ast)) { log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText()); } } } } + /** + * Checks if a token has empty two previous lines and multiple empty lines is not allowed + * @param token DetailAST token + * @return true, if token has empty two lines before and allowMultipleEmptyLines is false + */ + private boolean hasNotAllowedTwoEmptyLinesBefore(DetailAST token) + { + return !allowMultipleEmptyLines && hasEmptyLineBefore(token) + && isPrePreviousLineEmpty(token); + } + /** * Checks if a token has empty pre-previous line. * @param token DetailAST token. @@ -318,6 +328,9 @@ public class EmptyLineSeparatorCheck extends Check private boolean hasEmptyLineBefore(DetailAST token) { final int lineNo = token.getLineNo(); + if (lineNo == 1) { + return false; + } // [lineNo - 2] is the number of the previous line because the numbering starts from zero. final String lineBefore = getLines()[lineNo - 2]; return lineBefore.trim().isEmpty(); 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 93ab12b95..72bcb967a 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 @@ -110,4 +110,15 @@ public class EmptyLineSeparatorCheckTest verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorMultipleFieldsInClass.java"), expected); } + @Test + public void testAllowMultipleImportSeparatedFromPackage() throws Exception + { + DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class); + checkConfig.addAttribute("allowMultipleEmptyLines", "false"); + final String[] expected = { + + }; + verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java"), expected); + } + } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java new file mode 100644 index 000000000..c61bf3d2f --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java @@ -0,0 +1,8 @@ +package com.puppycrawl.tools.checkstyle.whitespace; + +import java.util.Calendar; +import java.util.Date; + +public class InputEmptyLineSeparatorMultipleImportEmptyClass +{ +}