Check your Java with Error Prone
Written by Nikos Vaggalis   
Monday, 24 October 2022

Error Prone is a new Java compiler plugin created by Google which checks your code for common errors at compile-time. Not does only Error Prone identify issues but suggests their fixes too by analyzing the code’s abstract syntax tree (AST).

An example from the official documentation will make its use clear :

public class ShortSet {
  public static void main (String[] args) {
   Set<Short> s = new HashSet<>();
   for (short i = 0; i < 100; i++) {
     s.remove(i - 1);
  System. out. println(s. size());

error: [CollectionIncompatibleType] Argument 'i - 1' should not be passed to this method;

its type int is not compatible with its collection's type argument Short
s.remove(i - 1);
1 error

This incompatibility is an example of a bug pattern that Error Prone is looking for. It comes with many default such patterns, for instance :

  • AlwaysThrows
    Detects calls that will fail at runtime

  • ArrayFillIncompatibleType
    Arrays. fill(Object[], Object) called with incompatible types.

  • ComparingThisWithNull
    this == null is always false, this != null is always true

  • FormatString
    Invalid printf-style format string

  • Immutable
    Type declaration annotated with @Immutable is not immutable

The treats do not stop here though. You can also create your own Bug patterns although not a straightforward endeavor since you need to use Error Prone's api and interact with the code's AST.

Recognizing the difficulty, Google released another tool that ships with Error Prone called Refaster. Refaster is a tool that refactors your code using before-and-after templates. Once you write these templates, you compile them into .refaster files, then use the Error Prone compiler to refactor your code according to those rules. Refaster then scans for code that matches the before-template which replaces with the code found in the after-template.

An example

public class StringIsEmpty {
    boolean equalsEmptyString(String string) {
    return string.equals("");

   boolean lengthEquals0(String string) {
   return string.length() == 0;

  boolean optimizedMethod(String string) {
  return string.isEmpty();



boolean b = someChained().methodCall().returningAString().length() == 0;


boolean b = someChained().methodCall().returningAString().isEmpty();


if (this.someStringField.equals(""))


if (this.someStringField.isEmpty())

Error Prone supports Java 8, 11 and 17 and can be used with Maven, Bazel, Ant and Gradle.


More Information

Error Prone


Related Articles

Semgrep - More Than Just a Glorified Grep

JetBrain's Qodana - More Than Just A Linter



To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Android 15 Developer Preview Updated

Google has released Android 15 Developer Preview 2 with changes including better handling of automatic language switching and updates for OpenJDK 17.

CISA Offers More Support For Open Source

The Cybersecurity and Infrastructure Security Agency (CISA) has announced a number of key actions that they hope will improve the open source ecosystem.

More News

raspberry pi books



or email your comment to:

Last Updated ( Monday, 24 October 2022 )