Saturday, 9 April 2016

Project Lombok - @Data annotation


Project Lombok is used to reduce the boilerplate code in a java class.

A normal POJO class has instance variable, setter and getter method, toString method, equals and hashCode method.

Below is a simple POJO called Student. A developer only has to type in the instance variable and rest of the code (in red box) can be generated with the help of IDE.

Taking a step further is Project Lombok. We do not even need this code(in red box). We can replace entire boilerplate code with some simple annotation.


 



Integrating Lombok in Project and Eclipse IDE

1. Add maven dependency to the pom.


<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
            <scope>provided</scope>
        </dependency>

 
2. Download lombok.jar file from below path https://projectlombok.org/download.html. Copy to eclipse home directory and run the java -jar Lombok.jar from eclipse home directory. This will install lombok in the IDE.

Once Lombok is added to project and IDE , our previous POJO class will look like 

With single annotation @Data, Lombok add getter and setter method, equals and hashCode method and toString Method to the POJO class.

Few important point 


1. If any of the method (marked in green in above diagram) is already present in the class that will NOT be overwritten.

2. Although methods added by Lombok are not visible in the in source code they are added into the .class file. So they are present in the class file after compilation.

3. @Data annotation is combination of multiple functionality. To have more granular control we has some more annotation.

3.1 @Setter and @Getter:
     Generate setter and getter method respectively. This has an optional parameter AccessLevel that specify the access level of the method generated.
  @Getter @Setter private boolean studentId= true;
  @Setter(AccessLevel.PROTECTED) private String studentName;
  
3.2 @EqualsAndHashCode
  With this annotation we can have control over following
   A. Equals and hashcode from the superclass can be invoked
   B. Which field in the POJO class we want for equality and hashcode. We can exclude which field we do not want for equality and hashcode comparison.
  @EqualsAndHashCode(callSuper=true,exclude={"studentName")       => Here studentName will be excluded from equals and hashcode comparison

3.3 @ToString
  Similar to @EqualsAndHashCode annotation we can have more control on the fields that we want to include or exclude from toString method with @ToString annotation.
  @ToString(exclude="studentId") => This will exclude studentId from toString() methods  
  
  Lombok has few more annotation like @NonNull, @Cleanup, @Synchronized and @SneakyThrows. Those are not covered in this blog.
  
  More information on Project Lombok can be found here
  
 1.  http://jnb.ociweb.com/jnb/jnbJan2010.html
 2.  https://projectlombok.org/
 3.  https://projectlombok.org/features/

 



No comments:

Post a Comment

Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...