Pages

In this article we are going to see how can we use lombok inside our regular Java Code to minimize code length & redundancy.

What is lombok?
Lombok , a compile time annotation pre-processor that helps to inject some code in compile time. Before going in detail, i request you should see the video from their site . I am not going live demo like this but I will show how we can use this inside project (including maven life cycle) .

Let's start with basic understanding. As I have told, lombok is a compile time annotation pre-processor. What does that means?
-> it will work in compile time (we can see effect during writing code)
-> we need dependencies in class path during compilation (with javac command execution)
-> We need to have plug-in support to see effect in IDE while coding.(as IDEs compiles during coding/saving)

Lombok has several Utility / feature . We will go through main items of them that I have used so far.

Note : For both eclipse and IntelliJ , I am using Outline View to look at the effects. To enable outline : Eclipse : Window -> Show view -> OutlineIntelliJ: View -> Tool Buttons , you will structure.

@Getter @Setter : It heps to make
getter setter with variable. It also supports access level parameters to
define scopes of getters & setters.

Parameter : Takes a Enum parameter AccessLevel
PUBLIC = Publicly accessible
MODULE, PACKAGE = same as default (not using any modifier)
PROTECTED = same as protected
PRIVATE = same as private
NONE = there will not be implementation

In class level :

Overriding class level getter :

@ToString : This is implementation of toString(), we can use only for the class level.
Parameters :
includeFieldNames() : boolean type => define all field inclusion
callSuper(): boolean type => calling super
doNotUseGetters() : boolean type => avoid using getters of the fields.
exclude() : String array of field names=> define field exclusion
of() : String array of field names = > defines which to be added (explicitly)

@NotNull : This will add null check on variable and throws null pointer exception.
Scope of use : method, parameter, local variable
Without checking

with @NotNull

@EqualsAndHashCode : It simple add equals(Object other), and hashCode() to class. (so it is class level annotation).
It supports parameters to customize those equals and hash code methods. All parameters are same as @ToString.

@Cleanup: It is a easy way to cleanup the resources. It actually ensures , the resources close() method will be called in finally block of try.
Only Local variable :

Calling close method for resources. if you have another method name, then call you need value=thatMehtodName (like exit)

Note : In my opinion, it is nice to use this when you are not caring about
managing resources. If you like to use your resources by your logic,
better avoid this. Example, multi threaded resource usages.

Logging using lombok :@log: This is one of the best usable feature. This picture shows all loggers to gather. My example in github will shows details.

We need to set log before class, thats all. It will provide a static variable log and we can use it based on what type of log we are using.l

Parameter : it takes topic as parameter , by default it is class name. we can set log topic.

Note : for different log implementation we need different type of configuration. Lombok doesn't provide any configuration help, it just inject code. So, we need to add depend on what type of log I am adding.
My example in github contains all 6 types of log configurations. See pom file carefully to know dependencies as well as resources for configuration files.

Note : There are lot more regular feature and experimantal features I skipped due to not scope of my tests(spatially @Synchronized) . I will add other one by one in github repository and add referrence notes here.