使用findbugs后出现的问题整理解析(取其中比较常见的,一些偏僻的没有写出来):
格式:
type name(details),
example,
translate.
1
BX_BOXING_IMMEDIATELY_UNBOXED(Primitive value is boxed and then immediately unboxed),
double d = Double.valueOf(a).doubleValue/4;-->double d = (double)a/4,
2
clone method does not call super.clone()
子类重写clone方法没有调用super.clone();
3
Method might ignore exception
catch块中没有做任何事情
4
DM_CONVERT_CASE(Consider using Locale parameterized version of invoked method)
String.toLowerCase()-->s.toLowerCase(Locale)
主要为了实现代码的国际化
5
DM_NUMBER_CTOR(Method invokes inefficient Number constructor; use static valueOf instead),
new Integer(int)-->Integer.valueOf(int)
6
DM_FP_NUMBER_CTOR(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,)
new Double()-->Double.valueOf()
7
DM_NEW_FOR_GETCLASS
new String().getClass()改为String.class.
仅为了获得一个方法就创建了一个对象
8
DM_STRING_TOSTRING(Method invokes toString() method on a String)
request.getParameter(name).toString()-->request.getParameter(name)
String.toString(),多此一举
9
DMI_EMPTY_DB_PASSWORD(Empty database password)
10
EQ_COMPARETO_USE_OBJECT_EQUALS(Class defines compareTo(...) and uses Object.equals())
11
ES_COMPARING_STRINGS_WITH_EQ(Comparison of String objects using == or !=)
其实主要是String!="",String==null
12
HE_EQUALS_USE_HASHCODE(Class defines equals() and uses Object.hashCode())
一个类覆写了equals方法,没有覆写hashCode方法,使用了Object对象的hashCode方法
13
ITA_INEFFICIENT_TO_ARRAY(Method uses toArray() with zero-length array argument)
(String[]) header.toArray(new String[0]); -->(String[]) list.toArray(new String[list.size]);
这么写可以少new一个数组,前面的写法先new一个空数组,然后在new一个和list大小一致的数组,后一个写法直接new一个和list大小一致的数组
14
MS_SHOULD_BE_FINAL
static 常量前面加上final
15
NM_CLASS_NAMING_CONVENTION(Class names should start with an upper case letter)
类名要大写字母开头
16
RCN_REDUNDANT_COMPARISON_OF_NULL_AND_NONNULL_VALUE(Redundant comparison of non-null value to null)
方法中包含一个不能为空的赋值还包含一个可以为空的赋值。冗余比较非空值为空
17
RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE(Redundant nullcheck of value known to be non-null)
方法中对不为空的值进行为空的判断(这里findbugs检测的不准确)
18
RI_REDUNDANT_INTERFACES(Class implements same interface as superclass)
主要Serializable,父类和子类多实现了Serializable接口
19
SBSC_USE_STRINGBUFFER_CONCATENATION(Method concatenates strings using + in a loop)
String在循环中做+操作,推荐使用StringBuffer代替
20
SF_SWITCH_NO_DEFAULT(Switch statement found where default case is missing)
使用switch的时候没有了default
21
UPM_UNCALLED_PRIVATE_METHOD(Private method is never called)
定义的私有方法没有被调用过
22
WMI_WRONG_MAP_ITERATOR(This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.)
Map<String,String> map = new HashMap<String,String>();
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
map.put("d", "d");
//avoid
for(Iterator<String> it = map.keySet().iterator();it.hasNext(); ){
String key = it.next();
String value = map.get(key);
System.out.println("key:"+key+",value:"+value);
}
//better
for(Iterator<Entry<String,String>> it = map.entrySet().iterator();it.hasNext();){
Entry<String,String> e = it.next();
System.out.println("key:"+e.getKey()+",value:"+e.getValue());
}