illegalClassNames property added to IllegalCatch check.

This commit is contained in:
Oleg Sukhodolsky 2003-08-21 20:55:19 +00:00
parent 9aa9d75107
commit 67da361a9a
4 changed files with 63 additions and 11 deletions

View File

@ -1027,6 +1027,23 @@ return !valid();
leads to code that inadvertantly catchs NPE, OutOfMemoryErrors,
etc.
</p>
<h4>Properties</h4>
<table width="100%" border="1" cellpadding="5" class="body">
<tr class="header">
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
</tr>
<tr>
<td>illegalClassNames</td>
<td>exception class names to reject</td>
<td><a href="property_types.html#stringSet">list of strings</a></td>
<td><span class="default">&quot;java.lang.Exception,
java.lang.Throwable, java.lang.RuntimeException&quot;</span></td>
</tr>
</table>
<h4>Examples</h4>
<p class="body">
To configure the check:

View File

@ -133,14 +133,16 @@
<li class="body">Added MethodLengthCheck option for counting empty and comment lines
(requests 589358 and 654039).</li>
<li class="body">Added a check for illegal token text
<li class="body">Added a check for illegal token text
(requests 740694 and 750633).</li>
<li class="body">Added property to HiddenFieldCheck to ignore the parameter of
<li class="body">Added property to HiddenFieldCheck to ignore the parameter of
property setter methods (request 790456).</li>
<li class="body">Added DescendantToken check (request 755021).</li>
<li class="body">Added IllegalCatch check. (request 750746).</li>
</ul>
<p class="body">

View File

@ -41,14 +41,13 @@ public final class IllegalCatchCheck extends Check
/** Creates new instance of the check. */
public IllegalCatchCheck()
{
mIllegalClassNames.add("Exception");
mIllegalClassNames.add("Error");
mIllegalClassNames.add("RuntimeException");
mIllegalClassNames.add("Throwable");
mIllegalClassNames.add("java.lang.Error");
mIllegalClassNames.add("java.lang.Exception");
mIllegalClassNames.add("java.lang.RuntimeException");
mIllegalClassNames.add("java.lang.Throwable");
setIllegalClassNames(new String[] {"Exception", "Error",
"RuntimeException", "Throwable",
"java.lang.Error",
"java.lang.Exception",
"java.lang.RuntimeException",
"java.lang.Throwable",
});
}
/** @see Check */
@ -87,4 +86,22 @@ public final class IllegalCatchCheck extends Check
{
return mIllegalClassNames.contains(aIdent);
}
/**
* Set the list of illegal exception classes.
* @param aClassNames array of illegal exception classes
*/
public void setIllegalClassNames(String[] aClassNames)
{
mIllegalClassNames.clear();
for (int i = 0; i < aClassNames.length; i++) {
String name = aClassNames[i];
mIllegalClassNames.add(name);
int lastDot = name.lastIndexOf(".");
if (lastDot > 0 && lastDot < (name.length() - 1)) {
String shortName = name.substring(name.lastIndexOf(".") + 1);
mIllegalClassNames.add(shortName);
}
}
}
}

View File

@ -7,7 +7,7 @@ import java.io.File;
public class IllegalCatchCheckTest extends BaseCheckTestCase
{
public void test() throws Exception
public void testDefault() throws Exception
{
DefaultConfiguration checkConfig = createCheckConfig(IllegalCatchCheck.class);
@ -22,4 +22,20 @@ public class IllegalCatchCheckTest extends BaseCheckTestCase
verify(checkConfig, getPath("coding" + File.separator + "InputIllegalCatchCheck.java"), expected);
}
public void testIllegalClassNames() throws Exception
{
DefaultConfiguration checkConfig = createCheckConfig(IllegalCatchCheck.class);
checkConfig.addAttribute("illegalClassNames",
"java.lang.Error, java.lang.Exception, java.lang.Throwable");
String[] expected = {
"7:11: Catching 'Exception' is not allowed.",
"8:11: Catching 'Throwable' is not allowed.",
"15:11: Catching 'java.lang.Exception' is not allowed.",
"16:11: Catching 'java.lang.Throwable' is not allowed.",
};
verify(checkConfig, getPath("coding" + File.separator + "InputIllegalCatchCheck.java"), expected);
}
}