Introduction
Error Prone is a Java compiler plugin developed by Google that augments the standard javac compiler with additional checks. It catches bugs like null dereferences, incorrect equals implementations, and resource leaks at compile time rather than at runtime.
What Error Prone Does
- Hooks into the javac compiler as a plugin to analyze code during compilation
- Detects over 500 bug patterns including null safety, concurrency, and API misuse
- Provides suggested fixes that can be applied automatically via patching
- Supports custom check authoring using the Error Prone API
- Integrates with Bazel, Maven, and Gradle build systems
Architecture Overview
Error Prone runs as a javac annotation processor plugin. It receives the full compiler AST after type resolution, giving each check access to resolved types, symbols, and control flow information. Checks are implemented as BugChecker classes that match specific AST patterns using a visitor API. Suggested fixes are expressed as source-level patches.
Self-Hosting & Configuration
- Add Error Prone as an annotation processor path in Maven or Gradle compiler configuration
- For Bazel projects, Error Prone is enabled by default in the Java rules
- Disable specific checks with
-Xep:CheckName:OFFcompiler flags - Promote warnings to errors with
-Xep:CheckName:ERRORfor stricter enforcement - Use
@SuppressWarnings("ErrorProneCheckName")for inline suppression
Key Features
- Over 500 built-in bug checks developed from Google's internal codebase experience
- Runs at compile time with no separate analysis step required
- Suggested fixes that can be applied as automated refactoring patches
- Severity levels (ERROR, WARNING, SUGGESTION) for each check
- Support for writing custom checks using the Error Prone Check API
Comparison with Similar Tools
- SpotBugs — analyzes compiled bytecode for bugs; Error Prone works at the source level during compilation
- Checkstyle — enforces code style conventions; Error Prone focuses on semantic correctness bugs
- PMD — finds code smells and unused code via static analysis; Error Prone catches deeper type-level bugs
- NullAway — an Error Prone plugin that adds comprehensive null safety checking on top of the base checks
FAQ
Q: Does Error Prone slow down compilation? A: The overhead is typically 10-20% on compilation time, which is offset by catching bugs before tests run.
Q: Can I use Error Prone with Kotlin or other JVM languages? A: Error Prone works only with javac and Java source files. It does not analyze Kotlin or Scala code.
Q: How do I apply suggested fixes automatically?
A: Use the -XepPatchChecks and -XepPatchLocation flags to generate patch files, then apply them with the Error Prone patching tool.
Q: Is Error Prone used at Google? A: Yes. Error Prone is used across Google's Java codebase and many of its checks originate from bugs found in production.