FindBugs常见错误描述和解决方法

SHOULD BE A STATIC INNER CLASS

官方介绍:

This class is an inner class, but does not use its embedded reference to the object which created it.This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static

 

解释说明:

若成员类中未访问外围类的非静态成员,为避免额外的空间和时间开销,建议改用静态成员类。

代码示例:

修改方式:

 

 

EQUALS METHOD OVERRIDES EQUALS IN SUPERCLASS AND MAY NOT BE SYMMETRIC

官方介绍:

This class defines an equals method that overrides an equals method in a superclass. Both equals methods methods use instanceof in the determination of whether two objects are equal. This is fraught with peril, since it is important that the equals method is symmetrical (in other words, a.equals(b) == b.equals(a)). If B is a subtype of A, and A's equals method checks that the argument is an instanceof A, and B's equals method checks that the argument is an instanceof B, it is quite likely that the equivalence relation defined by these methods is not symmetric。

 

解释说明:

子类覆盖了父类中的equals方法,而两个类中都是用了instanceof来判断两个对象是否相等,这个操作存在风险。equals方法应该具有对称性(a.equals(b) == b.equals(a)),但是当B是A的子类时,A的equals方法检查参数是A的实例,B的equals方法检查参数是B的实例,则这些方法定义的equal关系很可能不是对称的。

 

代码示例:

修改方式:

 

REPEATED CONDITIONAL TESTS

官方介绍:

The code contains a conditional test is performed twice, one right after the other (e.g., x == 0 || x == 0). Perhaps the second occurrence is intended to be something else (e.g., x == 0 || y == 0)。

 

解释说明:

该代码包含对同一个条件试验了两次,两边完全一样例如:(如X == 0 | | x == 0)。可能第二次出现是打算判断别的不同条件(如X == 0 | | y== 0)。

 

代码示例:

修改方式:

 

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

官方介绍:

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value。

 

解释说明:

没有检查方法的返回值,但返回值可以代表异常。

 

 

代码示例:

修改方式:

 

SE_COMPARATOR_SHOULD_BE_SERIALIZABLE

官方说明:

This class implements the Comparator interface. You should consider whether or not it should also implement the Serializable interface. If a comparator is used to construct an ordered collection such as a TreeMap, then the TreeMap will be serializable only if the comparator is also serializable. As most comparators have little or no state, making them serializable is generally easy and good defensive programming。

 

解释说明:

Comparator接口没有实现Serializable接口

修改方式:

实现Serializable接口

 

 

DM_NUMBER_CTOR

官方说明:

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) 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。

 

解释说明:

性能 - 方法调用低效的数字构造方法;使用静态valueOf代替

 

代码示例:

修改示例:

 

DM_BOXED_PRIMITIVE_FOR_PARSING

官方说明:

A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method。

解释说明:

装箱和拆箱的问题,把一个字符串装箱,解析获取箱子中原始值。更有效率就是使用静态方法,parseXXX 方法。

代码示例:

修改示例:

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值