applied patch 2829888 : New rule: Calling Thread.run() - thanks to Andy Throgmorton

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.3.x@7323 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse 2011-09-23 23:57:05 +00:00
parent 340e0f2583
commit 1668a79f03
4 changed files with 72 additions and 3 deletions

View File

@ -3,6 +3,9 @@
Add options --ignore-literals and --ignore-identifiers to the CPD command line task, thanks to Cd-Man
Fixed character reference in xml report - thanks to Seko
New Rule:
basic: DontCallThreadRun - thanks to Andy Throgmorton
September 14, 2011 - 4.2.6:
Fixed bug 2920057 - False + : CloseRessource whith an external getter
Fixed bug 1808110 - Fixed performance issue on PreserveStackTrace

View File

@ -45,6 +45,7 @@ public class BasicRulesTest extends SimpleAggregatorTst {
addRule("basic", "UnnecessaryConversionTemporary");
addRule("basic", "UselessOperationOnImmutable");
addRule("basic", "UselessOverridingMethod");
addRule("basic", "DontCallThreadRun");
}
public static junit.framework.Test suite() {

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
basic use case - calls to run()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void bar() {
Thread t = new Thread();
t.run(); // use t.start() instead
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
basic use case - call to Thread().run()
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void bar() {
new Thread().run();
}
}
]]></code>
</test-code>
</test-data>

View File

@ -1240,6 +1240,41 @@ public class Foo {
]]>
</example>
</rule>
<rule name="DontCallThreadRun"
since="4.3"
message="Don't call Thread.run() explicitly, use Thread.start()"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#DontCallThreadRun">
<description>
Explicitly calling Thread.run() method will execute in the caller's thread of control. Instead, call Thread.start() for the intended behavior.
</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//StatementExpression/PrimaryExpression
[
PrimaryPrefix
[
./Name[ends-with(@Image, '.run') or @Image = 'run']
and substring-before(Name/@Image, '.') =//VariableDeclarator/VariableDeclaratorId/@Image
[../../../Type/ReferenceType[ClassOrInterfaceType/@Image = 'Thread']]
or (
./AllocationExpression/ClassOrInterfaceType[@Image = 'Thread']
and ../PrimarySuffix[@Image = 'run'])
]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
Thread t = new Thread();
t.run(); // use t.start() instead
new Thread().run(); // same violation
]]>
</example>
</rule>
</ruleset>