diff --git a/docs/config_misc.html b/docs/config_misc.html index d21229f0d..9aeb8816b 100644 --- a/docs/config_misc.html +++ b/docs/config_misc.html @@ -72,6 +72,13 @@ regular expression ^$ (empty) + + message + message which is used to notify about violations, + if empty then default(hard-codded) message is used. + String + ""(empty) +

Examples

diff --git a/docs/releasenotes.html b/docs/releasenotes.html index d35241574..a3eee791c 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -132,6 +132,10 @@

  • Added check for redundant exceptions in throws clause (module RedundantThrows, request 579056)
  • +
  • Added property for GenericIllegalRegexp to + customize reported message (module GenericIllegalRegexp, + property message, request 738567)
  • +

    diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheck.java index 24a17603f..8b3ae510c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheck.java @@ -42,6 +42,26 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; */ public class GenericIllegalRegexpCheck extends AbstractFormatCheck { + /** + * Custom message for report if illegal regexp found + * ignored if empty. + */ + private String mMessage = ""; + + /** + * Setter for message property + * @param aMessage custom message which should be used + * to report about violations. + */ + + public void setMessage(String aMessage) + { + if (aMessage == null) { + aMessage = ""; + } + mMessage = aMessage; + } + /** * Instantiates an new GenericIllegalRegexpCheck. */ @@ -64,7 +84,12 @@ public class GenericIllegalRegexpCheck extends AbstractFormatCheck final String line = lines[i]; if (getRegexp().match(line)) { - log(i + 1, "illegal.regexp", getFormat()); + if ("".equals(mMessage)) { + log(i + 1, "illegal.regexp", getFormat()); + } + else { + log(i + 1, mMessage); + } } } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheckTest.java index 5a25c9f8d..4997159d3 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/GenericIllegalRegexpCheckTest.java @@ -18,4 +18,19 @@ public class GenericIllegalRegexpCheckTest }; verify(checkConfig, getPath("InputSemantic.java"), expected); } + + public void testMessageProperty() + throws Exception + { + final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; + final String message = "Bad line :("; + final DefaultConfiguration checkConfig = + createCheckConfig(GenericIllegalRegexpCheck.class); + checkConfig.addAttribute("format", illegal); + checkConfig.addAttribute("message", message); + final String[] expected = { + "69: " + message, + }; + verify(checkConfig, getPath("InputSemantic.java"), expected); + } }