From 67da361a9a70df8baa4264b61c3ad5ea77e57dcc Mon Sep 17 00:00:00 2001
From: Oleg Sukhodolsky
| name | +description | +type | +default value | +
|---|---|---|---|
| illegalClassNames | +exception class names to reject | +list of strings | +"java.lang.Exception, + java.lang.Throwable, java.lang.RuntimeException" | +
To configure the check: diff --git a/docs/releasenotes.html b/docs/releasenotes.html index 2c207796e..209d98c8d 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -133,14 +133,16 @@
diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java index 220786627..eff31eaf3 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java @@ -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); + } + } + } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java index e55aa6696..3fd26c64a 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java @@ -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); + } }