Hello, similar to the rule SuspiciousHashcodeMethodName I created one for equals (often used with wrong parameter type).
<rule name="SuspiciousEqualsMethodName"
message="The method name and parameter number are suspiciously close to equals(Object)"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
The method name and parameter number are suspiciously close to equals(Object), which
may mean you are trying (and failing) to override the equals(Object) method.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassDeclaration//MethodDeclarator
[@Image = 'equals']
[count(FormalParameters/*) = 1 ]
[ not (FormalParameters//Type/Name
[@Image = 'Object' or @Image = 'java.lang.Object'])
]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
public int equals(Object o) {
// oops, this probably was supposed to be boolean equals
}
public boolean equals(String s) {
// oops, this probably was supposed to be equals(Object)
}
}
]]>
</example>
</rule>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Oops, I had left the rule in the scratchpad.xml file... now it's in the newrules.xml file and I've uploaded a new pmd-1.9.jar... sorry about any problems...
Yours,
tom
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, similar to the rule SuspiciousHashcodeMethodName I created one for equals (often used with wrong parameter type).
<rule name="SuspiciousEqualsMethodName"
message="The method name and parameter number are suspiciously close to equals(Object)"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
The method name and parameter number are suspiciously close to equals(Object), which
may mean you are trying (and failing) to override the equals(Object) method.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassDeclaration//MethodDeclarator
[@Image = 'equals']
[count(FormalParameters/*) = 1 ]
[ not (FormalParameters//Type/Name
[@Image = 'Object' or @Image = 'java.lang.Object'])
]
]]>
</value>
</property>
</properties>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
public int equals(Object o) {
// oops, this probably was supposed to be boolean equals
}
public boolean equals(String s) {
// oops, this probably was supposed to be equals(Object)
}
}
]]>
</example>
</rule>
Good one, thanks Bruno! I've written some JUnit tests for this and checked it into CVS, and you can download a new pmd-1.9.jar file here:
http://infoether.com/~tom/pmd-1.9.jar
that contains this new rules in the "rulesets/newrules.xml" ruleset.
Thanks,
Tom
Oops, I had left the rule in the scratchpad.xml file... now it's in the newrules.xml file and I've uploaded a new pmd-1.9.jar... sorry about any problems...
Yours,
tom