如何使用 SpotBugs 发现 Java BUG

x33g5p2x  于2022-09-14 转载在 Java  
字(2.9k)|赞(0)|评价(0)|浏览(1657)

本文将向您介绍 SpotBugs 实用程序项目,它可以帮助您发现代码中可能变成运行时错误的 Java“BUG模式”。

SpotBugs 入门

首先,让我们看看 SpotBugs 是如何工作的。此工具使用定义了一组错误模式,将使用检测器在您的代码中进行扫描。反过来,每个 Bug 模式都有不同的 Categories

默认情况下,SpotBugs 在您的代码中搜索 allBug 模式 并包括 allCategories。但是,您可以选择过滤一组错误模式、项目中的类/方法选择以及特定类别的错误模式。

例如,您可以选择仅扫描特定的错误模式,如下所示,其中异常被捕获而不是抛出

  1. try {
  2. } catch (Exception e) {
  3. // TODO Auto-generated catch block
  4. e.printStackTrace();
  5. }

另一方面,您可以选择仅扫描属于 PERFORMANCE 类别的错误模式。

您可以使用 Detectors 检测错误模式。检测器列表可在此处获得:https://spotbugs.readthedocs.io/en/latest/detectors.html

安装 SpotBugs

为了安装 SpotBugs,您有多种选择:您可以从 GitHub 下载最新版本:https://github.com/spotbugs/spotbugs/

当您从源代码安装项目时,您将能够从命令行调用 SpotBugs。例如:

  1. java -jar $SPOTBUGS_HOME/lib/spotbugs.jar options...

另一方面,您也可以将 SpotBugs 作为 Maven / Gradle / Ant 或 Eclipse 插件运行。

在本文的下一部分中,我们将展示如何在 Maven 项目中使用 SpotBugs。

在 Maven 中使用 SpotBugs

为了在 Maven 项目中使用此工具,您只需将其 Maven 插件添加到 pom.xml 的报告部分:

  1. <reporting>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.github.spotbugs</groupId>
  5. <artifactId>spotbugs-maven-plugin</artifactId>
  6. <version>4.7.1.1</version>
  7. </plugin>
  8. </plugins>
  9. </reporting>

报告插件将在 Maven 站点目标中运行。因此,您可以按如下方式运行它:

  1. mvn compile site

然后,验证您的 Maven 日志中是否正在生成 SpotBugs 报告:

  1. [INFO] Generating "SpotBugs" report --- spotbugs-maven-plugin:4.7.1.1:spotbugs

最后,您会在项目的 target/site 文件夹下找到 spotbugs.htmlreport:

  1. [francesco@fedora tamutils]$ tree target/site
  2. target/site
  3. ├── css
  4. ├── dependencies.html
  5. ├── dependency-info.html
  6. ├── index.html
  7. ├── plugin-management.html
  8. ├── plugins.html
  9. ├── project-info.html
  10. ├── project-reports.html
  11. ├── spotbugs.html
  12. └── summary.html

打开 SpotBugs HTML 页面以检查您的项目的报告:

HTML 报告包含按类别分组的报告中包含的类列表。

高级用法

在我们的基本示例中,SpotBugs 将扫描所有类别中所有可能的错误模式。要将过滤器引入这些选项,您可以使用 XML 过滤器文件。例如,以下插件配置为您的项目引入了包含过滤器和排除过滤器:

  1. <plugin>
  2. <groupId>com.github.spotbugs</groupId>
  3. <artifactId>spotbugs-maven-plugin</artifactId>
  4. <version>4.7.1.1</version>
  5. <configuration>
  6. <includeFilterFile>myfilter-include.xml</includeFilterFile>
  7. <excludeFilterFile>myfilter-exclude.xml</excludeFilterFile>
  8. </configuration>
  9. </plugin>

例如,以下 myfilter-include.xml 定义了一个错误模式(“OS_OPEN_STREAM”:方法可能无法关闭流),该模式适用于类 com.foobar.MyClass 在其方法 writedataToFile 中:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <FindBugsFilter>
  3. <Match>
  4. <Class name="com.foobar.MyClass" />
  5. <Method name="writeDataToFile" />
  6. <Bug pattern="OS_OPEN_STREAM" />
  7. </Match>
  8. </FindBugsFilter>

另一方面,以下 XML 过滤器文件将 PERFORMANCE 类别中的所有错误模式定义为匹配过滤器:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <FindBugsFilter>
  3. <Match>
  4. <Bug category="PERFORMANCE" />
  5. </Match>
  6. </FindBugsFilter>

安装 SpotBugs Eclipse 插件

最后,我们将提到如何将该工具安装为 Eclipse Plugin。你可以通过两种方式做到这一点:

  1. 使用帮助 |安装新软件并添加 SpotBugs 更新站点插件 https://spotbugs.github.io/eclipse/
  2. Eclipse Market Place中,搜索插件,如下图所示:

重新启动 Eclipse 后,您将能够在您的项目中激活 SpotBugs 选项。您需要做的就是右键单击您的项目。然后,选择选项,例如 Find Bugs

SpotBugs 将执行,问题标记(显示在源窗口中,也显示在 Eclipse 问题视图中)将指向代码中可能存在错误模式实例的位置。

相关文章

最新文章

更多