What is Lombok?
Lombok is a Java library that auto generates lots of boiler plate code for Java classes. Lombok works at compile time and manipulates Java byte code to add additional features. Lombok uses annotations to specify what boiler plate code will be generates. Eg. @Getter annotation can be applied to any Java bean to auto generate Getter methods for all the fields in that bean.
In this article we will be looking at SuperBuilder annotation of Lombok.
What is SuperBuilder in Lombok?
Lombok can be used to generate Builder class for a Java bean by annotating the bean class with @Builder annotation. In case of a class extending another class, a simple builder will not be able to set values in the parent class. For this case we use @SuperBuilder annotation.
@SuperBuilder annotation when applied on both parent and child classes, allows us to set values in both parent and child class field using the generated Builder class.
See below classes on how to generate SuperBuilder and use it.
The Parent class: Employee.java
package com.blogspot.devnip.lombok.superbuilder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import java.time.LocalDate;
/**
* Represents an employee in the company.
*/
@Getter
@SuperBuilder
public class Employee {
/**
* Id of the employee
*/
private final String id;
/**
* Employees name
*/
private final String name;
/**
* Date when employee joined the company
*/
private final LocalDate joiningDate;
}
The extending Child class:
Manager.java
package com.blogspot.devnip.lombok.superbuilder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import java.time.LocalDate;
import java.util.List;
/**
* Manager is a type of Employee
*/
@Getter
@SuperBuilder
public class Manager extends Employee {
/**
* Employees managed by the Manager
*/
private final List<Employee> teamMembers;
/**
* Date on which this employee became Manager
*/
private final LocalDate promotionDate;
/**
* Customizing builder methods. Rest of the builder code will be auto generated by Lombok.
*
* @param <C>
* @param <B>
*/
public static abstract class ManagerBuilder<C extends Manager, B extends ManagerBuilder<C, B>>
extends EmployeeBuilder<C, B> {
private LocalDate promotionDate;
public B promotionDate(String dateStr) {
this.promotionDate = LocalDate.parse(dateStr);
return self();
}
}
}
Usage of Lombok generated builder methods:
ManagerService.java
package com.blogspot.devnip.lombok.superbuilder;
/**
* Provides operational methods on Manager entity.
*/
public class ManagerService {
/**
* Create a dummy Manager object for testing.
*
* @return
*/
public Manager createDummyManager() {
return Manager.builder()
.id("001")
.name("James Smith")
.promotionDate("2010-07-18")
.build();
}
}
Customizing or overriding methods of SuperBuilder in Lombok.
The Manager class above also demonstrates how to customize the behaviour of Builder class. We simply need to provide our portion of code that we want to be customize and Lombok will generate the remaining code.
0 comments:
Post a Comment