diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index af7c67b56b..dad093949c 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -4,6 +4,7 @@ BEFORE RELEASE: New rules: Basic ruleset: BrokenNullCheck + Strict exceptions ruleset: AvoidRethrowingException Fixed bug 1498910 - AssignmentInOperand no longer has a typo in the message. Fixed bug 1498960 - DontImportJavaLang no longer reports static imports of java.lang members. Fixed bug 1417106 - MissingBreakInSwitch no longer flags stmts where every case has either a return or a throw. diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/AvoidRethrowingExceptionTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/AvoidRethrowingExceptionTest.java new file mode 100644 index 0000000000..96b2137959 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strictexception/AvoidRethrowingExceptionTest.java @@ -0,0 +1,55 @@ +package test.net.sourceforge.pmd.rules.strictexception; + +import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleSetNotFoundException; +import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; +import test.net.sourceforge.pmd.testframework.TestDescriptor; + +/** + * Tests the AvoidRethrowingException rule. + * + * @author George Thomas + */ +public class AvoidRethrowingExceptionTest extends SimpleAggregatorTst { + + private Rule rule; + + /** + * @see SimpleAggregatorTst#setUp() + */ + public void setUp() throws RuleSetNotFoundException { + rule = findRule("strictexception", "AvoidRethrowingException"); + } + + /** + * @see SimpleAggregatorTst#testAll() + */ + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(FAILURE_TEST, "failure case", 1, rule), + new TestDescriptor(OK_TEST, "doing something else before throwing it, ok", 0, rule), + }); + } + + private static final String FAILURE_TEST = + "public class Foo {" + PMD.EOL + + " void bar() {" + PMD.EOL + + " try {" + PMD.EOL + + " } catch (SomeException se) {" + PMD.EOL + + " throw se;" + PMD.EOL + + " }" + PMD.EOL + + " }" + PMD.EOL + + "} " + PMD.EOL; + + private static final String OK_TEST = + "public class Foo {" + PMD.EOL + + " void bar() {" + PMD.EOL + + " try {" + PMD.EOL + + " } catch (SomeException se) {" + PMD.EOL + + " System.out.println(\"something interesting\");" + PMD.EOL + + " throw se;" + PMD.EOL + + " }" + PMD.EOL + + " }" + PMD.EOL + + "} " + PMD.EOL; +} diff --git a/pmd/rulesets/releases/38.xml b/pmd/rulesets/releases/38.xml index d9ce26f4c1..6f7423a1aa 100644 --- a/pmd/rulesets/releases/38.xml +++ b/pmd/rulesets/releases/38.xml @@ -7,5 +7,6 @@ This ruleset contains links to rules that are new in PMD v3.8 + diff --git a/pmd/rulesets/scratchpad.xml b/pmd/rulesets/scratchpad.xml index e5f2f9dc30..8322e66fa8 100644 --- a/pmd/rulesets/scratchpad.xml +++ b/pmd/rulesets/scratchpad.xml @@ -10,52 +10,40 @@ These are new rules that are still in progress - - -The null check is broken since it will throw a Nullpointer itself. -The reason is that a method is called on the object when it is null. -It is likely that you used || instead of && or vice versa. - - - - 1]//PrimaryPrefix/Name/@Image, -concat(PrimaryExpression//PrimaryPrefix/Name/@Image, '.'))] -or -.//ConditionalAndExpression/EqualityExpression[@Image='=='] -[starts-with(../*[position()>1]//PrimaryPrefix/Name/@Image, -concat(PrimaryExpression//PrimaryPrefix/Name/@Image, '.'))] -] - ]]> - - - - 2 - - - + + + Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity. + + + + 3 + + + + + + + - -