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.add(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);
^
(see https://errorprone.info/bugpattern/CollectionIncompatibleType)
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 {
  @BeforeTemplate
    boolean equalsEmptyString(String string) {
    return string.equals("");
  }

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

 @AfterTemplate
 @AlsoNegation
  boolean optimizedMethod(String string) {
  return string.isEmpty();
  }
 }

 

So

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

becomes

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

while

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

becomes

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

Github

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.

Banner


Google Opensources Privacy Library
08/11/2024

Google is making a new differential privacy library available as open source. PipelineDP4J is a Java-based library that can be used to analyse data sets while preserving privacy.



Copilot Improves Code Quality
27/11/2024

Findings from GitHub show that code authored with Copilot has increased functionality and improved readability, is of better quality, and receives higher approval rates than code authored without it.

 [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Monday, 24 October 2022 )