Implemented new rule InstantiationToGetClass, thanks Ernst!
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2837 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
?????, 2004 - 2.0:
|
||||
New rules: InstantiationToGetClass
|
||||
Moved development environment to Maven 1.0.
|
||||
Moved development environment to Ant 1.6.2. This is nice because using the new JUnit task attribute "forkmode='perBatch'" cuts test runtime from 90 seconds to 7 seconds. Sweet.
|
||||
MethodWithSameNameAsEnclosingClass now reports a more helpful line number.
|
||||
|
@ -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 InstantiationToGetClassRuleTest extends SimpleAggregatorTst {
|
||||
|
||||
private Rule rule;
|
||||
|
||||
public void setUp() throws RuleSetNotFoundException {
|
||||
rule = findRule("rulesets/newrules.xml", "InstantiationToGetClass");
|
||||
}
|
||||
|
||||
public void testAll() {
|
||||
runTests(new TestDescriptor[] {
|
||||
new TestDescriptor(TEST1, "simple failure case", 1, rule),
|
||||
new TestDescriptor(TEST2, "ok", 0, rule),
|
||||
new TestDescriptor(TEST3, "should catch param to constructor", 1, rule),
|
||||
});
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" Class clazz = new String().getClass();" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST2 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" Class clazz = getFoo().getClass();" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST3 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" Class clazz = new Integer(10).getClass();" + PMD.EOL +
|
||||
"}";
|
||||
}
|
@ -5,7 +5,35 @@
|
||||
These are new rules for the next release
|
||||
</description>
|
||||
|
||||
|
||||
<rule name="InstantiationToGetClass"
|
||||
message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead"
|
||||
class="net.sourceforge.pmd.rules.XPathRule">
|
||||
<description>
|
||||
Avoid instantiating an object just to call getClass() on it; use the .class public member instead
|
||||
</description>
|
||||
<example>
|
||||
<![CDATA[
|
||||
class Foo {
|
||||
Class c = new String().getClass();
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
<priority>4</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimarySuffix
|
||||
[@Image='getClass']
|
||||
[parent::PrimaryExpression
|
||||
[PrimaryPrefix/AllocationExpression]
|
||||
[count(PrimarySuffix) = 2]
|
||||
]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
|
||||
|
@ -5,21 +5,34 @@
|
||||
These are new rules that are still in progress
|
||||
</description>
|
||||
|
||||
<rule name="UnusedImports"
|
||||
message="Avoid unused imports such as ''{0}''"
|
||||
class="net.sourceforge.pmd.rules.UnusedImportsRule">
|
||||
<rule name="InstantiationToGetClass"
|
||||
message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead"
|
||||
class="net.sourceforge.pmd.rules.XPathRule">
|
||||
<description>
|
||||
Avoid unused import statements.
|
||||
Avoid instantiating an object just to call getClass() on it; use the .class public member instead
|
||||
</description>
|
||||
<example>
|
||||
<![CDATA[
|
||||
class Foo {
|
||||
Class c = new String().getClass();
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
<priority>4</priority>
|
||||
|
||||
<example>
|
||||
<![CDATA[
|
||||
// this is bad
|
||||
import java.io.File;
|
||||
public class Foo {}
|
||||
]]>
|
||||
</example>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimarySuffix
|
||||
[@Image='getClass']
|
||||
[parent::PrimaryExpression
|
||||
[PrimaryPrefix/AllocationExpression]
|
||||
[count(PrimarySuffix) = 2]
|
||||
]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
|
||||
|
@ -40,7 +40,7 @@
|
||||
</subsection>
|
||||
<subsection name="Contributors">
|
||||
<ul>
|
||||
<li>Ernst Reissner - several rule suggestions, bug reports for UnusedPrivateField/CloseConnectionRule/ConstructorCallsOverridableMethodRule, and bug report and documentation suggestions for UseSingletonRule</li>
|
||||
<li>Ernst Reissner - suggested InstantiationToGetClass, bug reports for UnusedPrivateField/CloseConnectionRule/ConstructorCallsOverridableMethodRule, and bug report and documentation suggestions for UseSingletonRule</li>
|
||||
<li>Philippe Couton - bug report for ExceptionAsFlowControl, OverrideBothEqualsAndHashcodeRule bug report, UseSingletonRule improvements, JUnitStaticSuiteRule improvements</li>
|
||||
<li>Dave Brosius - a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
|
||||
<li>Paul Rowe - suggestion for improving MethodWithSameNameAsEnclosingClass, bug reports for SimplifyBooleanExpressions and UnusedLocalVariable</li>
|
||||
|
Reference in New Issue
Block a user