RE : Extending checkstyle to detect objetc comparison with == operator.

Arnaud Roques <[email protected]> Fri, 26 Sep 2008 22:58:42 +0200 (CEST)
Newsgroups gmane.comp.java.audit.checkstyle.user
Message-ID <[email protected]>
Hello,

In http://checkstyle.sourceforge.net/5.x/writingchecks.html, about limitations :
         There are basically only two of them:       
                                                 
   You cannot determine the type of an expression.
   You cannot see the content of other files.
And indeed, it is very difficult to determine the type of an expression...
So in your example, 

if (item1==item2) {..}

It is difficult to know which are the type of item1 and item2. Difficult, but not impossible, if item1 and/or item2 are local variable or fields of the current class, you *can* determine the type.

But in :

if (someObject.foo1() == item2) {...}
You will not be able to determine the type of foo1().


[email protected] a écrit : Hello,

I am currently working on a project which was partly developped by java newbies and we encountered several bugs which had for root cause object comparison using == instead of .equals().

Those objects (1 class + 1 subclass) are from a cached repository so they would work for unit testing but would start showing erratic behavior after cache invalidations.

I wanted to use checkstyle on this project to mainly improve code quality and enforce standards but i would also like to extend it to detect those erroneous comparisons.

Bad code would look like

RepositoryItem item1 = ...
RepositoryItem item2 = ...

if (item1 == item2) {
    doCriticalStuff();
}

Is it possible to extend checkstyle to detect this ?

I have started working on a check class (largely inspired from StringLiteralEquality) but i am unsure on how to check the instance type. I have the AST items ready (aka. getFirstChild() + getFirstChild().nextSibling() ). But i dont know how to proceed to detect that they are instances of RepositoryItem or one of their subclass.

I would appreciate any help on this matter.


Thanks in advance,
Benoit.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Checkstyle-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
Checkstyle-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user