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

Arnaud Roques <[email protected]> Sat, 27 Sep 2008 12:13:43 +0200 (CEST)
Newsgroups gmane.comp.java.audit.checkstyle.user
Message-ID <[email protected]>
Hello,

You can also use regular expression to do the job.

Indeed, you want to detect:

SomeClass variable [...anything...] variable  == anythingButNULL

So if you assume that class name begins with an uppercase, variable with a lowercase, you can use:
 
^ *(?:[A-Z]\w+)\b +([a-z]\w*)\b[\s\S]*?\1 *==(?!null)(?! null)

And also:
SomeClass variable [...anything...] anythingButNULL == variable

^ *(?:[A-Z]\w+)\b +([a-z]\w*)\b[\s\S]*?==(?<!null==)(?<!null ==) *\1(?!\.)

I've attached a file with the correct module definition.



[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
equals.txt (text/plain, 673 B)
		<module name="Regexp">
			<property name="format"
				value="^ *(?:[A-Z]\w+)\b +([a-z]\w*)\b[\s\S]*?\1 *==(?!null)(?! null)" />
			<property name="message" value="Suspect use of == with this variable latter" />
			<property name="ignoreComments" value="true" />
			<property name="illegalPattern" value="true" />
		</module>

		<module name="Regexp">
			<property name="format"
				value="^ *(?:[A-Z]\w+)\b +([a-z]\w*)\b[\s\S]*?==(?&lt;!null==)(?&lt;!null ==) *\1(?!\.)" />
			<property name="message" value="Suspect use of == with this variable latter" />
			<property name="ignoreComments" value="true" />
			<property name="illegalPattern" value="true" />
		</module>