Introduction
Error Prone is a compile-time static analysis tool by Google that hooks into javac to detect common Java programming errors. Unlike standalone linters, it runs during compilation and surfaces issues as compiler errors or warnings, catching bugs before code ever runs.
What Error Prone Does
- Detects hundreds of common Java bug patterns during compilation
- Produces compiler errors or warnings with clear fix suggestions
- Integrates with Maven, Gradle, and Bazel build systems
- Allows teams to write custom bug pattern checkers
- Provides automatic refactoring patches for many detected issues
Architecture Overview
Error Prone operates as a javac plugin that analyzes the abstract syntax tree (AST) during compilation. Each bug pattern is implemented as a BugChecker class that inspects specific AST nodes. When a pattern matches, Error Prone emits a diagnostic with a human-readable description and often a suggested fix that can be applied automatically.
Self-Hosting & Configuration
- Add as a dependency to Maven, Gradle, or Bazel build configuration
- Requires JDK 11 or later
- Enable or disable specific checks via compiler flags
- Promote warnings to errors or demote errors to warnings per check
- Write custom BugChecker classes for project-specific patterns
Key Features
- 500+ built-in bug pattern detectors covering null safety, concurrency, and API misuse
- Automatic fix suggestions that can be applied with a single command
- Runs at compile time with minimal build overhead
- Extensible: teams can write and distribute custom checkers
- Battle-tested across Google internal codebase at massive scale
Comparison with Similar Tools
- SpotBugs — Analyzes bytecode post-compilation; Error Prone works at the source level during compilation
- PMD — Rule-based standalone linter; Error Prone integrates directly into javac
- SonarQube — Server-based analysis platform; Error Prone is a lightweight compiler plugin
- Checkstyle — Focuses on style and formatting; Error Prone focuses on correctness bugs
FAQ
Q: Does Error Prone slow down compilation? A: Overhead is typically under 10%, as it piggybacks on the existing compilation pass.
Q: Can I use it with Kotlin? A: No. Error Prone analyzes Java source code via javac. For Kotlin, consider detekt.
Q: How do I suppress a false positive? A: Use the @SuppressWarnings annotation with the check name.
Q: Is it used in production at Google? A: Yes. Error Prone is used across the entire Google Java codebase.