FindBugs使用手册.docx
- 文档编号:17607478
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:17
- 大小:390.79KB
FindBugs使用手册.docx
《FindBugs使用手册.docx》由会员分享,可在线阅读,更多相关《FindBugs使用手册.docx(17页珍藏版)》请在冰点文库上搜索。
FindBugs使用手册
FindBugs 使用手册
文件编号:
配置项编号:
FindBugs 使用手册
文档版本号
V 1.0
农信银资金清算中心创新研发部
未经农信银资金清算中心的书面许可,不得翻印或外传- 1/10 -
编
号
章节
名称
修订内容简述
修订日期
版本
号
修订人
批准人
1
创建《FindBugs 使用手
册》
2011-07-12
V 1.0
李远卓
FindBugs 使用手册
未经农信银资金清算中心的书面许可,不得翻印或外传- 2/10 -
FindBugs 使用手册
1、FindBugs 简介..............................................................................................................4
2、FindBugs 的检查规则........................................................................................................4
2.1Correctness(正确性) ......................................................................................4
2.2Bad practice(不良实践) .................................................................................5
2.3Performance(性能) ........................................................................................5
2.4Multithreaded correctness(多线程正确性)..................................................6
2.5Dodgy(不可靠) ...............................................................................................7
3、FindBugs 使用....................................................................................................................8
3.1安装 FindBugs .....................................................................................................8
3.2配置 FindBugs .....................................................................................................8
3.3使用 FindBugs .....................................................................................................8
未经农信银资金清算中心的书面许可,不得翻印或外传- 3/10 -
FindBugs 使用手册
1、FindBugs 简介
FindBugs 是用于 java 代码检查的一种静态分析工具,它检查类或者 JAR 文件,将字节
码与一组缺陷模式进行对比以发现可能的问题。
有了静态分析工具,就可以在不实际运行
程序的情况对软件进行分析。
FindBugs 专注于找出潜在程序错误,而不是编码风格问题,目的在于提高程序的健壮性。
2、FindBugs 的检查规则
FindBugs 提出了超过 200 种规则,这些规则可主要分为如下类别:
2.1 Correctness(正确性)
这些问题涉及到可能在某些方面不正确的代码。
如:
代码有无限递归,或者读取为写
入的字段,这类问题几乎无疑是程序的错误。
例 1:
使用未初始化的类成员,可能导致 NullPointException
代码:
public class FindBugsTest {
private List items;
public void addItem(String item) {
items.add(item);
}
}
FindBugs 检测结果:
Bug:
Read of unwritten field items
Pattern id:
NP_UNWRITTEN_FIELD, type:
NP, category:
CORRECTNESS
The program is dereferencing a field that does not seem to ever have
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:
com.nxy.test.FindBugsTest.testString() ignores return value of
String.replace(char, char)
Pattern id:
RV_RETURN_VALUE_IGNORED, type:
RV, category:
CORRECTNESS
未经农信银资金清算中心的书面许可,不得翻印或外传- 4/10 -
FindBugs 使用手册
The 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 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 = 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.printStackTrace();
}
}
FindBugs 检测结果:
Bug:
com.nxy.test.FindBugsTest.testFileNotClosed() may fail to close
stream
Pattern id:
OS_OPEN_STREAM, type:
OS, category:
BAD_PRACTICE
The 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 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.
未经农信银资金清算中心的书面许可,不得翻印或外传- 5/10 -
FindBugs 使用手册
2.3 Performance(性能)
这类规则的目的在于检测潜在的性能问题。
如:
代码创建了不需要的对象,或者在循
环中使用字符串连接而不是使用 StringBuffer。
例 4:
使用 new String(String)构造函数创建字符串
代码:
String str = new String("string");
FindBugs 检测结果:
Bug:
com.nxy.test.FindBugsTbineString() invokes inefficient
new String(String) constructor
Pattern id:
DM_STRING_CTOR, type:
Dm, category:
PERFORMANCE
Using the java.lang.String(String) constructor wastes memory because
the object so constructed will be functionally indistinguishable from
the String passed as 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 com.nxy.test.FindBugsTest() invokes Thread.start()
Pattern id:
SC_START_IN_CTOR, type:
SC, category:
MT_CORRECTNESS
The 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;
}
未经农信银资金清算中心的书面许可,不得翻印或外传- 6/10 -
FindBugs 使用手册
public void setName(String name) {
this.name = name;
}
FindBugs检测结果:
Bug:
Inconsistent synchronization of com.nxy.test.FindBugsTest.name;
locked 50% of time
Pattern id:
IS2_INCONSISTENT_SYNC, type:
IS, category:
MT_CORRECTNESS
The fields of this class appear to be accessed inconsistently with
respect to synchronization.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)
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 show 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 accurate 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 检测结果:
未经农信银资金清算中心的书面许可,不得翻印或外传- 7/10 -
FindBugs 使用手册
Bug:
Dead store to name
Pattern id:
DLS_DEAD_LOCAL_STORE, type:
DLS, category:
STYLE
This 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 generates 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 路径下,重启 eclipse
3.2 配置 FindBugs
进入如下目录:
Project → Properties,如下图所示:
说明:
选中‘Enable Project specific settings’为特定的项目详细配置 FindBugs。
选中‘Run automatically’,在每次对类进行修改后,FindBugs 将自动检查问题。
‘Detectordetails’项会给出被选中 Detector 的相信说明,包括该 Detector 的检测规
则,所属模式等信息。
‘Reporter Configuration’定义了报告错误的级别,默认情况分别为‘Medium’,同时
定义了错误类别,默认情况下为前面所列的几种主要类别。
未经农信银资金清算中心的书面许可,不得翻印或外传- 8/10 -
FindBugs 使用手册
3.3 使用 FindBugs
点击‘OK’,FindBugs 开始运行:
可以通过‘Problems(问题)’视图查看 FindBugs 的检查结果,在该视图中,FindBugs
检测出来的问题和其他错误和警告一起列出。
要查看某个 FindBugs 检查结果的详细信息,使用如下方法:
在源代码中点击“红色小
虫”标记,将在‘Properties(属性)’视图中显示出该问题的详细信息。
如下图所示:
未经农信银资金清算中心的书面许可,不得翻印或外传- 9/10 -
FindBugs 使用手册
使用 Filter 选择地禁用规则
可以有不同的方式定义过滤器:
匹配一个类的过滤器。
可以用这些过滤器 忽略在特定类中发现的所有问题。
匹配一个类中特定缺陷代码(bugcode)的过滤器。
可以用这些过滤器忽略在特定类
中发现的一些缺陷。
匹配一组缺陷的过滤器。
可以用这些过滤器 忽略所分析的所有类中的一组缺陷。
匹配所分析的一个类中的某些方法的过滤器。
可以用这些过滤器忽略在一个类中的一
组方法中发现的所有缺陷。
匹配在所分析的一个类中的方法中发现的某些缺陷的过滤器。
可以用这些过滤器忽略
在一组方法中发现的特定缺陷。
未经农信银资金清算中心的书面许可,不得翻印或外传- 10/10 -
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- FindBugs 使用手册