Applied Allan Caplan's IntegerInstantiation new rule patch, thanks Allan!
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4026 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
????, 2006 - 3.5:
|
||||
New rules:
|
||||
Migration ruleset: IntegerInstantiation
|
||||
Removed old-style CPD command line options that were deprecated in 3.4.
|
||||
|
||||
November 30, 2005 - 3.4:
|
||||
|
@ -0,0 +1,34 @@
|
||||
package test.net.sourceforge.pmd.rules.migrating;
|
||||
|
||||
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
|
||||
import test.net.sourceforge.pmd.testframework.TestDescriptor;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleSetNotFoundException;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
|
||||
public class IntegerInstantiationTest extends SimpleAggregatorTst {
|
||||
|
||||
private Rule rule;
|
||||
|
||||
public void setUp() throws RuleSetNotFoundException {
|
||||
rule = findRule("migrating", "IntegerInstantiation");
|
||||
}
|
||||
|
||||
public void testAll() {
|
||||
runTests(new TestDescriptor[] {
|
||||
new TestDescriptor(TEST1, "new Integer(), bad", 1, rule),
|
||||
new TestDescriptor(TEST2, "Integer.valueOf(), ok", 0, rule),
|
||||
});
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" Integer i = new Integer(42);" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST2 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" Integer i = Integer.valueOf(\"42\");" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
}
|
@ -147,5 +147,32 @@ public class Foo implements Enumeration {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="IntegerInstantiation"
|
||||
message="Avoid instantiating Integer objects. Call Integer.valueOf() instead"
|
||||
class="net.sourceforge.pmd.rules.XPathRule">
|
||||
<description>In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly.</description>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryPrefix
|
||||
/AllocationExpression
|
||||
[not (ArrayDimsAndInits)
|
||||
and (ClassOrInterfaceType/@Image='Integer'
|
||||
or ClassOrInterfaceType/@Image='java.lang.Integer')]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<priority>2</priority>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class Foo {
|
||||
private Integer i = new Integer(0); // change to Integer i = Integer.valueOf(0);
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
|
||||
|
@ -7,6 +7,7 @@ Contains rules for migrating to JDK 1.5
|
||||
</description>
|
||||
|
||||
<rule ref="rulesets/migrating.xml/AvoidEnumAsIdentifierTest"/>
|
||||
<rule ref="rulesets/migrating.xml/IntegerInstantiation"/>
|
||||
|
||||
</ruleset>
|
||||
|
||||
|
@ -45,12 +45,12 @@
|
||||
</subsection>
|
||||
<subsection name="Contributors">
|
||||
<ul>
|
||||
<li>Allan Caplan - wrote IntegerInstantiation, wrote UseStringBufferLength, wrote AvoidEnumAsIdentifier, bug report 1313216 for designer flaw, patch 1306999 to enhance CloseResource to check for more types, suggested including suppressed rule violations in the report</li>
|
||||
<li>Jean-Marc Vanel - suggested enhancements to the PMD scoreboard</li>
|
||||
<li>Didier Duquennoy - bug report for ImmutableField, suggestions for improving Benchmark utility, bug report for InefficientStringBuffering, bug report for AvoidConcateningNonLiteralsInStringBuffer, reported a missed hit for EqualsNull, bug report for MissingStaticMethodInNonInstantiatableClass, pmd-netbeans feedback</li>
|
||||
<li>Fabio Insaccanebbia - AvoidDecimalLiteralsInBigDecimalConstructor, ClassCastExceptionWithToArray</li>
|
||||
<li>Johan Stuyts - nice patch for UncommentedEmptyConstructor and UncommentedEmptyMethod, patch to allow empty catch blocks with comments in them, patch to clean up build environment</li>
|
||||
<li>Andriy Rozeluk - suggested UseStringBufferLength, bug report 1306180 for AvoidConcatenatingNonLiteralsInStringBuffer, reported bug 1293157 for UnusedPrivateMethod, suggested UnnecessaryCaseChange, bug report for SimplifyConditional, suggested UnnecessaryLocalBeforeReturn, suggestions for improving BooleanInstantiation, UnnecessaryReturn, AvoidDuplicateLiterals RFEs and bug reports, various other RFEs and thoughtful discussions as well</li>
|
||||
<li>Allan Caplan - wrote UseStringBufferLength, wrote AvoidEnumAsIdentifier, bug report 1313216 for designer flaw, patch 1306999 to enhance CloseResource to check for more types, suggested including suppressed rule violations in the report</li>
|
||||
<li>Bruno Juillet - suggested reporting suppressed warnings, bug report for missing package/class/method names, patch for Ant task's excludeMarker attribute, bug report on ruleset overrides</li>
|
||||
<li>Chris Erskine - documentation suggestions</li>
|
||||
<li>Noel Grandin - bug report for MissingStaticMethodInNonInstantiatableClass, bug report for MissingBreakInSwitch, EqualsNull rule, bug report for IfElseStmtsMustUseBracesRule</li>
|
||||
|
Reference in New Issue
Block a user