diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt
index f34091a604..95b919811d 100644
--- a/pmd/etc/changelog.txt
+++ b/pmd/etc/changelog.txt
@@ -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.
diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/InstantiationToGetClassRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/InstantiationToGetClassRuleTest.java
new file mode 100644
index 0000000000..fb4d2a14cc
--- /dev/null
+++ b/pmd/regress/test/net/sourceforge/pmd/rules/InstantiationToGetClassRuleTest.java
@@ -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 +
+ "}";
+}
diff --git a/pmd/rulesets/newrules.xml b/pmd/rulesets/newrules.xml
index 489298de5c..e690b6321a 100644
--- a/pmd/rulesets/newrules.xml
+++ b/pmd/rulesets/newrules.xml
@@ -5,7 +5,35 @@
These are new rules for the next release
-
+
+
+Avoid instantiating an object just to call getClass() on it; use the .class public member instead
+
+
+
+
+ 4
+
+
+
+
+
+
+
+
diff --git a/pmd/rulesets/scratchpad.xml b/pmd/rulesets/scratchpad.xml
index 818bd08806..ba774520d2 100644
--- a/pmd/rulesets/scratchpad.xml
+++ b/pmd/rulesets/scratchpad.xml
@@ -5,21 +5,34 @@
These are new rules that are still in progress
-
+
- Avoid unused import statements.
+Avoid instantiating an object just to call getClass() on it; use the .class public member instead
+
+
+
4
-
-
-
-
+
+
+
+
+
+
+
diff --git a/pmd/xdocs/credits.xml b/pmd/xdocs/credits.xml
index 8191a830ab..666f3e30ad 100644
--- a/pmd/xdocs/credits.xml
+++ b/pmd/xdocs/credits.xml
@@ -40,7 +40,7 @@
- - Ernst Reissner - several rule suggestions, bug reports for UnusedPrivateField/CloseConnectionRule/ConstructorCallsOverridableMethodRule, and bug report and documentation suggestions for UseSingletonRule
+ - Ernst Reissner - suggested InstantiationToGetClass, bug reports for UnusedPrivateField/CloseConnectionRule/ConstructorCallsOverridableMethodRule, and bug report and documentation suggestions for UseSingletonRule
- Philippe Couton - bug report for ExceptionAsFlowControl, OverrideBothEqualsAndHashcodeRule bug report, UseSingletonRule improvements, JUnitStaticSuiteRule improvements
- 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 :-)
- Paul Rowe - suggestion for improving MethodWithSameNameAsEnclosingClass, bug reports for SimplifyBooleanExpressions and UnusedLocalVariable