FindBugs使用
FindBugs简介
FindBugs是一款静态分析工具,使用它可以查出代码编写过程中一些不规范和错误的地方。
下载地址
安装步骤
1、解压压缩包
2、将解压出的文件findbugs-3.0.1拷贝到Eclipse安装目录plugins文件夹下
3、重启Eclipse工具,FindBugs生效
4、检查FindBugs是否安装成功,以下两种方法皆可检验
(1)在Eclipse中,Window-->Preferences 搜索FindBugs 若出现搜索结果则表明安装成功
(2) 在项目中右键出现FindBugs选项也表示安装成功
使用方法
1、右键项目,选择FindBugs
2、根据提示解决相应问题
错误代码
以下是FindBugs一些常见的错误代码对应解释及将解决办法,可以参考
序号 |
问题英文描述 |
问题分析 |
解决办法 |
1 |
Comparison of String objects using == or != |
比较字符串使用了双等号 |
使用String的equels()方法 |
2 |
Call to equals() comparing different types |
Equals方法比较了两个不同的数据类型 |
转换类型后比较 |
3 |
Call to method of static java.text.DateFormat |
同一个format多次调用会导致线性不安全, private static final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 方法体内调用: format.format(new Date()); |
在方法体内使用,尽量避免定义全局的对象 format.format(new Date()); |
4 |
Bad attempt to compute absolute value of signed 32-bit random integer |
String nums = Math.abs(r.nextInt()) + ""; |
修改为String nums = Math.abs(r.nextInt(Integer.MAX_VALUE)) + ""; |
5 |
The class name com.aisino.fpcxbd.action.printAction doesn't start with an upper case letter |
类名首字母大写 |
类名首字母大写 |
6 |
Dead store to rePacket |
不被使用的变量或常量 |
看业务是否会使用,如果不使用,请删除,如果有使用,初始值不要直接new 一个对象,初始值可以置为null |
7 |
Exception is caught when Exception is not thrown |
没有抛出异常时,异常被捕获
|
结合业务进行处理 |
8 |
Load of known null value |
填充了空值 |
如果没有的空值,请删除 |
9 |
Method invokes inefficient new String(String) constructor |
方法中调用了低效的new String()构造方法,如 context.put("name",new String("Velocity") ); |
如果内容本身为字符串,则直接赋值即可,如 context.put("name","Velocity" ); |
10 |
Method may fail to close stream |
方法可能未关闭stream,方法产生了一个IO流,却未关闭,将会导致文件描绘符的泄漏。 |
建议使用finally block来确保io stream被关闭。如果无异常,请使用完毕后关闭IO流。 |
11 |
Method might ignore exception |
捕捉了异常,但是没有进行处理 |
需要在catch体中对异常进行处理。或者打印相关的描述。 |
12 |
Method names should start with a lower case letter |
方法首字母没有小写, 没有遵循Java命名规范 |
请将方法首字母小写 |
13 |
Method uses the same code for two branches |
例如: fpkjmx.setSyl(fpkjmx.getSyl()); |
请确认该写法是否有意义。 |
14 |
Non-transient non-serializable instance field in serializable class |
在可序列化的类中存在不能序列化或者不能暂存的数据
|
将属性对象实现可实例化 |
15 |
Null pointer dereference |
会出现空指针,如 if(null != nsrxx){ }else{ nsrxx.setLoginZt(3); } |
这种逻辑有明显的错误,请根据业务修改 |
16 |
Nullcheck of value previously dereferenced |
会出现空指针异常 |
先做非空判断,再进行业务逻辑的处理,避免空指针异常 |
17 |
Possible null pointer dereference |
可能会出现空指针异常 |
请根据业务处理,如有必要先做非空判断 |
18 |
Possible null pointer dereference in method on exception path |
在异常部分放弃null值检查,可能会导致后面的代码出现空指针异常 |
请根据业务进行处理 |
19 |
Redundant nullcheck of value known to be non-null |
该对象已不为空,没有必要再做非空判断 |
没有必要的判断去掉即可,(根据实际业务) |
20 |
Redundant nullcheck of value known to be non-null |
已知该对象为空,没有必要再做非空判断 |
已知该对象为空,没有必要再做非空判断 |
21 |
Repeated conditional tests |
重复条件测试 |
重复的判断,请去掉一个 |
22 |
Self assignment of local variable |
自赋值的局部变量 |
请检查代码,是否可以去掉自赋值 |
23 |
Should be a static inner class |
应该定义为静态内部类 |
如果出现该提示,请定义为静态内部类 |
24 |
Store of non serializable object into HttpSession |
在HttpSession中存放非序列化的对象
|
将Javabean实现可实例化 |
25 |
Unread field |
未被使用的变量或对象 |
请查看代码,如果没有引用,请删除或者注释掉 |