Findbugs reports an incompatible bit mask bug, but I don't see how
I have a Comp开发者_JAVA技巧arator that checks "null"s for the two objects before comparing their contents. The compare method looks like this:
public int compare(MyClass left, MyClass right) {
if (left == null) {
return right == null ? 0 : 1;
}
if (right == null) {
return -1;
}
// do some other comparing
}
When I run this through sonar code quality checking tool, it reports "incompatible bit masks" error at the if statements. (It reads something like: "Correctness - Incompatible bit masks: Incompatible bit masks in (e | 0x1 = 0x0) yields constant result in ....Compare (MyClass, MyClass) I cannot see how this can be the case. Can anybody shed some light on this? Is this a false positive case?
By the way, The sonar version I am using is 2.6.
I believe i know what's going on. I believe your code is being weaved by Clover, and the clover code is embellishing that code and the way it does it is in a not so clean way.
44: sipush 14625
47: invokevirtual #10; //Method com_cenqua_clover/CoverageRecorder.iget:(I)I
50: ifeq 57
53: iconst_1
54: goto 58
57: iconst_0
58: iconst_1
59: ior
60: ifne 85
That is what FindBugs is complaining about.
public int compare(MyClass left, MyClass right) {
if (left == null) {
return right == null ? 0 : 1;
}
if (right == null) {
return -1;
}
// do some other comparing
}
This code should not even compile because in method signature you have made an agreement that this method will return int type but as it is having only if statement it is possible in some cases it might not even return a value.
Now coming to your "Incompatible bit masks" question it is because for (right == null) the method seems to return two value i.e. 0 and 1 although both not at the same time. It appears to your tool sonar code quality checking that the method is return two different values for the same comparision and hence one value may sometimes hide another value.
精彩评论