Extending checkstyle to detect objetc comparison with == operator.
[email protected] Thu, 25 Sep 2008 17:14:40 +0200 (CEST)
| Newsgroups | gmane.comp.java.audit.checkstyle.user |
|---|---|
| Message-ID | <1542409254.4117961222355680700.JavaMail.root@spooler6-g27.priv.proxad.net> |
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=/