FindBugs代码审查插件,apply plugin: 'findbugs'

apply plugin: 'findbugs'

一.什么是FindBugs?

     FindBugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。

  

Chris Grindstaff 写道
   静态分析工具承诺无需开发人员费劲就能找出代码中已有的缺陷。当然,如果有多年的编写经验,就会知道这些承诺并不是一定能兑现。尽管如此,好的静态分析工具仍然是工具箱中的无价之宝.
   代码质量工具的一个问题是它们容易为开发人员提供大量但并非真正问题的问题——即 伪问题(false positives)。出现伪问题时,开发人员要学会忽略工具的输出或者放弃它。FindBugs 的设计者 David Hovemeyer 和 William Pugh 注意到了这个问题,并努力减少他们所报告的伪问题数量。与其他静态分析工具不同,FindBugs 不注重样式或者格式,它试图只寻找真正的缺陷或者潜在的性能问题。

 

 

 

二.FindBugs特性

    1.从一中的概念都是我从google和baidu搜索来的。从这些话我们可以看出: FindBugs只是在一个对你的文件编译后进行.class检查(不是源文件),那么如果你的程序在逻辑上有运行时错误的隐患,那FindBugs是找不到的。

 

   2.目前已包含300多条检测规则.所支持的bugs详情描述:http://findbugs.sourceforge.net/bugDescriptions.html

 

  3.bug的分类:正确性,典型错误,性能,安全等

  4.优先级: 重要,一般,次要

 

三.FindBugs检测举例

    1. 检测器:忽略方法返回值 
   这个检测器查找代码中忽略了不应该忽略的方法返回值的地方。这种情况的一个常见例子是在调用 String 方法时,如下:
 
    

Java代码   收藏代码
  1.    public void  test1() {  
  2.     String a = "bob";  
  3.     <span style="color: #ff0000;">a.replace('b''p');</span>  
  4.   
  5.   
  6.     if(a.equals("pop")){  
  7.             
  8.         System.out.println(a);  
  9.            
  10.      }  
  11.   
  12. }  

 这个错误很常见。在第三行红色代码,程序员认为他已经用 p 替换了字符串中的所有 b。确实是这样,但是他忘记了字符串是不可变的。所有这类方法都返回一个新字符串,而从来不会改变消息的接收者。  

 

使用findbugs检查后,出现的bug信息如下:

写道
Bug: FindBugsDemo.test1() ignores return value of String.replace(char, char) 
Pattern id: RV_RETURN_VALUE_IGNORED, type: RV, category: CORRECTNESS 


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();
 

 很显然,告诉我们忽略了 String.replace(char, char) 的返回值。并且还要我们应该注意检查方法的返回值,最后还给了代码示例。

 

  2.检测器:初始化之前读取字段

   这个检测器寻找在构造函数中初始化之前被读取的字段

    

Java代码   收藏代码
  1. <span style="font-size: x-small;">                 private List actions;  
  2.      public void Thing(String startingActions) {  
  3.                   StringTokenizer tokenizer = new StringTokenizer(startingActions);  
  4.                   while (tokenizer.hasMoreTokens()) {  
  5.                     <span style="color: #ff0000;">  actions.add(tokenizer.nextToken());</span>  
  6.   
  7.   
  8.                   }     
  9.                 
  10.       }</span>  

  上面代码段中的红色代码会出现空指针异常,因为 List actions在使用之前,从来没有被初始化过。

   使用findbugs检查后,出现的bug信息如下:

写道
Bug: Read of unwritten field actions
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. 

 

  3. 检测器:Null 指针对 null 的dereference和冗余比较

     这个检测器查找两类问题。它查找代码路径将会或者可能造成 null 指针异常的情况,它还查找对 null 的冗余比较的情况。例如,如果两个比较值都为 null,那么它们就是冗余的并可能表明代码错误。FindBugs 在可以确定一个值为 null 而另一个值不为 null 时,检测类似的错误.如下列代码:

 

Java代码   收藏代码
  1. public void  test2(Map<String,Persion> map ){  
  2.      Persion person = map.get("bob");  
  3.       if (person != null) {  
  4.          System.out.println(person);  
  5.        }  
  6.       <span style="color: #ff0000;"> String name = person.getName();</span>  
  7.   
  8.   
  9.        
  10.        
  11.  }  
 

   在这个例子中,如果第 2 行的 Map 不包括一个名为“bob”的人,那么在第 6 行询问 person 的名字时就会出现 null 指针异常。因为 FindBugs 不知道 map 是否包含“bob”,所以它将第 6 行标记为可能 null 指针异常。

 

使用findbugs检查后,出现的bug信息如下:

写道
Pattern: Possible null pointer dereference
id: NP_NULL_ON_SOME_PATH, type: NP, category: CORRECTNESS


There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed; deciding that is beyond the ability of FindBugs.

 

 

四.findbugs的使用

 

  1.在eclipse中使用

     1)安装插件:Install New Software-->update site :http://findbugs.cs.umd.edu/eclipse/,安装eclipse插件

     2)运行findbugs:在“Package Explorer”或“Navigater”里,右键点击项目(也可包名、类名)名称,选择“Find  bugs”-“FindBugs”开始运行

     3)查看结果:运行完成后在FindBugs视图可以跟踪到具体的代码,并给问题代码打上红色或者黄色虫子标识,在Properties窗口里是问题的详细描述及建议方案,一目了然。

 

 

  4)规则设置:在Preferences里可以对FindBugs规则等进行详细设置,从庞大的代码中过滤掉那些不重要的缺陷。

        

 

  2.findbugs与maven集成:

     1) 在pom.xml里加入:

 

Java代码   收藏代码
  1.  <reporting>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.codehaus.mojo</groupId>  
  5.             <artifactId>findbugs-maven-plugin</artifactId>  
  6.             <version>2.0.1</version>  
  7.         </plugin>  
  8.     </plugins>  
  9. </reporting>   
 

   2)运行mvn site生成的报告位置在:target\site\findbugs.html。更多maven集成用法参见:

        http://mojo.codehaus.org/findbugs-maven-plugin/2.0.1/usage.html

 

 

 

参考资料:

 

 

 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值