# SpotBugs

**SpotBugs** 是一款免费的开源静态分析工具，它通过扫描编译后的字节码来查找 [Java](https://www.edgepedia.org/zh/java-programming-language) 程序中的“缺陷模式”（bug pattern）实例——即那些很可能是错误的代码构造。<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> 它是 Java 缺陷模式分析领域的先驱 FindBugs 的后继者，FindBugs 的原仓库现已将开发工作重定向至 SpotBugs 项目。<sup>[2](https://github.com/findbugsproject/findbugs)</sup> 该工具可检查 400 多种缺陷模式<sup>[3](https://spotbugs.github.io/)</sup>，通常从 Gradle 或 Maven 构建中运行，也可在持续集成环境中运行，或与 PMD、[SonarQube](https://www.edgepedia.org/zh/sonarqube)、Error Prone 等其他分析器配合使用。<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 对 Java 字节码进行静态分析，查找“缺陷模式”，即很可能是错误的代码实例<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> |
| 模式覆盖 | 400 多种缺陷模式<sup>[3](https://spotbugs.github.io/)</sup>；一份 2025 年的研究数据集列举了 SpotBugs 的 490 种缺陷模式，分为十个类别<sup>[5](https://www.nature.com/articles/s41597-025-06154-7)</sup> |
| 运行要求 | Java 11 或更高版本的运行时，至少 512 MB 内存；可在 GNU/Linux、Windows 和 MacOS X 上运行<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> |
| 输入 | 编译后的 .class 文件；支持 JDK 11 及更新版本生成的字节码，不支持更早版本的 JDK 字节码<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> |
| 起源 | FindBugs 的后继者；FindBugs 仓库声明“FindBugs 现已成为 SpotBugs”，并指向 SpotBugs 的 GitHub 项目<sup>[2](https://github.com/findbugsproject/findbugs)</sup> |
| 构建集成 | 官方 Gradle 插件为每个源集生成一个任务（例如 spotbugsMain 和 spotbugsTest），要求 Gradle 7.0 或更高版本<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup> |
| 可扩展性 | 第三方插件可添加检测器；以安全为侧重点的插件 Find Security Bugs 通过 spotbugsPlugins 依赖引入<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup> |

## 工作原理

SpotBugs 作用于编译后的 class 文件，而非源代码。它在字节码上运行一组检测器，每个检测器查找一种或多种缺陷模式，并将匹配结果作为警告报告出来。<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> 由于分析只能看到字节码，某些警告可能被错误触发；缺陷描述文档举例指出，当 switch 语句的 default 分支位于末尾、而其他分支没有 break 语句时，针对 switch 语句的警告可能是误报。<sup>[6](https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html)</sup>

标准检测器覆盖了广泛的正确性与质量问题。例如：查找无限循环和无限递归循环、volatile 字段的非原子自增、捕获异常后不做任何处理的方法、将值与自身比较之类的自比较、equals() 与 hashCode() 定义中的问题、双重检查锁定，以及传递给 SQL 执行方法的非常量字符串——FindSqlInjection 检测器使用数据流分析来定位后者。<sup>[7](https://spotbugs.readthedocs.io/en/stable/detectors.html)</sup> 部分检测器被标记为慢速，例如有一个检测器会分析所有方法以确定哪些方法会无条件地对参数解引用，这些信息随后用于查找可能传入 null 的调用点。<sup>[7](https://spotbugs.readthedocs.io/en/stable/detectors.html)</sup>

SpotBugs 将其缺陷模式分为十个主要类别：不良实践、正确性、实验性、国际化、恶意代码漏洞、多线程正确性、虚假随机噪声、性能、安全性、可疑代码。<sup>[5](https://www.nature.com/articles/s41597-025-06154-7)</sup>

## 要求与版本

SpotBugs 使用 JDK 11 构建，可在 JRE 11 及更新版本上运行。它可以扫描 JDK 11 及更新版本生成的字节码，不过文档将 Java 11 及更新版本字节码的支持描述为实验性，并指引用户到问题追踪器查看已知问题。不支持来自更早 JDK（如 10、9、7 及更早版本）的字节码。<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup> 当前文档记载的发布版本为 4.10.5。<sup>[1](https://spotbugs.readthedocs.io/en/stable/introduction.html)</sup>

该项目的里程碑计划记录了两条未来路线：SpotBugs 5.0.0 将要求使用 Java 17 来运行 SpotBugs 本身，并将重新审视注解依赖，例如移除 jsr305、添加部分 jakarta 注解和 jspecify；SpotBugs 6.0.0 则尚未排定日期。<sup>[8](https://github.com/spotbugs/spotbugs/milestones)</sup>

## 构建集成

**Gradle。** 官方 SpotBugs Gradle 插件可在 Java 和 Android 项目上运行 SpotBugs。它为 Gradle Java 插件创建的每个源集生成一个任务，因此具有 main 和 test 两个源集的项目会得到 spotbugsMain 和 spotbugsTest 任务。这些任务以编译后的 .class 文件为输入，因此在 Java 编译之后运行；插件将它们接入 check 生命周期任务，使 ./gradlew check 会运行 SpotBugs。该插件不支持 Gradle 6；需要 Gradle 7.0 或更高版本，插件 README 指出当前配置要求 Gradle 8.2 或更高版本。<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup><sup> • </sup><sup>[9](https://github.com/spotbugs/spotbugs-gradle-plugin)</sup> 另有一个独立的 SpotBugs Base 插件不会自动生成任何任务，供希望从零开始配置分析的团队使用。<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup>

**Maven。** spotbugs-maven-plugin 为 Maven 构建基于 SpotBugs 库生成报告。<sup>[10](https://spotbugs.github.io/spotbugs-maven-plugin/project-info.html)</sup>

**插件。** 额外的检测器以 SpotBugs 插件形式分发，并声明为分析的依赖项。文档中的示例通过 spotbugsPlugins 配置添加 Find Security Bugs 插件（com.h3xstream.findsecbugs:findsecbugs-plugin:1.14.0）。<sup>[4](https://spotbugs.readthedocs.io/en/stable/gradle.html)</sup> 检测器作者编写派生自字节码扫描器、操作码栈扫描器等基础检测器的类；插件通过插件元数据被发现，每个插件提供一个 messages.xml 文件，SpotBugs 读取它来生成人类可读的报告。<sup>[11](https://github.com/MassiveSecOrg/spotbugs-v2/blob/master/docs/detector-development.md)</sup><sup> • </sup><sup>[12](https://spotbugslm.readthedocs.io/en/latest/implement-plugin.html)</sup>

## 研究中的有效性

SpotBugs 是软件工程研究中评估最频繁的 Java 静态分析器之一，通常与 PMD、SonarQube、Error Prone 和 Infer 进行比较。一项实证召回率研究测量了 Error Prone、Infer 和 SpotBugs 能发现多少来自 15 个热门开源项目的 594 个真实 Java 缺陷。<sup>[13](https://github.com/sola-da/StaticBugCheckers)</sup> 一项针对 Juliet 测试套件的案例研究比较了 PMD、SpotBugs、Find Security Bugs 和 SonarQube 在 CWE Top 25 列表中六种弱点上的表现，结论是特定弱点最适合用特定工具检测；该研究使用 SpotBugs 4.2.3 并采用中等置信度设置，Find Security Bugs 的结果通过将其作为插件添加到 SpotBugs 获得。<sup>[14](https://doi.org/10.1556/606.2021.00454)</sup> 一项使用 Juliet 1.3 套件和 [Apache Tomcat](https://www.edgepedia.org/zh/apache-tomcat) 数据集的比较研究发现，在 Java 侧 SpotBugs 检测到的漏洞类别多于 PMD。<sup>[15](https://www.sciencedirect.com/science/article/pii/S1877050920312023)</sup>

研究也记录了局限。一项针对真实缺陷的 Error Prone 与 SpotBugs 初步研究发现，这些分析器易于加入基于 Maven 的工具链，但对所研究的缺陷并不十分有效，在该样本中 SpotBugs 仅成功检测到一个缺陷。<sup>[16](https://pldb.kirancodes.me/p/conf/sigsoft/Tomassi18)</sup> 一份 2025 年的大型数据集包含超过 120 万条 Java 静态分析警告，使用 SpotBugs、SonarQube 和 PMD 从 102 个 GitHub 项目中收集数据构建而成，发现所观察到的 SpotBugs 缺陷模式类型中只有 16% 同时产生可操作和不可操作的警告，低于 PMD 的 81%；作者将此归因于其数据中 SpotBugs 警告样本较小；该数据集在所有工具中总共包含 196,940 条可操作警告和 1,030,823 条不可操作警告。<sup>[5](https://www.nature.com/articles/s41597-025-06154-7)</sup>

## 与相关工具的比较

SpotBugs 在 Java 分析器中占据一个特定的生态位。与 Error Prone 一样，它使用预定义规则来发现已知的缺陷模式，一项静态缺陷检测器综述将其描述为 FindBugs 的后继者。<sup>[17](https://software-lab.org/publications/Dissertation_Andrew_Habib.pdf)</sup> 与分析源代码的 PMD 不同，SpotBugs 分析字节码，这是从业者在选择工具时会提及的区别；字节码分析也意味着代码必须先编译才能被检查，Nature 数据集研究指出，对于实际项目而言这一要求颇为严格。<sup>[5](https://www.nature.com/articles/s41597-025-06154-7)</sup>

## References

1. [Introduction, SpotBugs 4.10.5 documentation](https://spotbugs.readthedocs.io/en/stable/introduction.html)
2. [findbugsproject/findbugs, GitHub (FindBugs is now SpotBugs)](https://github.com/findbugsproject/findbugs)
3. [SpotBugs project web site](https://spotbugs.github.io/)
4. [Using the SpotBugs Gradle Plugin, SpotBugs documentation](https://spotbugs.readthedocs.io/en/stable/gradle.html)
5. [Kószó, D. et al. (2025). A Large-Scale Collection Of (Non-)Actionable Static Code Analysis Reports. Scientific Data 12, 1884.](https://www.nature.com/articles/s41597-025-06154-7)
6. [Bug descriptions, SpotBugs 4.10.5 documentation](https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html)
7. [Detectors, SpotBugs 4.10.5 documentation](https://spotbugs.readthedocs.io/en/stable/detectors.html)
8. [Milestones, spotbugs/spotbugs, GitHub](https://github.com/spotbugs/spotbugs/milestones)
9. [spotbugs/spotbugs-gradle-plugin, GitHub](https://github.com/spotbugs/spotbugs-gradle-plugin)
10. [Project Information, spotbugs-maven-plugin](https://spotbugs.github.io/spotbugs-maven-plugin/project-info.html)
11. [Detector development documentation, spotbugs-v2](https://github.com/MassiveSecOrg/spotbugs-v2/blob/master/docs/detector-development.md)
12. [Implement SpotBugs plugin, SpotBugs documentation](https://spotbugslm.readthedocs.io/en/latest/implement-plugin.html)
13. [sola-da/StaticBugCheckers, GitHub (empirical study of static bug checkers)](https://github.com/sola-da/StaticBugCheckers)
14. [Detecting security vulnerabilities with static analysis: a case study (2022)](https://doi.org/10.1556/606.2021.00454)
15. [A Comparative Study of Static Code Analysis Tools for Vulnerability Detection in C/C++ and Java Source Code, Procedia Computer Science](https://www.sciencedirect.com/science/article/pii/S1877050920312023)
16. [Tomassi et al. Bugs in the Wild: Examining the Effectiveness of Static Analyzers at Finding Real-World Bugs, FSE 2018](https://pldb.kirancodes.me/p/conf/sigsoft/Tomassi18)
17. [Habib, A. The State of Static Bug Detectors (dissertation)](https://software-lab.org/publications/Dissertation_Andrew_Habib.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Static analysis and code quality tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/spotbugs

English original: https://www.edgepedia.org/spotbugs
