Implemented OptimizableToArrayCallRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2687 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2004-05-18 19:23:57 +00:00
parent 360cc4c025
commit 755564bf81
4 changed files with 80 additions and 2 deletions

View File

@ -1,5 +1,5 @@
????, 2004 - 1.8:
New rules: ExceptionAsFlowControlRule, BadComparisonRule, AvoidThrowingCertainExceptionTypesRule, AvoidCatchingNPERule
New rules: ExceptionAsFlowControlRule, BadComparisonRule, AvoidThrowingCertainExceptionTypesRule, AvoidCatchingNPERule, OptimizableToArrayCallRule
Major grammar changes - lots of new node types added, many superfluous nodes removed from the runtime AST. Bug 786611 - http://sourceforge.net/tracker/index.php?func=detail&aid=786611&group_id=56262&atid=479921 - explains it a bit more.
Fixed bug 786611 - Expressions are no longer over-expanded in the AST
Fixed bug 874284 - The AST now contains tokens for bitwise or expressions - i.e., "|"

View File

@ -0,0 +1,39 @@
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.PMD;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class OptimizableToArrayCallRuleTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("rulesets/newrules.xml", "OptimizableToArrayCallRule");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "failure case", 1, rule),
new TestDescriptor(TEST2, "Array dimensioner uses method call, ok", 0, rule),
new TestDescriptor(TEST3, "Array dimensioner uses variable, ok", 0, rule),
});
}
private static final String TEST1 =
"public class Foo {" + PMD.EOL +
" {x.toArray(new Foo[0]);}" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + PMD.EOL +
" {x.toArray(new Foo[x.size()]);}" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + PMD.EOL +
" {x.toArray(new Foo[y]);}" + PMD.EOL +
"}";
}

View File

@ -6,6 +6,45 @@ These are new rules for the next release
</description>
<rule name="OptimizableToArrayCallRule"
message="This call to Collection.toArray() may be optimizable"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
A call to Collection.toArray can use the Collection's size vs an empty Array of the desired type.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name[ends-with(@Image, 'toArray')]]
[
PrimarySuffix/Arguments/ArgumentList/Expression
/PrimaryExpression/PrimaryPrefix/AllocationExpression
/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0']
]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
class Example {
void bar() {
// A bit inefficient, unlike...
x.toArray(new Foo[0]);
// ..this one, which sizes the destination array, avoiding
// a reflection call in some Collection implementations
x.toArray(new Foo[x.size()]);
}
} ]]>
</example>
</rule>
<rule name="BadComparisonRule"
message="Avoid equality comparisons with Double.NaN"
class="net.sourceforge.pmd.rules.XPathRule">

View File

@ -39,6 +39,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Radim Kubacki - OptimizableToArrayCallRule suggestion, bug reports, pmd-netbeans feedback</li>
<li>Will Sargent - AvoidThrowingCertainExceptionTypesRule, AvoidCatchingNPERule, ExceptionAsFlowControlRule, URL updates for 'Similar projects' page</li>
<li>Mike Thome - BadComparisonRule suggestion</li>
<li>Bertrand Mollinier Toublet - Bug report which led to platform character set encoding enhancement</li>
@ -131,7 +132,6 @@
<li>J.D. Fagan - feature suggestions</li>
<li>William McArthur - ForLoopShouldBeWhileLoop rule</li>
<li>Ales Bukovsky - pmd-netbeans feedback</li>
<li>Radim Kubacki - bug reports, pmd-netbeans feedback</li>
<li>Stefan Bodewig - bug report</li>
<li>Paul Kendall - enhanced EmptyCatchBlock rule</li>
<li>Sean Sullivan - rule suggestions</li>