白盒測試靜态檢查--java經典bug
經過一段時間白盒靜态掃描,目前已經掃了兩個包,發現了100多個bug.
經典案例。
如下清單展示
Type
headline
demo
Comment
1
多線程
單例action裡面包含有屬性并且允許讀寫
public class XxxAction extends Action{
private StringBuffer vo = new StringBuffer();
public void setVo(StringBuffer invo){
vo=invo;
}
public StringBuffer getVo(){
return this.vo;
.................
單例action裡面包含有屬性進行讀寫
會引起資料錯亂
2
Bad practice
字元串比較用==或者=
if(dao.getADTerminalByServiceCode(serviceCode).getServicecode() == serviceCode){
String類型的比較不要用==或者!=,應該用equal方法
就上面的代碼永遠都為false,因為==表示在同一常量區才會為true
3
字元串分割正規表達式使用不對
String[] area = areaSize.split("*");
正規表達式使用不對,會一直抛出異常
應該使用轉義字元
4
字元串比較的空點異常
vo.getVirtualFlag().equals("NO")
建議修改為"NO".equals(vo.getVirtualFlag())
5
對象比較不對
if (this == obj) {
return true;
}
對象必須重再了hashcode方法,比較才有意義
6
流沒關閉
InputStream input = new BufferedInputStream(file.getInputStream());
流沒有關閉
會造成檔案句柄資源耗盡,那麼就無法再打開新的流
7
connection statement rs等沒關閉
try{
.....
} catch(..){
}finally
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
在rs有異常會關閉不了statement conn
在statement有異常會關閉不了conn
8
<a>Performance</a>
循環中建立字元串
String children = "" ;
ClassSpecVO[] t = cs.getTree();
if ( t == null )
return "";
for ( int i = 0 ; i < t.length ; i ++ )
{
if ( children.length() != 0 )
children += ",";
children += constructJsonString(t[i]);
children會循環建立對象,必須改為StringBuffer對象。用append進行連接配接字元串
9
不應該建立字元串對象
String vsplist=new String();
修改為高效的構造方法String vsplist=“”
“”的字元串會在常量區不用建立對象
本文轉自hcy's workbench部落格園部落格,原文連結:http://www.cnblogs.com/alterhu/archive/2012/03/14/2395696.html,如需轉載請自行聯系原作者。