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

 

解释说明:

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

代码示例:

FindBugs常见错误描述和解决方法_第1张图片

修改方式:

FindBugs常见错误描述和解决方法_第2张图片

 

 

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关系很可能不是对称的。

 

代码示例:

修改方式:

FindBugs常见错误描述和解决方法_第3张图片

 

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 方法。

代码示例:

修改示例:

你可能感兴趣的:(FindBugs常见错误描述和解决方法)