版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
FindBugs常見問題指南1
ComparisonofStringobjectsusing==or!=例,overrideequals方法時(shí)容易犯錯(cuò)Java代碼
\o"收藏這段代碼"if(this.topic
!=
key.getTopic())
return
false;2DeadstoretonewStatusRecord
定義局部變量后沒有引用3InvocationoftoStringonvalues
直接調(diào)用數(shù)組的toString方法Java代碼
\o"收藏這段代碼"public
Query
createQuery(String
hql,
Object
values[],Session
session){
logger.debug(values);
logger.debug((new
StringBuilder()).append("hql=[").append(hql).append("]
").append(((Object)
}
正確的例子,調(diào)用Arrays.toString()和Arrays.deepToString()方法。Java代碼
\o"收藏這段代碼"
import
java.util.Arrays;
class
A{
}
class
B{
@Override
public
String
toString()
{
return
"BBBBB";
}
}
public
class
Test
{
public
static
void
main(String[]
args)
{
Object
[]
a
=
{new
Integer(0),new
Boolean(true),true,new
A(),new
B()};
Object[][]b
={{new
A(),new
B()},{new
A(),new
B()},{new
A(),new
B()}};
System.out.println(Arrays.deepToString(b));
}
}
4ignoresexceptionalreturnvalueofjava.io.File.mkdirs()
忽略了返回值,應(yīng)當(dāng)含有返回值Java代碼
\o"收藏這段代碼"public
void
initFolder()
{
(!exitDir.isDirectory())
{
exitDir.mkdirs();
("===Finishing
create
exit
trade
image
folder!====");
}
Thismethodreturnsavaluethatisnotchecked.Thereturnvalueshouldbecheckedsinceitcanindicateanunusualorunexpectedfunctionexecution.Forexample,theFile.delete()methodreturnsfalseifthefilecouldnotbesuccessfullydeleted(ratherthanthrowinganException).Ifyoudon'tchecktheresult,youwon'tnoticeifthemethodinvocationsignalsunexpectedbehaviorbyreturninganatypicalreturnvalue.5不使用newString()定義空的字符串Java代碼
\o"收藏這段代碼"String
alarmCodeCond
=
new
String();
應(yīng)當(dāng)
Java代碼
\o"收藏這段代碼"String
alarmCodeCond
=
"";
例:
Java代碼
\o"收藏這段代碼"public
void
method1()
{
String
ip
=
null;
try
{
ip
=
InetAddress.getLocalHost().getHostAddress();
}
catch
(UnknownHostException
e)
{
e.printStackTrace();
}
long
ipCount
=
countIpAddress(ip);
//
可能會(huì)傳入空引用
//...
}
long
countIpAddress(String
ip)
{
long
ipNum
=
0;
String[]
ipArray
=
ip.split("\\.");
}
修改后:Java代碼
\o"收藏這段代碼"public
void
method1()
{
String
ip
=
null;
try
{
ip
=
InetAddress.getLocalHost().getHostAddress();
}
catch
(UnknownHostException
e)
{
e.printStackTrace();
}
long
ipCount
=
countIpAddress(ip);
//
可能會(huì)傳入空引用
//...
}
long
countIpAddress(String
ip)
{
long
ipNum
=
0;
if
(ip
==
null)
{
return
0;
//或者拋出異常
}
String[]
ipArray
=
ip.split("\\.");
//...
}
注意:函數(shù)入口需要交驗(yàn)入?yún)⒌暮戏ㄐ浴?3Methodconcatenatesstringsusing+inaloop
在循環(huán)里使用字符串連接,效率低,應(yīng)該使用StringBuilder/StringBuffer
例:
Java代碼
\o"收藏這段代碼"String
writeData
=
"";
for
(int
i
=
0;
i
<
10;
i++)
{
writeData
=
writeData
+
"a";
}
14Methodmayfailtoclosedatabaseresource
沒有釋放數(shù)據(jù)庫資源Java代碼
\o"收藏這段代碼"public
ResultSet
callProcedure(String
procedure)
{
Session
ses
=
getSessionForUpdate();
ResultSet
rs
=
null;
try
{
Connection
conn
=
ses.connection();
conn.setAutoCommit(false);
CallableStatement
statement
=
conn.prepareCall(procedure);
//may
fail
to
close
CallableStatement
rs
=
statement.executeQuery();
mit();
}
catch
(Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
ses.close();
}
catch
(SQLException
e)
{
throw
e;
}
}
return
rs;
}
應(yīng)當(dāng)修改為:
Java代碼
\o"收藏這段代碼"public
ResultSet
callProcedure(String
procedure)
{
Session
ses
=
getSessionForUpdate();
ResultSet
rs
=
null;
CallableStatement
statement
=
null;
try
{
Connection
conn
=
ses.connection();
conn.setAutoCommit(false);
statement
=
conn.prepareCall(procedure);
rs
=
statement.executeQuery();
mit();
}
catch
(Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
statement.close();
ses.close();
}
catch
(SQLException
e)
{
e.printStackTrace();
}
}
return
rs;
}
15Methodmayfailtoclosestream
沒有關(guān)閉流,可能會(huì)導(dǎo)致文件描述符泄露,應(yīng)該在finally中關(guān)閉
例:Java代碼
\o"收藏這段代碼"try
{
FileInputStream
in
=
new
FileInputStream(file);
InputStreamReader
inputStreamReader
=
new
InputStreamReader(in);
BufferedReader
reader
=
new
BufferedReader(inputStreamReader);
//...
in.close();
inputStreamReader.close();
reader.close();
}
catch
(IOException
e)
{
}
修改為:Java代碼
\o"收藏這段代碼"FileInputStream
in
=
null;
InputStreamReader
inputStreamReader
=
null;
BufferedReader
reader
=
null;
try
{
in
=
new
FileInputStream(file);
inputStreamReader
=
new
InputStreamReader(in);
reader
=
new
BufferedReader(inputStreamReader);
//
...
}
catch
(IOException
e)
{
}
finally
{
try
{
in.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
try
{
inputStreamReader.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
try
{
reader.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
16Methodmightignoreexception
Thismethodmightignoreanexception.
Ingeneral,exceptionsshouldbehandledorreportedinsomeway,ortheyshouldbethrownoutofthemethod.應(yīng)該將異常處理、打印或者拋出反例:Java代碼
\o"收藏這段代碼"try
{
//...
}
catch
(Exception
e)
{
}
17Classdefinesnon-transientnon-serializableinstancefieldreaderTypeInfo
一個(gè)實(shí)現(xiàn)了Serializable接口的類,含有非transient和非serializable的實(shí)例對象域。
ThisSerializableclassdefinesanon-primitiveinstancefieldwhichisneithertransient,Serializable,orjava.lang.Object,anddoesnotappeartoimplementtheExternalizableinterfaceorthereadObject()andwriteObject()methods.
Objectsofthisclasswillnotbedeserializedcorrectlyifanon-Serializableobjectisstoredinthisfield.18Nullcheckofvaluepreviouslydereferenced
前面獲取的對象,現(xiàn)在引用的時(shí)候沒有交驗(yàn)是否為null反例:Java代碼
\o"收藏這段代碼"Reader
reader
=
null;
try
{
reader
=
this.getReaderByName(readerBasicInfo.getByName());
}
catch
(Exception
e1)
{
e1.printStackTrace();
return
ReaderStateConst.FAIL;
}
DependenceRelation
dependenceRelation
=
new
DependenceRelation();
dependenceRelation.setDescription(reader.getIpAddress());
//
使用前沒有做null校驗(yàn)
19
Possiblenullpointerdereference
可能存在的空引用
Java代碼
\o"收藏這段代碼"capInfo
=
wrapper.wrapperToClient((ReaderCapabilities)
object);
try
{
if
(capInfo
!=
null)
{
transactionDs
.saveReaderCapabilityCom((ReaderCapabilities)
object);
}
}
catch
(RuntimeException
e)
{
capInfo.setDetailMsg(ReaderStateConst.DB_OPT_FAIL);
return
capInfo;
}
capInfo.setDetailMsg(ReaderStateConst.SUCCESSFUL);
//capInfo可能為null
20引用前需要做空校驗(yàn)Java代碼
\o"收藏這段代碼"public
synchronized
void
remove(String
batNo,
int
count)
{
List<Task>
taskList
=
commandMap.get(batNo);
synchronized
(taskList)
{
//使用前需要作null
check
//...
}
}
21Possiblenullpointerdereferenceinmethodonexceptionpath例Java代碼
List<District>
districts
=
null;
try
{
districts
=
this.getDistricts(ReaderConst.DESC);
}
catch
(Exception
e)
{
e.printStackTrace();
}
if
(start
>=
districts.size())
{
//districts
可能是null
tableData.setTotalCount(0);
return
tableData;
}
22內(nèi)部類沒有引用外部類的屬性/方法的時(shí)候,應(yīng)該作為靜態(tài)內(nèi)部類。Thisclassisaninnerclass,butdoesnotuseitsembeddedreferencetotheobjectwhichcreatedit.
Thisreferencemakestheinstancesoftheclasslarger,andmaykeepthereferencetothecreatorobjectalivelongerthannecessary.
Ifpossible,theclassshouldbemadestatic.
23包裝類的比較應(yīng)該使用eueqls,要比較值類型,需要強(qiáng)制類型轉(zhuǎn)換后再使用。Thismethodcomparestworeferencevaluesusingthe==
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026云南怒江州教育體育系統(tǒng)引進(jìn)緊缺學(xué)科教師16人考試備考題庫附答案
- 阿壩師范學(xué)院2025年下半年公開選調(diào)工作人員(1人)參考題庫必考題
- 2026河北唐山遵化坤桐醫(yī)院招聘衛(wèi)生專業(yè)技術(shù)人員參考題庫必考題
- 2025年遵義市綏陽縣衛(wèi)生健康局選聘鄉(xiāng)村醫(yī)生筆試真題
- 2025年西藏?zé)煵菡衅缚荚囌骖}
- 2025山東省體育局所屬事業(yè)單位招聘63人備考題庫及完整答案詳解一套
- 2026中國科學(xué)院西北高原生物研究所第一批科研崗位招聘22人備考題庫(青海)及答案詳解(考點(diǎn)梳理)
- 2026中信銀行招聘3人備考題庫有完整答案詳解
- 2025廣西百色市西林縣民政局招聘編外聘用人員(補(bǔ)招聘)1人備考題庫及答案詳解(考點(diǎn)梳理)
- 行星科普教學(xué)課件
- 七年級數(shù)學(xué)上冊期末試卷及答案(多套題)
- 2023年P(guān)CB工程師年度總結(jié)及來年計(jì)劃
- 2024年度初會(huì)《初級會(huì)計(jì)實(shí)務(wù)》高頻真題匯編(含答案)
- 績效考核和薪酬方案通用模板
- YY/T 0590.1-2018醫(yī)用電氣設(shè)備數(shù)字X射線成像裝置特性第1-1部分:量子探測效率的測定普通攝影用探測器
- GB/T 16927.1-2011高電壓試驗(yàn)技術(shù)第1部分:一般定義及試驗(yàn)要求
- 政府會(huì)計(jì)準(zhǔn)則優(yōu)秀課件
- 陣發(fā)性室性心動(dòng)過速課件
- 無機(jī)與分析化學(xué)理論教案
- 名詞性從句 講義-英語高考一輪復(fù)習(xí)語法部分
- T∕ZZB 2722-2022 鏈板式自動(dòng)排屑裝置
評論
0/150
提交評論