Findbugs 中的常用的bug pattern配置
Bug pattern |
description |
Bad practice |
不好的习惯 |
Correctness |
代码的正确性 |
Dodgy |
小问题 |
Malicious code vulnerability |
恶意代码 |
Internationalization |
国际化问题 |
Performance |
性能问题 |
Security |
安全性问题 |
Multithreaded currectness |
线程问题 |
Experrimental |
实验性问题 |
常见错误和原因
- ES_COMPARING_PARAMETER_STRING_WITH_EQ
ES: Comparison of String parameter using == or != (ES_COMPARING_PARAMETER_STRING_WITH_EQ)
使用 == 或者 != 来比较字符串或interned字符串,不会获得显著的性能提升,同时并不可靠,请考虑使用equals()方法。
- HE_EQUALS_NO_HASHCODE
HE: Class defines equals() but not hashCode() (HE_EQUALS_NO_HASHCODE)
类定义了equals()方法但没有重写hashCode()方法,这样违背了相同对象必须具有相同的hashcodes的原则
- IT_NO_SUCH_ELEMENT
It: Iterator next() method can’t throw NoSuchElement exception (IT_NO_SUCH_ELEMENT)
迭代器Iterator无法抛出NoSuchElement异常,类实现了java.util.Iterator接口,但是next()方法无法抛出java.util.NoSuchElementException异常,因此,next()方法应该做如此修改,当被调用时,如果没有element返回,则抛出NoSuchElementException异常
- J2EE_STORE_OF_NON_SERIALIZABLE_OBJECT_INTO_SESSION
J2EE: Store of non serializable object into HttpSession (J2EE_STORE_OF_NON_SERIALIZABLE_OBJECT_INTO_SESSION)
将没有实现serializable的对象放到HttpSession中,当这个session被钝化和迁移时,将会产生错误,建议放到HttpSession中的对象都实现serializable接口。
- ODR_OPEN_DATABASE_RESOURCE
ODR: Method may fail to close database resource (ODR_OPEN_DATABASE_RESOURCE)
方法可能未关闭数据库资源,未关闭数据库资源将会导致性能变差,还可能引起应用与服务器间的通讯问题。
- OS_OPEN_STREAM
OS: Method may fail to close stream (OS_OPEN_STREAM)
方法可能未关闭stream,方法产生了一个IO流,却未关闭,将会导致文件描绘符的泄漏,建议使用finally block来确保io stream被关闭。
- DMI_CALLING_NEXT_FROM_HASNEXT
DMI: hasNext method invokes next (DMI_CALLING_NEXT_FROM_HASNEXT)
The hasNext() method invokes the next() method. This is almost certainly wrong, since the hasNext() method is not supposed to change the state of the iterator, and the next method is supposed to change the state of the iterator.
- IL_INFINITE_LOOP
IL: An apparent infinite loop (IL_INFINITE_LOOP)
明显的无限循环.
- IL_INFINITE_RECURSIVE_LOOP
IL: An apparent infinite recursive loop (IL_INFINITE_RECURSIVE_LOOP)
明显的无限迭代循环,将导致堆栈溢出.
- WMI_WRONG_MAP_ITERATOR
WMI: Inefficient use of keySet iterator instead of entrySet iterator (WMI_WRONG_MAP_ITERATOR)
使用了keySet iterator和Map.get(key)来获取Map值,这种方式效率低,建议使用entrySet的iterator效率更高.
- IM_BAD_CHECK_FOR_ODD
IM: Check for oddness that won’t work for negative numbers (IM_BAD_CHECK_FOR_ODD)
奇偶检测逻辑,未考虑负数情况.
常见bug 类型
- Call to equals() comparing different types
id: EC_UNRELATED_TYPES, type: EC, category: CORRECTNESS
原因分析:
这缺陷的意思是,大部分都是类型永远不会有这种情况, 比如a为DOUBLE类型所以EQUALS只匹配字符串 if(a.equals())或if(a.quals())这类判断是根本不会有用的;
示例:if(“1”.equals(DAOValue.valueofSuccess()))
- Dead store to local variable
id: DLS_DEAD_LOCAL_STORE, type: DLS, category: STYLE
原因分析:
DLS问题指的是给本地变量赋了一个值,但随后的代码并没有用到这个值。
- Method call passes null for nonnull parameter
id: NP_NULL_PARAM_DEREF, type: NP, category: CORRECTNESS
原因分析:对参数为null的情况未作处理。
问题代码:
修改后:
- Method with Boolean return type returns explicit null
id: NP_BOOLEAN_RETURN_NULL, type: NP, category: BAD_PRACTICE
原因分析:
方法如果定义为返回类型Boolean,则可以返回Boolean.TRUE, Boolean.FALSE or null (如果 return 的是 true or false, 则AutoBoxing 成 Boolean.TRUE, Boolean.FALSE)。因为JDK 支持 基本类型和装箱类型的自动转化, 所以下面的代码中:
boolean result = test_NP_BOOLEAN_RETURN_NULL();
因为此时test_NP_BOOLEAN_RETURN_NULL() 返回的是NULL, 所以 JDK 做 automatic unboxing 的操作时, 即调用了 object. booleanValue() 方法时,抛出了空指针。
改成:boolean result = test_NP_BOOLEAN_RETURN_NULL()==null?false:true;
- No relationship between generic parameter and method argument
id: GC_UNRELATED_TYPES, type: GC, category: CORRECTNESS
原因分析:调用Collection类中的contains方法比较时,所比较的两个参数类型不致;
例如:
修改后:
- Null pointer dereference in method on exception path
id: NP_ALWAYS_NULL_EXCEPTION, type: NP, category: CORRECTNESS
原因分析:在异常处理时,调用一个空对象的方法时可能引起空指针异常。
例如:
- Nullcheck of value previously dereferenced
id: RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE, type: RCN, category: CORRECTNESS
原因分析:前面获取的对象,现在引用的时候没有交验是否为null。
例如:
- Possible null pointer dereference
id: NP_NULL_ON_SOME_PATH, type: NP, category: CORRECTNESS
原因分析:可能存在空引用。
例如:
- Possible null pointer dereference in method on exception path
id: NP_NULL_ON_SOME_PATH_EXCEPTION, type: NP, category: CORRECTNESS
原因分析:
代码调用时, 遇到异常分支, 可能造成一个对象没有获得赋值依旧保持NULL空指针。 接下来如果对这个对象有引用, 可能造成NullPointerException 空指针异常。.
- Test for floating point equality
id: FE_FLOATING_POINT_EQUALITY, type: FE, category: STYLE
原因分析:
Float类型的数据比较时,会存在的定的误差值,用!=来比较不是很准确,建议比较两个数的绝对值是否在一定的范围内来进行比较。如,if ( Math.abs(x - y) < .0000001 )
例如:
- Useless assignment in return statement
id: DLS_DEAD_LOCAL_STORE_IN_RETURN, type: DLS, category: STYLE
原因分析:
在return的对象中,没有必要通过对象赋值再进行返回。
例如:
- Write to static field from instance method
id: ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD, type: ST, category: STYLE
原因分析:向static字段中写入值。
例如:
private static DBRBO dbrBO;
public final void refresh() {
danskeBankBO = null;
dbrBO = null;
fileAndPathBO = null;
}
建
议改为:去掉static。
- Incorrect lazy initialization and update of static field
id: LI_LAZY_INIT_UPDATE_STATIC, type: LI, category: MT_CORRECTNESS
原因分析:
该方法的初始化中包含了一个迟缓初始化的静态变量。你的方法引用了一个静态变量,估计是类静态变量,那么多线程调用这个方法时,你的变量就会面临线程安全的问题了,除非别的东西阻止任何其他线程访问存储对象从直到它完全被初始化。
- Method ignores return value
id: RV_RETURN_VALUE_IGNORED, type: RV, category: CORRECTNESS
原因分析:方法忽略了设置返回值。
例如:
- Method might ignore exception
id: DE_MIGHT_IGNORE, type: DE, category: BAD_PRACTICE
原因分析:应该将异常 处理、打印或者抛出
例如:
id: UWF_UNWRITTEN_FIELD, type: UwF, category: CORRECTNESS
原因分析:从未被初始化的变量,调用它时,将返回默认值,要么初始化,要么删掉它。
例如:
- Value is null and guaranteed to be dereferenced on exception path
id: NP_GUARANTEED_DEREF_ON_EXCEPTION_PATH, type: NP, category: CORRECTNESS
原因分析:exception分支上,存在引用一个null对象的方法,引发空指针异常。
例如:
- Very confusing method names
id: NM_VERY_CONFUSING, type: Nm, category: CORRECTNESS
原因分析:被引用的方法中存在容易混淆的变量。
fzgsdm改成 fzgsDm 即可。
- Method invokes inefficient new String() constructor
id: DM_STRING_VOID_CTOR, type: Dm, category: Performance
原因分析:不使用new String()定义空的字符串
例如:
改成:
id: NP_LOAD_OF_KNOWN_NULL_VALUE, type: Np, category: Dodgy
原因分析:null值的不当使用。
例如:
- Method concatenates strings using + in a loop
id: SBSC_USE_STRINGBUFFER_CONCATENATION, type: SBSC, category: Performance
原因分析:在循环里使用字符串连接,效率低,应该使用StringBuilder/StringBuffer
例如:
// This is bad
String s = "";
for (int i = 0; i < field.length; ++i) {
s = s + field[i];
}
改为:
// This is better
StringBuffer buf = new StringBuffer();
for (int i = 0; i < field.length; ++i) {
buf.append(field[i]);
}
String s = buf.toString();
详细的参考以下网址
API:http://findbugs.sourceforge.net/api/index.html
技术手册:http://findbugs.sourceforge.net/manual/index.html
更多请参见官网:http://findbugs.sourceforge.net/bugDescriptions.html
本文转载在:https://www.cnblogs.com/pony1223/p/Findbugs.html