FindBugs使用手册_第1页
FindBugs使用手册_第2页
FindBugs使用手册_第3页
FindBugs使用手册_第4页
FindBugs使用手册_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文件编号:配置项编号:FindBugs使用手册文档版本号V 1.0农信银资金清算中心创新研发部文档修订记录编号章节名称修订内容简述修订日期版本号修订人批准人1创建FindBugs使用手册2011-07-12V 1.0李远卓目录1、FindBugs简介42、FindBugs的检查规则42.1Correctness(正确性)42.2Bad practice(不良实践)52.3Performance(性能)52.4Multithreaded correctness(多线程正确性)62.5Dodgy(不可靠)73、FindBugs使用83.1安装FindBugs83.2配置FindBugs83.3使用

2、FindBugs81、 FindBugs简介FindBugs是用于java代码检查的一种静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。FindBugs专注于找出潜在程序错误,而不是编码风格问题,目的在于提高程序的健壮性。2、FindBugs的检查规则FindBugs提出了超过200种规则,这些规则可主要分为如下类别:2.1 Correctness(正确性)这些问题涉及到可能在某些方面不正确的代码。如:代码有无限递归,或者读取为写入的字段,这类问题几乎无疑是程序的错误。例1:使用未初始化的类

3、成员,可能导致NullPointException代码:public class FindBugsTest private List items;public void addItem(String item) items.add(item);FindBugs检测结果:Bug: Read of unwritten field itemsPattern id: NP_UNWRITTEN_FIELD, type: NP, category: CORRECTNESSThe program is dereferencing a field that does not seem to ever have

4、 a non-null value written to it. Dereferencing this value will generate a null pointer exception. 例2:不使用方法的返回值代码:String aString = "bob"aString.replace('b', 'p');FindBugs检测结果:Bug: () ignores return value of String.replace(char, char)Pattern id: RV_RETURN_VALUE_IGNORED, type:

5、 RV, category: CORRECTNESSThe return value of this method should be checked. One common cause of this warning is to invoke a method on an immutable object, thinking that it updates the object. For example, in the following code fragment,String dateString = getHeaderField(name);dateString.trim();the

6、programmer seems to be thinking that the trim() method will update the String referenced by dateString. But since Strings are immutable, the trim() function returns a new String value, which is being ignored here. The code should be corrected to: String dateString = getHeaderField(name);dateString =

7、 dateString.trim();2.2 Bad practice(不良实践)这类问题明确违反建议的编程标准。如:删除异常,或未关闭文件,或未数据库连接资源等。例3:未关闭打开的文件输出流资源代码:public void testFileNotClosed() try FileOutputStream fos = new FileOutputStream("D:test.txt");fos.write(0); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.prin

8、tStackTrace(); FindBugs检测结果:Bug: () may fail to close streamPattern id: OS_OPEN_STREAM, type: OS, category: BAD_PRACTICEThe method creates an IO stream object, does not assign it to any fields, pass it to other methods that might close it, or return it, and does not appear to close the stream on all

9、 paths out of the method.  This may result in a file descriptor leak.  It is generally a good idea to use a finally block to ensure that streams are closed.2.3 Performance(性能)这类规则的目的在于检测潜在的性能问题。如:代码创建了不需要的对象,或者在循环中使用字符串连接而不是使用StringBuffer。例4:使用new String(String)构造函数创建字符串代码:String str = new

10、 String("string");FindBugs检测结果:Bug: () invokes inefficient new String(String) constructorPattern id: DM_STRING_CTOR, type: Dm, category: PERFORMANCEUsing the (String) constructor wastes memory because the object so constructed will be functionally indistinguishable from the String passed a

11、s a parameter.  Just use the argument String directly.2.4 Multithreaded correctness(多线程正确性)这是一类特殊的问题,涉及到同步和多线程代码有关的问题。例5:在构造方法中start线程代码:public FindBugsTest() Thread thread = new Thread();thread.start();FindBugs检测结果:Bug: new () invokes Thread.start()Pattern id: SC_START_IN_CTOR, type: SC, categ

12、ory: MT_CORRECTNESSThe constructor starts a thread. This is likely to be wrong if the class is ever extended/subclassed, since the thread will be started before the subclass constructor is started.例6:同一成员变量的getter和setter方法的同步性不统一代码:private String name;public synchronized String getName() return name

13、;public void setName(String name) = name;FindBugs检测结果:Bug: Inconsistent synchronization of com.nxy.test.FindBugsT; locked 50% of timePattern id: IS2_INCONSISTENT_SYNC, type: IS, category: MT_CORRECTNESSThe fields of this class appear to be accessed inconsistently with respect to sy

14、nchronization.  This bug report indicates that the bug pattern detector judged that 1、The class contains a mix of locked and unlocked accesses, 2、At least one locked access was performed by one of the class's own methods, and 3、The number of unsynchronized field accesses (reads and writes)

15、was no more than one third of all accesses, with writes being weighed twice as high as reads A typical bug matching this bug pattern is forgetting to synchronize one of the methods in a class that is intended to be thread-safe.You can select the nodes labeled "Unsynchronized access" to sho

16、w the code locations where the detector believed that a field was accessed without synchronization.Note that there are various sources of inaccuracy in this detector; for example, the detector cannot statically detect all situations in which a lock is held.  Also, even when the detector is accu

17、rate in distinguishing locked vs. unlocked accesses, the code in question may still be correct.2.5 Dodgy(不可靠)这类问题涉及奇怪的代码。如:未使用的本地变量或未检查的类型转换(cast)。例7:定义了未被使用的变量代码:Person person = (Person) aMap.get("bob");String name = person.getName();/后续代码不曾使用过本地变量name.FindBugs检测结果:Bug: Dead store to name

18、Pattern id: DLS_DEAD_LOCAL_STORE, type: DLS, category: STYLEThis instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used. Note that Sun's javac compiler often genera

19、tes dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives. 3、FindBugs使用3.1 安装FindBugs可以以多种方式运行 FindBugs从 GUI、从命令行、使用 Ant、作为 Eclipse 插件程序和使用 Maven。这里重点介绍FindBugs作为 Eclipse 插件程序的使用方法。从internet上下载FindBugs的eclipse plugin 压缩包;将plugin解压至$ECLIPSE_ROOT$/plugins路径下,重启eclipse3.2 配置FindBugs进入如下目录:Project à Properties,如下图所示:说明:选中Enable Project specific

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论