Incorporated patch from Bill Schneider to add property to

GenericIllegalRegexpCheck for case-insensitive matches
(request 740112).
This commit is contained in:
Rick Giles 2003-05-21 13:39:23 +00:00
parent cb5ab77359
commit 1e67ec5577
4 changed files with 93 additions and 2 deletions

View File

@ -72,10 +72,16 @@
<td><a href="property_types.html#regexp">regular expression</a></td>
<td><span class="default">^$</span> (empty)</td>
</tr>
<tr>
<td>ignoreCase</td>
<td>Controls whether to ignore case when searching.</td>
<td><a href="property_types.html#boolean">Boolean</a></td>
<td><span class="default">false</span></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>
if empty then default(hard-coded) message is used.</td>
<td><a href="property_types.html#String">String</a></td>
<td><span class="default">&quot;&quot;</span>(empty)</td>
</tr>
@ -98,6 +104,15 @@
&lt;module name=&quot;GenericIllegalRegexp&quot;&gt;
&lt;!-- \s matches whitespace character, $ matches end of line. --&gt;
&lt;property name=&quot;format&quot; value=&quot;\s$&quot;/&gt;
&lt;/module&gt;
</pre>
<p class="body">
To configure the check to find case-insensitive occurrences of &quot;debug&quot;:
</p>
<pre class="body">
&lt;module name=&quot;GenericIllegalRegexp&quot;&gt;
&lt;property name=&quot;format&quot; value=&quot;debug&quot;/&gt;
&lt;property name=&quot;ignoreCase&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</pre>
<h4>Package</h4>
@ -349,7 +364,7 @@ like <span class="code">1</span>.
<!-- --> <a name="FinalParameters"></a> <h2>FinalParameters</h2> <h4>Description</h4>
<p class="body">
Check that method/constructor parameters are final. Interface methods are not checked -
the final keyword does not make sense for interface mathod parameters as there is no code
the final keyword does not make sense for interface method parameters as there is no code
that could modify the parameter.
</p>
<p class="body">

View File

@ -145,6 +145,10 @@
a Reload Java File button and storage of the current directory
of the last opened file (request 740545).</li>
<li class="body">Patch from Bill Schneider to add
GenericIllegalRegexpCheck property to perform
case-insensitive matches (request 740112).</li>
</ul>
<p class="body">

View File

@ -18,6 +18,8 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks;
import org.apache.regexp.RE;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
/**
@ -39,6 +41,7 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
* &lt;/module&gt;
* </pre>
* @author lkuehne
* @author <a href="mailto:bschneider@vecna.com">Bill Schneider</a>
*/
public class GenericIllegalRegexpCheck extends AbstractFormatCheck
{
@ -48,6 +51,9 @@ public class GenericIllegalRegexpCheck extends AbstractFormatCheck
*/
private String mMessage = "";
/** case insensitive? **/
private boolean mIgnoreCase = false;
/**
* Setter for message property
* @param aMessage custom message which should be used
@ -62,6 +68,15 @@ public class GenericIllegalRegexpCheck extends AbstractFormatCheck
mMessage = aMessage;
}
/**
* Set whether or not the match is case sensitive.
* @param aCaseInsensitive true if the match is case insensitive.
*/
public void setIgnoreCase(boolean aCaseInsensitive)
{
mIgnoreCase = aCaseInsensitive;
}
/**
* Instantiates an new GenericIllegalRegexpCheck.
*/
@ -93,4 +108,26 @@ public class GenericIllegalRegexpCheck extends AbstractFormatCheck
}
}
}
/** @return the regexp to match against */
public RE getRegexp()
{
final RE regexp = super.getRegexp();
if (mIgnoreCase) {
regexp.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
}
// Without the else, in a sequence of GenericIllegalRegexpCheck's,
// the first with mIgnoreCase true, the second with mIgnoreCase
// false, the second still has match behaviour flags as
// RE.MATCH_CASEINDEPENDENT.
// This happens with GenericIllegalRegexpCheckTest method
// testIgnoreCaseFalse(), for example.
// TODO: Check whether this a bug or known feature of
// org.apache.regexp.RE.
else {
regexp.setMatchFlags(RE.MATCH_NORMAL);
}
return regexp;
}
}

View File

@ -33,4 +33,39 @@ public class GenericIllegalRegexpCheckTest
};
verify(checkConfig, getPath("InputSemantic.java"), expected);
}
public void testIgnoreCaseTrue()
throws Exception
{
final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
final DefaultConfiguration checkConfig =
createCheckConfig(GenericIllegalRegexpCheck.class);
checkConfig.addAttribute("format", illegal);
checkConfig.addAttribute("ignoreCase", "true");
final String[] expected = {
"69: Line matches the illegal pattern '" + illegal + "'."
};
verify(checkConfig, getPath("InputSemantic.java"), expected);
}
public void testIgnoreCaseFalse()
throws Exception
{
final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
final DefaultConfiguration checkConfigTrue =
createCheckConfig(GenericIllegalRegexpCheck.class);
checkConfigTrue.addAttribute("format", illegal);
checkConfigTrue.addAttribute("ignoreCase", "true");
final String[] expectedTrue = {
"69: Line matches the illegal pattern '" + illegal + "'."};
verify(checkConfigTrue, getPath("InputSemantic.java"), expectedTrue);
final DefaultConfiguration checkConfigFalse =
createCheckConfig(GenericIllegalRegexpCheck.class);
checkConfigFalse.addAttribute("format", illegal);
checkConfigFalse.addAttribute("ignoreCase", "false");
final String[] expectedFalse = {};
verify(checkConfigFalse, getPath("InputSemantic.java"), expectedFalse);
}
}