FindBugs错误分析说明 - 图文

发布时间 : 星期一 文章FindBugs错误分析说明 - 图文更新完毕开始阅读

Bug: PerfmSingleGraphPanel$RSCategory defines equals and uses Object.hashCode() Pattern id: HE_EQUALS_USE_HASHCODE, type: HE, category: BAD_PRACTICE 解释:

重载了equals方法,却没有重载hashCode方法,如果使用object自己的hashCode,我们可 以从JDK源代码可以看到object的hashCode方法是native的,它的值由虚拟机分配(某种 情况下代表了在虚拟机中的地址或者唯一标识),每个对象都不一样。所以这很可能违反 “Equals相等,hashcode一定相等;hashcode相等,equals不一定相等。”除非你保证不运 用到HashMap/HashTable等运用散列表查找值的数据结构中。否则,发生任何事情都是有可 能的。

关于何时改写hashcode,请参考:在重写了对象的equals方法后,还需要重写hashCode 方法吗?

关于编写高质量的equals方法:

1.先使用==操作符检查是否是同一个对象,==都相等,那么逻辑相等肯定成立; 2.然后使用instanceof操作符检查“参数是否为正确的类型”; 3.把参数转换成正确的类型;

4.对于该类中的非基本类型变量,递归调用equals方法;

5.变量的比较顺序可能会影响到equals方法的性能,应该最先比较最有可能不一致的变 量,或者是开销最低的变量。

当你编写完成equals方法之后,应该问自己三个问题:它是否是对称的、传递的、一 致的?

解决方法:

除非你保证不运用到HashMap/HashTable等运用散列表查找值的数据结构中,请重写 hashcode方法。

9. NM_CONFUSING

Bug: Confusing to have methods

xxx.SellerBrandServiceImpl.getAllGrantSellerBrandsByBrandId(long) and xxx.DefaultSellerBrandManager.getALLGrantSellerBrandsByBrandId(long) Pattern id: NM_CONFUSING, type: Nm, category: BAD_PRACTICE

The referenced methods have names that differ only by capitalization.

解释:

同一个包两个类中有一模一样的两个方法(包括参数) 解决方法:

最好可以修改为不一样的方法名称

10. MF_CLASS_MASKS_FIELD

Bug: Field PDHSubCardInstanceDialogCommand.m_instance masks field in superclass ViewNEProperity

Pattern id: MF_CLASS_MASKS_FIELD, type: MF, category: CORRECTNESS

This class defines a field with the same name as a visible instance field in a superclass. This is confusing, and may indicate an error if methods update or access one of the fields when they wanted the other.

解释:

这是什么意思呢?想要字段也能够具有多态性吗?太迷惑了。

当你想要更新一个m_instance时,你要更新哪个?你用到它时,你知道哪个又被更新了? 解决方法:

要么去掉其中一个字段,要么重新命名。

11. NM_CLASS_NAMING_CONVENTION

Bug: The class name crossConnectIndexCollecter doesn't start with an upper case letter 解释:

Pattern id: NM_CLASS_NAMING_CONVENTION, type: Nm, category: BAD_PRACTICE 看到这样的命名方式,我第一个反映就是有点晕车! 解决方法:

类名第一个字符请大写。

12. RE_POSSIBLE_UNINTENDED_PATTERN

Bug: \

Pattern id: RE_POSSIBLE_UNINTENDED_PATTERN, type: RE, category: CORRECTNESS 解释:

String的split方法传递的参数是正则表达式,正则表达式本身用到的字符需要转义,如:句

点符号“.”,美元符号“$”,乘方符号“^”,大括号“{}”,方括号“[]”,圆括号“()” ,竖线“|”,星号“*”,加号“+”,问号“?”等等,这些需要在前面加上“ \\\\”转义符。 解决方法:

在前面加上“\\\\”转义符。

13.IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OU TER_METHOD

外部类:

内部类:

??

Bug: Ambiguous invocation of either an outer or inherited method JExtendDialog.onOK() Pattern id: IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD, type: IA, category: STYLE 解释:

TargetSetupDialog是JExtendDialog的子类,JExtendDialog有一个onOK方法,但是JExtendDialog 的外部类也有一个onOK方法,到底这个onOK方法调用的是它父类onOK方法还是调用它 外部类onOK方法呢,这不免让人误解。

当然这并没有编译错误,实际上优先调用的是父类JExtendDialog的onOK方法,如果把 JExtendDialog的onOK方法去掉,它调用的就是外部类onOK方法,这个时候不能写成 this.onOK,因为此时的this并不代表外部类对象。 解决方法:

如果要引用外部类对象,可以加上“outclass.this”。 如果要引用父类的onOK方法,请使用super.onOK()。

14. DM_FP_NUMBER_CTOR

Bug: Method

OnlineLicenseDAOTest.testUpdateOnlineLicenseByOnlineMerchantId() invokes inefficient Double.valueOf(double) constructor; use OnlineLicenseDAOTest.java:[line 81] instead

Pattern id: DM_FP_NUMBER_CTOR, type: Bx, category: PERFORMANCE

Using new Double(double) is guaranteed to always result in a new object whereas Double.valueOf(double) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.

联系合同范文客服:xxxxx#qq.com(#替换为@)