Lombok
Last updated
Was this helpful?
Last updated
Was this helpful?
is a library that adds many missing features into Java language. Lets have a look what we can use to speed up our development and make Java code more concise, error prone and readable.
Before we start, lets make sure we can build our project and we can work with lombok in our IDE ().
We need to do two things to get lombok working in our IDE.
We have to install plugin before we can work with lombok. If we don't install the plugin, we would get compile error from IDE but project would work if ran from the build tool. When we have installed the plugin and restarted IDE, we can make sure the plugin is properly configured. See the following configuration that will make lombok working in IDEA.The second thing that needs to be done is to enable annotation compilation in IDEA.
Instead of manual IDE configuration, we can use
net.ltgt.apt-idea
ornet.ltgt.apt-eclipse
in Gradle to automatically configure what we did above.
We are going to use Gradle code and build management tool. Here is everything what is needed to get lombok project working.
When making a domain objects or DTO (Data Transformation Objects) in pure Java, it is all about description of data using classes, private fields, getter and setter methods. But what we want to do is something like this:
We just want to say type name, what fields it contains and what are the types of the fields. Unfortunattely we need to provide getters and setters in pure Java. But lombok gives us annotations that will enhance the bytecode and provide us with getters and setters for non final fields. Also there is an annotation to generate the constructor with all the fields.
When we use these annotations we can write code like this:
But trying to set id would give us compilation error.
We can annotate class with @EqualsAndHashCode
annotation to provide generated equals
and hashCode
methods from all field in the class.