Findbugs如何使用?Findbugs的使用说明
findbugs使用说明
1 用途
findbugs 是一个java bytecode静态分析工具,它可以帮助java工程师提高代码质量以及排除隐含的缺陷。
findbugs检查类或者 jar 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。
有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。findbugs不是通过分析类文件的形式或结构来确定程序的意图,而是通常使用 visitor 模式进行分析(visitor 模式的更多信息)。
2 安装
目前findbugs最新的版本是1.3.9,
2.1 eclipse插件的安装
环境要求,findbugs要求eclipse 3.4 以上的版本,jre/jdk 1.5.0以上的版本。
步骤,将edu.umd.cs.findbugs.plugin.eclipse_1.3.9.20090821.zip解压到eclipse的 "plugins"子目录下,这样就可以在 <eclipse_install_dir>/plugins/edu.umd.cs.findbugs.plugin.eclipse_1.3.9.20090821/下看到findbugs logo图片findbugs.png。
启动eclipse 然后选择 help → about eclipse platform → plug-in details,你应该找到 "findbugs plug-in"。
3 使用
启动
选中java工程,点击鼠标右键,选择名为“find bugs”的菜单,findbugs开始运行,问题指示器将指向根据bug模式识别出来的潜在问题代码位置。
可选项定制
你还可以通过java工程的属性对话框来定制findbugs的运行方式,可选项包括:
控制"run findbugs automatically" 开关的checkbox。 选中时, findbugs 将在每次修改java类后启动运行。
选择最小告警优先级和bug类别。这些选项将选择哪些警告被显示。例如,如果你选择"medium",只有medium 和 high priority 警告将被显示。近似地,如果你未选中 "style" checkbox,style类的警告信息将不会被显示。
选择探测器。这个列表允许你选择你想在工程中使用的探测器。
4 配套的bug模式解释
为了有针对性的使用这个工具,减少bug的误报,提高使用效率,我们选择了10个左右的bug模式,下面就是对这10个模式的解释。
这些bug可能会引起程序的性能或逻辑问题.
需要说明的是,findbugs能检测的bug pattern远不仅于此,甚至可以定制自己的探测器,因此,这个文档会不断扩充,同时,也欢迎大家不断探索和分享使用实践.
4.1 es_comparing_parameter_string_with_eq
es: comparison of string parameter using == or != (es_comparing_parameter_string_with_eq)
this code compares a java.lang.string parameter for reference equality using the == or != operators. requiring callers to pass only string constants or interned strings to a method is unnecessarily fragile, and rarely leads to measurable performance gains. consider using the equals(object) method instead.
使用 == 或者 != 来比较字符串或interned字符串,不会获得显著的性能提升,同时并不可靠,请考虑使用equals()方法。
4.2 he_equals_no_hashcode
he: class defines equals() but not hashcode() (he_equals_no_hashcode)
this class overrides equals(object), but does not override hashcode(). therefore, the class may violate the invariant that equal objects must have equal hashcodes.
类定义了equals()方法但没有重写hashcode()方法,这样违背了相同对象必须具有相同的hashcodes的原则
4.3 it_no_such_element
it: iterator next() method can't throw nosuchelement exception (it_no_such_element)
this class implements the java.util.iterator interface. however, its next() method is not capable of throwing java.util.nosuchelementexception. the next() method should be changed so it throws nosuchelementexception if is called when there are no more elements to return.
迭代器iterator无法抛出nosuchelement异常,类实现了java.util.iterator接口,但是next()方法无法抛出java.util.nosuchelementexception异常,因此,next()方法应该做如此修改,当被调用时,如果没有element返回,则抛出nosuchelementexception异常
4.4 j2ee_store_of_non_serializable_object_into_session
j2ee: store of non serializable object into httpsession (j2ee_store_of_non_serializable_object_into_session)
this code seems to be storing a non-serializable object into an httpsession. if this session is passivated or migrated, an error will result.
将没有实现serializable的对象放到httpsession中,当这个session被钝化和迁移时,将会产生错误,建议放到httpsession中的对象都实现serializable接口。
4.5 odr_open_database_resource
odr: method may fail to close database resource (odr_open_database_resource)
the method creates a database resource (such as a database connection or row set), does not assign it to any fields, pass it to other methods, or return it, and does not appear to close the object on all paths out of the method. failure to close database resources on all paths out of a method may result in poor performance, and could cause the application to have problems communicating with the database.
方法可能未关闭数据库资源,未关闭数据库资源将会导致性能变差,还可能引起应用与服务器间的通讯问题。
4.6 os_open_stream
os: method may fail to close stream (os_open_stream)
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.
方法可能未关闭stream,方法产生了一个io流,却未关闭,将会导致文件描绘符的泄漏,建议使用finally block来确保io stream被关闭。
4.7 dmi_calling_next_from_hasnext
dmi: hasnext method invokes next (dmi_calling_next_from_hasnext)
the hasnext() method invokes the next() method. this is almost certainly wrong, since the hasnext() method is not supposed to change the state of the iterator, and the next method is supposed to change the state of the iterator.
4.8 il_infinite_loop
il: an apparent infinite loop (il_infinite_loop)
this loop doesn't seem to have a way to terminate (other than by perhaps throwing an exception).
明显的无限循环.
4.9 il_infinite_recursive_loop
il: an apparent infinite recursive loop (il_infinite_recursive_loop)
this method unconditionally invokes itself. this would seem to indicate an infinite recursive loop that will result in a stack overflow.
明显的无限迭代循环,将导致堆栈溢出.
4.10 wmi_wrong_map_iterator
wmi: inefficient use of keyset iterator instead of entryset iterator (wmi_wrong_map_iterator)
this method accesses the value of a map entry, using a key that was retrieved from a keyset iterator. it is more efficient to use an iterator on the entryset of the map, to avoid the map.get(key) lookup.
使用了keyset iterator和map.get(key)来获取map值,这种方式效率低,建议使用entryset的iterator效率更高.
4.11 im_bad_check_for_odd
im: check for oddness that won't work for negative numbers (im_bad_check_for_odd)
the code uses x % 2 == 1 to check to see if a value is odd, but this won't work for negative numbers (e.g., (-5) % 2 == -1). if this code is intending to check for oddness, consider using x & 1 == 1, or x % 2 != 0.
奇偶检测逻辑,未考虑负数情况.
下一篇: 如何快速找回一起作业的账号和密码?