Added property for GenericIllegalRegexp to customize reported message.

(module GenericIllegalRegexp, property message, request 738567)
This commit is contained in:
Oleg Sukhodolsky 2003-05-17 18:09:28 +00:00
parent 8fc92d312c
commit 896b671caa
4 changed files with 52 additions and 1 deletions

View File

@ -72,6 +72,13 @@
<td><a href="property_types.html#regexp">regular expression</a></td>
<td><span class="default">^$</span> (empty)</td>
</tr>
<tr>
<td>message</td>
<td>message which is used to notify about violations,
if empty then default(hard-codded) message is used.</td>
<td><a href="property_types.html#String">String</a></td>
<td><span class="default">&quot;&quot;</span>(empty)</td>
</tr>
</table>
<h4>Examples</h4>
<p class="body">

View File

@ -132,6 +132,10 @@
<li class="body">Added check for redundant exceptions in
throws clause (module RedundantThrows, request 579056)</li>
<li class="body">Added property for GenericIllegalRegexp to
customize reported message (module GenericIllegalRegexp,
property message, request 738567)</li>
</ul>
<p class="body">

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}