问题描述
||
在我的findbugs报告中,以下代码部分中有SIO_SUPERFLUOUS_INSTANCEOF正确性错误
/** 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.topology.TopologyListNode.compareto (AbstractListItem)
// does an unnecessary type check using instanceof operator when it
// can be determined statically
return -1;
}
静态确定类型的正确方法是什么?解决方法
public int compareTo(AbstractListItem o)
-o
是AbstractListItem
,您无需检查。
如果您有public int compareTo(Object o)
,则需要instanceof
,并且不会发出警告。,在您的代码中,对象o的类型已经静态确定为AbstractListItem,因此instanceof是不必要的。 Findbugs将其报告为警告,因为它可能是错误的标志,例如,您要检查的是AbstractListItem的子类型,但是您错误地键入了AbstractListItem。