I have been surprised that Java lets expressions like the following compile:
int sum = 5;
sum += 4.2; // same as sum = (int)(sum + 4.2);
This would be a great candidate for a findbugs check if it's feasible. (I realize it also might cause a warning when someone types in sum = (int)(sum + 4.2), but IMHO that is rare enough not to be a big deal.)
And, thanks for the awesome tool! My students (and staff) really appreciate it.
Check is possible (there are even dedicated bytecode instructions I2D / D2I for this "integer to double/double to integer" conversion), but even because of this I guess this pattern happens pretty often, so that many false positives would make it too noisy.
If you could give some rough numbers how often the use of such code is actually "misuse"?
Regards,
Andrey
I don't know how to search for this at all, and I have to admit general ignorance on how findbugs works, which might help me search for it.
I tried using javap, and one of the patterns was
6: iload_1
7: i2d
8: ldc2_w #2; //double 4.2d
11: dadd
12: d2i
13: istore_1
(or _2 in place of _1). But if I am +=ing something more complex like += Math.sqrt(98.0) it looks like
25: iload_2
26: i2d
27: ldc2_w #6; //double 98.0d
30: invokestatic #8; //Method java/lang/Math.sqrt:(D)D
33: dadd
34: d2i
35: istore_2
So (1) is this actually a pattern that is feasible for findbugs? (It was said to be impossible for checkstyle.) and (2) if so, do you know how I can search for data within my data set?
Thanks!
I was trying to see how one can access an AST for Java source code. It looks like the com.sun.source.tree API lets you do this, but it doesn't give you type information about elements unless you use the not-totally-stable JCTree API. I just learned about the PMD tool today but it seems to have the same problem.
I wrote small detrctor for this, after some polishing I can offer it to you for some statistical research. OK?
Regards,
Andrey
Thanks! If you have the time for that, I will definitely run it.