Added AvoidLosingExceptionInformation from George Thomas.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6972 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson 2009-07-21 01:43:49 +00:00
parent 0e3a92c291
commit d6b326838f
5 changed files with 146 additions and 2 deletions

View File

@ -15,7 +15,7 @@ Android ruleset: CallSuperLast rule now also checks for finish() redefinitions
New rule:
Android: DoNotHardCodeSDCard
Controversial : AvoidLiteralsInIfCondition (patch 2591627)
StrictExceptions : AvoidCatchingGenericException
StrictExceptions : AvoidCatchingGenericException, AvoidLosingExceptionInformation
February 08, 2009 - 4.2.5:

View File

@ -11,6 +11,7 @@ public class StrictExceptionRulesTest extends SimpleAggregatorTst {
addRule("strictexception", "AvoidCatchingGenericException");
addRule("strictexception", "AvoidCatchingNPE");
addRule("strictexception", "AvoidCatchingThrowable");
addRule("strictexception", "AvoidLosingExceptionInformation");
addRule("strictexception", "AvoidRethrowingException");
addRule("strictexception", "AvoidThrowingNewInstanceOfSameException");
addRule("strictexception", "AvoidThrowingNullPointerException");

View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
basic failure case
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
} catch (SomeException se) {
se.getMessage();
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
fetch the value returned by getMessage(), ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
} catch (SomeException se) {
String msg = se.getMessage();
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
fetch the value returned by getCause(), ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
} catch (SomeException se) {
Throwable t = se.getCause();
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
a mix of guilty calls to getMessage(), getLocalizedMessage(), getCause() or getStackTrace(), failure
]]></description>
<expected-problems>2</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
// do something
} catch (SomeException se) {
se.printStackTrace();
System.out.println("boo");
long l = 1L + 4L;
se.getMessage();
int a = 1;
se.getMessage();
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
a larger mix of guilty calls to getMessage(), getLocalizedMessage(), getCause(), getStackTrace() or toString(), failure
]]></description>
<expected-problems>5</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {
// do something
} catch (SomeException se) {
se.printStackTrace();
System.out.println("boo");
int i = 1;
se.getLocalizedMessage();
long l = 1L + 4L;
se.getMessage();
int a = 1;
se.toString();
se.getMessage();
System.out.println("far");
se.getStackTrace();
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -373,5 +373,48 @@ public class PrimitiveType {
</example>
</rule>
<rule name="AvoidLosingExceptionInformation"
since="4.2.6"
message="Avoid statements in a catch block that invoke accessors on the exception without using the information"
externalInfoUrl="http://pmd.sourceforge.net/rules/strictexception.html#AvoidLosingExceptionInformation"
class="net.sourceforge.pmd.rules.XPathRule">
<description>Statements in a catch block that invoke accessors on the exception without using the information
only add to code size. Either remove the invocation, or use the return result.</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CatchStatement/Block/BlockStatement/Statement/StatementExpression/PrimaryExpression/PrimaryPrefix/Name
[
@Image = concat(../../../../../../../FormalParameter/VariableDeclaratorId/@Image, '.getMessage')
or
@Image = concat(../../../../../../../FormalParameter/VariableDeclaratorId/@Image, '.getLocalizedMessage')
or
@Image = concat(../../../../../../../FormalParameter/VariableDeclaratorId/@Image, '.getCause')
or
@Image = concat(../../../../../../../FormalParameter/VariableDeclaratorId/@Image, '.getStackTrace')
or
@Image = concat(../../../../../../../FormalParameter/VariableDeclaratorId/@Image, '.toString')
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar() {
try {
// do something
} catch (SomeException se) {
se.getMessage();
}
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -86,7 +86,7 @@
<li>Peter Van de Voorde - Rewrote the 'create rule XML' functionality in the designer utility</li>
<li>Josh Devins - Reported bug with annotation parsing</li>
<li>Alan Berg - Reported bug in Ant task</li>
<li>George Thomas - Wrote AvoidRethrowingException rule</li>
<li>George Thomas - Wrote AvoidRethrowingException rule, new AvoidLosingExceptionInformation rule</li>
<li>Robert Simmons - Reported bug in optimizations package along with suggestions for fix</li>
<li>Brian Remedios - display cleanup of CPD GUI, code cleanup of StringUtil and various rules, cleanup of rule designer, code cleanup of net.sourceforge.pmd.ant.Formatter.java, code improvements to Eclipse plugin, created AbstractPoorMethodCall and refactored UseIndexOfChar</li>
<li>Max Tardiveau - A nice XML to HTML stylesheet for CPD.</li>