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 :
This incompatibility is an example of a bug pattern that Error Prone is looking for. It comes with many default such patterns, for instance :
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
So
becomes
while
becomes
Error Prone supports Java 8, 11 and 17 and can be used with Maven, Bazel, Ant and Gradle.
More InformationRelated ArticlesSemgrep - 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.
Comments
or email your comment to: comments@i-programmer.info |
Last Updated ( Monday, 24 October 2022 ) |