RFE 1562230 - New Rules as IntegerInstantiation for Byte, Short, Long

Request had recomended XPath which worked very well.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5027 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-02-01 02:10:53 +00:00
parent e95199385f
commit 30063d0b33
11 changed files with 241 additions and 0 deletions

View File

@ -9,6 +9,7 @@ Fixed bug 1631646 - UselessOperationOnImmutable doesn't throw on variable.method
Fixed bug 1627830 - UseLocaleWithCaseConversions now works with compound string operations
Fixed bug 1613807 - DontImportJavaLang rule allows import to Thread inner classes
Applied patch 1612455 - RFE 1411022 CompareObjectsWithEquals now catches the case where comparison is against new Object
Implemented RFE 1562230 - Added migration rule to check for instantiation of Short/Byte/Long
Implemented RFE 1627581 - SuppressWarnings("unused") now suppresses all warnings in unusedcode.xml
XPath rules are now chained together for an extra speedup in processing
PMD now requires JDK 1.5 to be compiled. Java 1.4 support is provided using Retroweaver

View File

@ -0,0 +1,17 @@
package test.net.sourceforge.pmd.rules.migrating;
import net.sourceforge.pmd.Rule;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class ByteInstantiationTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() {
rule = findRule("migrating", "ByteInstantiation");
}
public void testAll() {
runTests(rule);
}
}

View File

@ -0,0 +1,17 @@
package test.net.sourceforge.pmd.rules.migrating;
import net.sourceforge.pmd.Rule;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class LongInstantiationTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() {
rule = findRule("migrating", "LongInstantiation");
}
public void testAll() {
runTests(rule);
}
}

View File

@ -0,0 +1,17 @@
package test.net.sourceforge.pmd.rules.migrating;
import net.sourceforge.pmd.Rule;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class ShortInstantiationTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() {
rule = findRule("migrating", "ShortInstantiation");
}
public void testAll() {
runTests(rule);
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
new Byte(), bad
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void f(byte b){
Byte i = new Byte(b);
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Byte.valueOf(), ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void f(byte b){
Byte i = Byte.valueOf(b);
}
}
]]></code>
</test-code>
</test-data>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
new Long(), bad
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void f(long l){
Long i = new Long(l);
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Long.valueOf(), ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void f(long l){
Long i = Long.valueOf(l);
}
}
]]></code>
</test-code>
</test-data>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
new Short(), bad
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public void f(short b){
Short i = new Short(b);
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Short.valueOf(), ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void f(short b){
Short i = Short.valueOf(b);
}
}
]]></code>
</test-code>
</test-data>

View File

@ -177,6 +177,91 @@ public class Foo {
]]>
</example>
</rule>
<rule name="ByteInstantiation"
message="Avoid instantiating Byte objects. Call Byte.valueOf() instead"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
In JDK 1.5, calling new Byte() causes memory allocation. Byte.valueOf() is more memory friendly.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix/AllocationExpression
[not (ArrayDimsAndInits)
and (ClassOrInterfaceType/@Image='Byte'
or ClassOrInterfaceType/@Image='java.lang.Byte')]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
private Byte i = new Byte(0); // change to Byte i =
Byte.valueOf(0);
}
]]>
</example>
</rule>
<rule name="ShortInstantiation"
message="Avoid instantiating Short objects. Call Short.valueOf() instead"
class="net.sourceforge.pmd.rules.XPathRule">
<description>In JDK 1.5, calling new Short() causes memory allocation. Short.valueOf() is more memory friendly.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix
/AllocationExpression
[not (ArrayDimsAndInits)
and (ClassOrInterfaceType/@Image='Short'
or ClassOrInterfaceType/@Image='java.lang.Short')]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
private Short i = new Short(0); // change to Short i =
Short.valueOf(0);
}
]]>
</example>
</rule>
<rule name="LongInstantiation"
message="Avoid instantiating Long objects.Call Long.valueOf() instead"
class="net.sourceforge.pmd.rules.XPathRule">
<description>In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix
/AllocationExpression
[not (ArrayDimsAndInits)
and (ClassOrInterfaceType/@Image='Long'
or ClassOrInterfaceType/@Image='java.lang.Long')]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
private Long i = new Long(0); // change to Long i =
Long.valueOf(0);
}
]]>
</example>
</rule>
</ruleset>

View File

@ -11,6 +11,9 @@ Contains rules for migrating to JDK 1.5
<rule ref="rulesets/migrating.xml/AvoidEnumAsIdentifier"/>
<rule ref="rulesets/migrating.xml/IntegerInstantiation"/>
<rule ref="rulesets/migrating.xml/LongInstantiation"/>
<rule ref="rulesets/migrating.xml/ShortInstantiation"/>
<rule ref="rulesets/migrating.xml/ByteInstantiation"/>
</ruleset>

View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<ruleset name="40" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
This ruleset contains links to rules that are new in PMD v4.0
</description>
<rule ref="rulesets/migrating.xml/LongInstantiation"/>
<rule ref="rulesets/migrating.xml/ShortInstantiation"/>
<rule ref="rulesets/migrating.xml/ByteInstantiation"/>
</ruleset>

View File

@ -297,6 +297,7 @@
<li>Ebu - Eclipse smoothed icons</li>
<li>Jacques Lebourgeois - Eclipse fix malformed UTF-8 characters</li>
<li>Chris Grindstaff - Eclipse fix SWTException when PMD is run on a file with syntax error</li>
<li>jmichelberger - wrote Byte/Short/Long Instantiation migration rules</li>
</ul>
</subsection>
<subsection name="Organizations">