From 67da361a9a70df8baa4264b61c3ad5ea77e57dcc Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Thu, 21 Aug 2003 20:55:19 +0000 Subject: [PATCH] illegalClassNames property added to IllegalCatch check. --- docs/config_coding.html | 17 ++++++++++ docs/releasenotes.html | 6 ++-- .../checks/coding/IllegalCatchCheck.java | 33 ++++++++++++++----- .../checks/coding/IllegalCatchCheckTest.java | 18 +++++++++- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/docs/config_coding.html b/docs/config_coding.html index 8b35db278..1a600174f 100644 --- a/docs/config_coding.html +++ b/docs/config_coding.html @@ -1027,6 +1027,23 @@ return !valid(); leads to code that inadvertantly catchs NPE, OutOfMemoryErrors, etc.

+

Properties

+ + + + + + + + + + + + + +
namedescriptiontypedefault value
illegalClassNamesexception class names to rejectlist of strings"java.lang.Exception, + java.lang.Throwable, java.lang.RuntimeException"
+

Examples

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 @@

  • Added MethodLengthCheck option for counting empty and comment lines (requests 589358 and 654039).
  • -
  • Added a check for illegal token text +
  • Added a check for illegal token text (requests 740694 and 750633).
  • -
  • Added property to HiddenFieldCheck to ignore the parameter of +
  • Added property to HiddenFieldCheck to ignore the parameter of property setter methods (request 790456).
  • Added DescendantToken check (request 755021).
  • +
  • Added IllegalCatch check. (request 750746).
  • +

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