Menu

Rule for SuspiciousEqualsMethodName

2004-07-29
2012-10-07
  • Peter Kofler

    Peter Kofler - 2004-07-29

    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>

     
    • Tom Copeland

      Tom Copeland - 2004-07-29

      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

       
      • Tom Copeland

        Tom Copeland - 2004-07-29

        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

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.