diff options
author | rnveach <rveach02@gmail.com> | 2015-12-17 16:59:15 -0500 |
---|---|---|
committer | Roman Ivanov <ivanov-jr@mail.ru> | 2016-01-30 15:19:29 -0800 |
commit | cce5bcbcb0c3473f5f13e0ebc14d7500d9f9c253 (patch) | |
tree | b28d4b0ec5a0a98fd856f3293a4bb326d2b01bc7 | |
parent | cbe324997364777dc88310f636c028702d88fe3f (diff) | |
download | checkstyle-cce5bcbcb0c3473f5f13e0ebc14d7500d9f9c253.tar.gz |
Issue #2645: fixed StringIndexOutOfBoundsException in Indentation check
5 files changed, 52 insertions, 13 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java index c3c376989..ad70a26ad 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java @@ -228,7 +228,7 @@ public abstract class AbstractExpressionHandler { */ protected final int getLineStart(String line) { int index = 0; - while (Character.isWhitespace(line.charAt(index))) { + while (index < line.length() && Character.isWhitespace(line.charAt(index))) { index++; } return CommonUtils.lengthExpandedTabs( @@ -328,9 +328,11 @@ public abstract class AbstractExpressionHandler { */ private void checkLineIndent(int lineNum, IndentLevel indentLevel) { final String line = indentCheck.getLine(lineNum - 1); - final int start = getLineStart(line); - if (indentLevel.isGreaterThan(start)) { - logChildError(lineNum, start, indentLevel); + if (!line.isEmpty()) { + final int start = getLineStart(line); + if (indentLevel.isGreaterThan(start)) { + logChildError(lineNum, start, indentLevel); + } } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index 0c123d577..ea9b522c9 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -52,12 +52,9 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils; * @author jrichard */ public class IndentationCheckTest extends BaseCheckTestSupport { - private static final Pattern NONEMPTY_LINE_REGEX = - Pattern.compile(".*?\\S+.*?"); - private static final Pattern LINE_WITH_COMMENT_REGEX = - Pattern.compile(".*?\\S+.*?(//indent:(\\d+)" - + " exp:((>=\\d+)|(\\d+(,\\d+)*?))( warn)?)"); + Pattern.compile(".*?(//indent:(\\d+)" + + " exp:((>=\\d+)|(\\d+(,\\d+)*?))( warn)?)$"); private static final Pattern GET_INDENT_FROM_COMMENT_REGEX = Pattern.compile("//indent:(\\d+).*?"); @@ -108,7 +105,7 @@ public class IndentationCheckTest extends BaseCheckTestSupport { lineNumber)); } } - else if (NONEMPTY_LINE_REGEX.matcher(line).matches()) { + else if (!line.isEmpty()) { throw new IllegalStateException(String.format(Locale.ROOT, "File \"%1$s\" has no indentation comment or its format " + "malformed. Error on line: %2$d", @@ -1639,4 +1636,22 @@ public class IndentationCheckTest extends BaseCheckTestSupport { final String[] expected = EMPTY_EXPECTED; verifyWarns(checkConfig, getNonCompilablePath("InputLambda2.java"), expected, 0); } + + @Test + public void testSeparatedStatements() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + final String fileName = getPath("InputSeparatedStatements.java"); + final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; + verifyWarns(checkConfig, fileName, expected); + } + + @Test + public void testSeparatedLineWithJustSpaces() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + final String fileName = getPath("InputSeparatedStatementWithSpaces.java"); + final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; + verify(checkConfig, fileName, expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFromGuava.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFromGuava.java index 9bd30ac1d..f278b5031 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFromGuava.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputFromGuava.java @@ -216,7 +216,7 @@ class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> { //indent:2 exp:2 return null; //indent:4 exp:4 } //indent:2 exp:2 - + private static class ValueReference<T1, T2> { //indent:2 exp:2 } //indent:2 exp:2 @@ -239,7 +239,7 @@ class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> } //indent:4 exp:4 } //indent:2 exp:2 - + private static class StrongValueReference<T1, T2> { //indent:2 exp:2 public StrongValueReference(int value) //indent:4 exp:4 @@ -248,7 +248,7 @@ class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> } //indent:4 exp:4 } //indent:2 exp:2 - + private static class WeightedStrongValueReference<T1, T2> { //indent:2 exp:2 public WeightedStrongValueReference(int value, int weight) //indent:4 exp:4 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatementWithSpaces.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatementWithSpaces.java new file mode 100644 index 000000000..e169ad720 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatementWithSpaces.java @@ -0,0 +1,10 @@ +// test file has no expected comments to test out blank line with just spaces +package com.puppycrawl.tools.checkstyle.checks.indentation; + +import java.util.* + // next line should be empty with just spaces, indented correctly + + ; + +public class InputSeparatedStatementWithSpaces { +}
\ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatements.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatements.java new file mode 100644 index 000000000..1889cd51d --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputSeparatedStatements.java @@ -0,0 +1,12 @@ +package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:0 exp:0 + +import java.io.*//indent:0 exp:0 + + ;//indent:4 exp:4 + +import java.util.*//indent:0 exp:0 + //indent:4 exp:4 + ;//indent:4 exp:4 + +public class InputSeparatedStatements {//indent:0 exp:0 +}//indent:0 exp:0 |