From 751ed84e0d9d63aeb379284d1052de92ce171221 Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Thu, 5 Feb 2009 03:29:15 +0000 Subject: [PATCH] new rule in strictexception: AvoidThrowingNewInstanceOfSameException contributed by George Thomas: https://sourceforge.net/tracker/?func=detail&atid=479923&aid=2566369&group_id=56262 git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6814 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 3 + .../StrictExceptionRulesTest.java | 1 + ...voidThrowingNewInstanceOfSameException.xml | 139 ++++++++++++++++++ pmd/rulesets/strictexception.xml | 43 +++++- 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/strictexception/xml/AvoidThrowingNewInstanceOfSameException.xml diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 0fbbf7579a..38886976fe 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -10,6 +10,9 @@ Fixed bug 2404700 - UseSingleton should not act on enums Fixed bug 2225474 - VariableNamingConventions does not work with nonprimitives Fixed bug - JUnitTestsShouldIncludeAssert now detects Junit 4 Assert.assert... constructs +New rule: + StrictExceptions : AvoidThrowingNewInstanceOfSameException + October 12, 2008 - 4.2.4: Fixed bug 1481051 - false + UnusedNullCheckInEquals (and other false positives too) diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/StrictExceptionRulesTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/StrictExceptionRulesTest.java index 99a96c2385..bcca57f0bd 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/StrictExceptionRulesTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/StrictExceptionRulesTest.java @@ -11,6 +11,7 @@ public class StrictExceptionRulesTest extends SimpleAggregatorTst { addRule("strictexception", "AvoidCatchingNPE"); addRule("strictexception", "AvoidCatchingThrowable"); addRule("strictexception", "AvoidRethrowingException"); + addRule("strictexception", "AvoidThrowingNewInstanceOfSameException"); addRule("strictexception", "AvoidThrowingNullPointerException"); addRule("strictexception", "AvoidThrowingRawExceptionTypes"); addRule("strictexception", "DoNotExtendJavaLangError"); diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/xml/AvoidThrowingNewInstanceOfSameException.xml b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/xml/AvoidThrowingNewInstanceOfSameException.xml new file mode 100644 index 0000000000..05b9bf162e --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/xml/AvoidThrowingNewInstanceOfSameException.xml @@ -0,0 +1,139 @@ + + + + + 1 + + + + + 0 + + + + + 1 + + + + + 0 + + + + + 1 + + + + + 0 + + + + + 0 + + + + + 0 + + + + diff --git a/pmd/rulesets/strictexception.xml b/pmd/rulesets/strictexception.xml index e4a2b8b188..bdd0a772c9 100644 --- a/pmd/rulesets/strictexception.xml +++ b/pmd/rulesets/strictexception.xml @@ -287,7 +287,48 @@ public class Foo { } ]]> - + + + + Catch blocks that merely rethrow a caught exception wrapped inside a new instance of the same type only add to code size and runtime complexity. + + 3 + + + + + + + + + + +