`

提高代码质量—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 方法时,如下:

    

    public void  test1() {
		String a = "bob";
		a.replace('b', 'p');


	    if(a.equals("pop")){
	    	  
	    	System.out.println(a);
	    	 
	     }

	}

 这个错误很常见。在第三行红色代码,程序员认为他已经用 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.检测器:初始化之前读取字段

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

    

                 private List actions;
	 public void Thing(String startingActions) {
		          StringTokenizer tokenizer = new StringTokenizer(startingActions);
		          while (tokenizer.hasMoreTokens()) {
		              actions.add(tokenizer.nextToken());


		          }   
		      
	  }

  上面代码段中的红色代码会出现空指针异常,因为 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 时,检测类似的错误.如下列代码:

 

 public void  test2(Map<String,Persion> map ){
		 Persion person = map.get("bob");
		  if (person != null) {
			 System.out.println(person);
		   }
		   String name = person.getName();


		 
		 
	 }
 

   在这个例子中,如果第 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里加入:

 

  <reporting>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>findbugs-maven-plugin</artifactId>
				<version>2.0.1</version>
			</plugin>
		</plugins>
	</reporting> 
 

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

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

 

 

 

参考资料:

 

 

 

 

     

  • 大小: 64.2 KB
  • 大小: 139.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics