NullPointerException at java.util.regex.Pattern, fixes #1224; from

com.puppycrawl.tools.checkstyle.checks.regexp.DetectorOptions.getPattern
This commit is contained in:
Michael Vorburger 2015-06-18 22:40:10 +02:00 committed by Roman Ivanov
parent cf8a2787c1
commit edc63f0d66
3 changed files with 29 additions and 3 deletions

View File

@ -20,6 +20,8 @@
package com.puppycrawl.tools.checkstyle.checks.regexp;
import java.util.regex.Matcher;
import com.google.common.base.Strings;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.LineColumn;
@ -41,6 +43,12 @@ class MultilineDetector {
*/
public static final String REGEXP_MINIMUM = "regexp.minimum";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String EMPTY = "regexp.empty";
/** The detection options to use. */
private final DetectorOptions options;
/** Tracks the number of matches. */
@ -65,9 +73,15 @@ class MultilineDetector {
public void processLines(FileText text) {
this.text = text;
resetState();
matcher = options.getPattern().matcher(text.getFullText());
findMatch();
finish();
if (!Strings.isNullOrEmpty(options.getFormat())) {
matcher = options.getPattern().matcher(text.getFullText());
findMatch();
finish();
}
else {
options.getReporter().log(0, EMPTY);
}
}
/** recursive method that finds the matches. */

View File

@ -4,3 +4,5 @@ regexp.minimum=File does not contain at least {0} matches for pattern ''{1}''.
illegal.regexp=Line matches the illegal pattern ''{0}''.
required.regexp=Required pattern ''{0}'' missing in file.
duplicate.regexp=Found duplicate pattern ''{0}''.
regexp.empty=Empty (null) pattern.

View File

@ -31,6 +31,7 @@ import org.junit.rules.TemporaryFolder;
import java.io.File;
import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.REGEXP_EXCEEDED;
import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.EMPTY;
public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport {
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
@ -119,4 +120,13 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport {
};
verify(checkConfig, getPath("InputSemantic.java"), expected);
}
@Test
public void testEmptyFormat() throws Exception {
checkConfig.addAttribute("format", null);
final String[] expected = {
"0: " + getCheckMessage(EMPTY),
};
verify(checkConfig, getPath("InputSemantic.java"), expected);
}
}