FindBugs looks for the instanceof
prior to checkcast
bytecode. You can use an assert
to please the FindBugs warning and future maintainers of your code.
A a = new B();
if (a.isOfTypeB()){
assert a instanceof B : a.getClass(); //Safe to call getClass because 'a' is non-null.
doSomething((B) a);
}
Prior to FindBugs 3.0 you could use dynamic cast to work around this warning. Don't do this as it is detected in later versions of FindBugs.
A a = new B();
if (a.isOfTypeB()) {
doSomething(B.class.cast(a));
}
One of the things to consider is that FindBugs detects patterns that do create actual bugs and patterns that can create actual bugs. The 'instanceof' keyword and Class.cast behavior can’t be overridden but 'isTypeOfB' can be overridden. Even if FindBugs doesn't detect that both examples of your code and mine function as expected, maybe the warning is correct on the grounds that it is advisable to not do this.