回来顶部

FindBugs怎样运用静态剖析东西的教程办法

发布时刻:2017-03-12 17:21:58来历:体系盒阅读数:
  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"子目录下,这样就能够在 /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.

  奇偶检测逻辑,未考虑负数状况.


ghost win8 | win7 64位旗舰版 | 体系下载
本站发布的电脑体系及电脑软件仅用于人个测验学习运用,不得运用于任何商业用处,请在下载后24小时内删去,请支撑正版软件。
如侵犯到您的权益,请及时告诉咱们,咱们会及时处理。邮箱:463439782#qq.com[请将#换成@]
Copyright @ 2014 体系盒