日期:2014-05-16 浏览次数:20374 次
比如我跑了一个Findbugs的report.html结果如下图所示(部分):
?
前面的Code部分就是类型吧。
下面说个例子,比如Findbugs的描述如下:
WMI
??
?Method
com.trilogy.update.persistent.hibernate.UpdateHomeHibernate.updateQuery(UpdateSettings,
Map) makes inefficient use of keySet iterator instead of entrySet
iterator
Bug type WMI_WRONG_MAP_ITERATOR (click for details)
In class com.trilogy.update.persistent.hibernate.UpdateHomeHibernate
In method com.trilogy.update.persistent.hibernate.UpdateHomeHibernate.updateQuery(UpdateSettings, Map)
At UpdateHomeHibernate.java:[line 30]
1.找到源文件line 30附近的代码,如下:
for (Iterator keys = queryParameters.keySet().iterator(); keys.hasNext();) {
??????????? final String key = (String) keys.next();
??????????? final Object value = queryParameters.get(key);
??????????? query.setParameter(key, value);
}
2.根据Code的提示找到解决方案,这里是VMI
(Findbugs分析结果详解:http://hi.baidu.com/zhangna_307/blog/item/00a63658a6c57f3c2934f0e9.html)
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.
3.修改代码这块为:
for(Iterator it = queryParameters.entrySet().iterator(); it.hasNext();) {
?????? ??? ?Map.Entry entry = (Map.Entry)it.next();
?????? ??? ?final String key = (String) entry.getKey();
?????? ??? ?final Object value = entry.getValue();
??????????? query.setParameter(key, value);
}
4.Finish.再跑一下Findbugs,发现这个bug已经没有了。
(Improved Map Iteration http://hanuska.blogspot.com/2006/08/improved-map-iteration_13.html)