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

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7321 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse
2011-09-23 23:54:25 +00:00
parent ee738e5d89
commit 4d20daf2e3
4 changed files with 72 additions and 2 deletions

View File

@ -443,7 +443,7 @@ Fixed character reference in xml report - thanks to Seko
New Java rules:
Basic ruleset: ExtendsObject,CheckSkipResult,AvoidBranchingStatementAsLastInLoop
Basic ruleset: ExtendsObject,CheckSkipResult,AvoidBranchingStatementAsLastInLoop,DontCallThreadRun
Controversial ruleset: AvoidLiteralsInIfCondition, AvoidPrefixingMethodParameters, OneDeclarationPerLine, UseConcurrentHashMap
Coupling ruleset: LoosePackageCoupling,LawofDemeter
Design ruleset: LogicInversion,UseVarargs,FieldDeclarationsShouldBeAtStartOfClass,GodClass

View File

@ -8,7 +8,7 @@ import org.junit.Before;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class BasicRulesTest extends SimpleAggregatorTst {
private static final String RULESET = "java-basic";
@Before
@ -34,6 +34,7 @@ public class BasicRulesTest extends SimpleAggregatorTst {
addRule(RULESET, "MisplacedNullCheck");
addRule(RULESET, "OverrideBothEqualsAndHashcode");
addRule(RULESET, "ReturnFromFinallyBlock");
addRule(RULESET, "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

@ -736,6 +736,45 @@ for (int i = 0; i < 10; i++) {
</example>
</rule>
<rule name="DontCallThreadRun"
language="java"
since="4.3"
message="Don't call Thread.run() explicitly, use Thread.start()"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/java/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>
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyCatchBlock" />
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyIfStmt" />
<rule deprecated="true" ref="rulesets/java/empty.xml/EmptyWhileStmt" />