Adding a new rule 'UseConcurrentHashMap' to promote use of ConcurrentHashMap implementation in Java 5

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6999 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse
2009-11-14 15:49:41 +00:00
parent ba902fe1ed
commit 33f33e6e95
3 changed files with 56 additions and 1 deletions

View File

@ -31,7 +31,8 @@ public class ControversialRulesTest extends SimpleAggregatorTst {
addRule(RULESET, "SuspiciousOctalEscape");
addRule(RULESET, "UnnecessaryConstructor");
addRule(RULESET, "UnnecessaryParentheses");
addRule(RULESET, "UseObjectForClearerAPI");
addRule(RULESET,"UseConcurrentHashMap");
addRule(RULESET, "UseObjectForClearerAPI");
}
public static junit.framework.Test suite() {

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
Basic use case
]]></description>
<expected-problems>2</expected-problems>
<code><![CDATA[
public class Foo {
private Map _map = new TreeMap();
public void m() {
Map m = new HashMap();
}
public Map contructMap() {
return new HashMap(); //not detected
}
}
]]></code>
</test-code>
</test-data>

View File

@ -821,5 +821,37 @@ public class MyClass {
]]>
</example>
</rule>
<rule name="UseConcurrentHashMap"
language="java"
since="4.2.6"
message="If you run in Java5 and have concurrent access, you should use ConcurrentHashMap implementation"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/java/controversial.html#UseConcurrentHashMap">
<description>
Since Java5 brought a new implementation of the Map interface, specially designed for concurrent application.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Type[../VariableDeclarator/VariableInitializer]/ReferenceType/ClassOrInterfaceType[@Image = 'Map']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class ConcurrentApp {
public void getMyInstance() {
Map map = new HashMap();
}
}
]]>
</example>
</rule>
</ruleset>