FindBugs使用手册教案资料.docx

上传人:b****1 文档编号:1156208 上传时间:2022-10-18 格式:DOCX 页数:8 大小:192.05KB
下载 相关 举报
FindBugs使用手册教案资料.docx_第1页
第1页 / 共8页
FindBugs使用手册教案资料.docx_第2页
第2页 / 共8页
FindBugs使用手册教案资料.docx_第3页
第3页 / 共8页
FindBugs使用手册教案资料.docx_第4页
第4页 / 共8页
FindBugs使用手册教案资料.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

FindBugs使用手册教案资料.docx

《FindBugs使用手册教案资料.docx》由会员分享,可在线阅读,更多相关《FindBugs使用手册教案资料.docx(8页珍藏版)》请在冰豆网上搜索。

FindBugs使用手册教案资料.docx

FindBugs使用手册教案资料

文件编号:

配置项编号:

FindBugs使用手册

文档版本号

V1.0

农信银资金清算中心创新研发部

文档修订记录

编号

章节

名称

修订内容简述

修订日期

版本号

修订人

批准人

1

创建《FindBugs使用手册》

2011-07-12

V1.0

李远卓

1、FindBugs简介

FindBugs是用于java代码检查的一种静态分析工具,它检查类或者JAR文件,将字节码与一组缺陷模式进行对比以发现可能的问题。

有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。

FindBugs专注于找出潜在程序错误,而不是编码风格问题,目的在于提高程序的健壮性。

2、FindBugs的检查规则

FindBugs提出了超过200种规则,这些规则可主要分为如下类别:

2.1Correctness(正确性)

这些问题涉及到可能在某些方面不正确的代码。

如:

代码有无限递归,或者读取为写入的字段,这类问题几乎无疑是程序的错误。

例1:

使用未初始化的类成员,可能导致NullPointException

代码:

publicclassFindBugsTest{

privateListitems;

publicvoidaddItem(Stringitem){

items.add(item);

}

}

FindBugs检测结果:

Bug:

Readofunwrittenfielditems

Patternid:

NP_UNWRITTEN_FIELD,type:

NP,category:

CORRECTNESS

Theprogramisdereferencingafieldthatdoesnotseemtoeverhaveanon-nullvaluewrittentoit.Dereferencingthisvaluewillgenerateanullpointerexception.

例2:

不使用方法的返回值

代码:

StringaString="bob";

aString.replace('b','p');

FindBugs检测结果:

Bug:

com.nxy.test.FindBugsTest.testString()ignoresreturnvalueofString.replace(char,char)

Patternid:

RV_RETURN_VALUE_IGNORED,type:

RV,category:

CORRECTNESS

Thereturnvalueofthismethodshouldbechecked.Onecommoncauseofthiswarningistoinvokeamethodonanimmutableobject,thinkingthatitupdatestheobject.Forexample,inthefollowingcodefragment,

StringdateString=getHeaderField(name);

dateString.trim();

theprogrammerseemstobethinkingthatthetrim()methodwillupdatetheStringreferencedbydateString.ButsinceStringsareimmutable,thetrim()functionreturnsanewStringvalue,whichisbeingignoredhere.Thecodeshouldbecorrectedto:

StringdateString=getHeaderField(name);

dateString=dateString.trim();

2.2Badpractice(不良实践)

这类问题明确违反建议的编程标准。

如:

删除异常,或未关闭文件,或未数据库连接资源等。

例3:

未关闭打开的文件输出流资源

代码:

publicvoidtestFileNotClosed(){

try{

FileOutputStreamfos=newFileOutputStream("D:

\test.txt");

fos.write(0);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

FindBugs检测结果:

Bug:

com.nxy.test.FindBugsTest.testFileNotClosed()mayfailtoclosestream

Patternid:

OS_OPEN_STREAM,type:

OS,category:

BAD_PRACTICE

ThemethodcreatesanIOstreamobject,doesnotassignittoanyfields,passittoothermethodsthatmightcloseit,orreturnit,anddoesnotappeartoclosethestreamonallpathsoutofthemethod. Thismayresultinafiledescriptorleak. Itisgenerallyagoodideatouseafinallyblocktoensurethatstreamsareclosed.

2.3Performance(性能)

这类规则的目的在于检测潜在的性能问题。

如:

代码创建了不需要的对象,或者在循环中使用字符串连接而不是使用StringBuffer。

例4:

使用newString(String)构造函数创建字符串

代码:

Stringstr=newString("string");

FindBugs检测结果:

Bug:

com.nxy.test.FindBugsTbineString()invokesinefficientnewString(String)constructor

Patternid:

DM_STRING_CTOR,type:

Dm,category:

PERFORMANCE

Usingthejava.lang.String(String)constructorwastesmemorybecausetheobjectsoconstructedwillbefunctionallyindistinguishablefromtheStringpassedasaparameter. JustusetheargumentStringdirectly.

2.4Multithreadedcorrectness(多线程正确性)

这是一类特殊的问题,涉及到同步和多线程代码有关的问题。

例5:

在构造方法中start线程

代码:

publicFindBugsTest(){

Threadthread=newThread();

thread.start();

}

FindBugs检测结果:

Bug:

newcom.nxy.test.FindBugsTest()invokesThread.start()

Patternid:

SC_START_IN_CTOR,type:

SC,category:

MT_CORRECTNESS

Theconstructorstartsathread.Thisislikelytobewrongiftheclassiseverextended/subclassed,sincethethreadwillbestartedbeforethesubclassconstructorisstarted.

例6:

同一成员变量的getter和setter方法的同步性不统一

代码:

privateStringname;

publicsynchronizedStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

FindBugs检测结果:

Bug:

Inconsistentsynchronizationofcom.nxy.test.FindBugsTest.name;locked50%oftime

Patternid:

IS2_INCONSISTENT_SYNC,type:

IS,category:

MT_CORRECTNESS

Thefieldsofthisclassappeartobeaccessedinconsistentlywithrespecttosynchronization. Thisbugreportindicatesthatthebugpatterndetectorjudgedthat

1、Theclasscontainsamixoflockedandunlockedaccesses,

2、Atleastonelockedaccesswasperformedbyoneoftheclass'sownmethods,and

3、Thenumberofunsynchronizedfieldaccesses(readsandwrites)wasnomorethanonethirdofallaccesses,withwritesbeingweighedtwiceashighasreads

Atypicalbugmatchingthisbugpatternisforgettingtosynchronizeoneofthemethodsinaclassthatisintendedtobethread-safe.

Youcanselect

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 其它考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1