Empty Line Separator Check, fixed Exception if file doesn't have header, issue #621

This commit is contained in:
alexkravin 2015-02-09 22:38:28 +04:00 committed by Roman Ivanov
parent 351d79582d
commit 7c262e61e6
3 changed files with 23 additions and 2 deletions

View File

@ -285,11 +285,15 @@ public class EmptyLineSeparatorCheck extends Check
*/
private boolean isPrePreviousLineEmpty(DetailAST token)
{
boolean result = false;
final int lineNo = token.getLineNo();
// 3 is the number of the pre-previous line because the numbering starts from zero.
final int number = 3;
final String prePreviousLine = getLines()[lineNo - number];
return prePreviousLine.trim().isEmpty();
if (lineNo >= number) {
final String prePreviousLine = getLines()[lineNo - number];
result = prePreviousLine.trim().isEmpty();
}
return result;
}
/**

View File

@ -81,4 +81,15 @@ public class EmptyLineSeparatorCheckTest
};
verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorCheckMultipleEmptyLines.java"), expected);
}
@Test
public void testFormerArrayIndexOutOfBounds() throws Exception
{
DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLines", "false");
final String[] expected = {
};
verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorFormerException.java"), expected);
}
}

View File

@ -0,0 +1,6 @@
package com.puppycrawl.tools.checkstyle.whitespace;
public class InputEmptyLineSeparatorFormerException
{
}