My work uses FreeBuilder extensively to generate the Builder pattern for Java classes. In addition to this, we use the generated Builder classes to deserialize the data calsses using Jackson. After a while it became tiresome to type @FreeBuilder
and class Builder extends ...
everywhere. So I decided to write and IntelliJ IDEA plugin that does it for me.
These are the things I wanted the plugin to do for me:
- Annotate the public class from the current file with
@FreeBuilder
annotation. - Create an inner class for the annotated class - this should be a
static
class if the annotated class is anabstract
class and a child class if the annotated class is aninterface
. - Ensure that the generated
Builder
class is annotated with@JsonIgnoreProperties(ignoreUnknown=true)
because this is a convention we like to follow. - Ensure that the parent class gets annotated with
@JsonDeserialize(builder=...)
annotation. - Rebuild the project so that the annotation processing for FreeBuilder runs.
After poking around the IntelliJ Plugin development documentation, I was able to write a simple enough plugin that does it. For whatever reason, trying to find how to achieve simple things like how to create a new class that you can add as a child to an existing class was painful.
The plugin is available here from the IntelliJ plugin repository and the source code is here on GitHub. In addition to the above mentioned features, I wanted to make sure that the annotations gets added only if the annoattion classes were in the classpath of the current module. The plugin also displayes messages when it decides to skip a step because an annotation class was not in the classpath or because nnotations already exist on the class.
A short demo of the plugin in action is shown below.