diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/MultilineDetector.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/MultilineDetector.java index 4a82734de..3786a6b72 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/MultilineDetector.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/MultilineDetector.java @@ -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. */ diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/regexp/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/regexp/messages.properties index fe3b3dc7a..e9d7c338d 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/regexp/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/regexp/messages.properties @@ -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. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java index 73858a399..d210063d1 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java @@ -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); + } }