In my findbugs report I have a SIO_SUPERFLUOUS_INSTANCEOF correctness error in the following code section
/** Allow Comparison based on User-Labels */
public int compareTo(AbstractListItem o) {
if ( !( o instanceof AbstractListItem ) ) {
// Correctness - Unnecessary type check done using instanceof operator
// : Method com.x.y.gui.topolog开发者_开发技巧y.TopologyListNode.compareTo (AbstractListItem)
// does an unnecessary type check using instanceof operator when it
// can be determined statically
return -1;
}
What is the correct way to statically determine the type?
public int compareTo(AbstractListItem o)
- o
is an AbstractListItem
, you don't need to check.
If you had public int compareTo(Object o)
then your instanceof
would be needed and not produce a warning.
In your code the type of object o is already statically determined as AbstractListItem, and therefore the instanceof is unnecessary. Findbugs reported it as a warning because it may be a sign of bug, e.g., what you wanted to check was a subtype of AbstractListItem, but you'd typed AbstractListItem by mistake.
精彩评论