Project Lombok Intro
Project Lombok helps to reduce boilerplate code.
Adding Lombok to Project
For Maven project, add Maven Dependency. see https://projectlombok.org/setup/maven
1 | <dependencies> |
For Gradle project. see https://projectlombok.org/setup/gradle
1 | repositories { |
NOTE: the maven dependency scope is provided because lombok is not needed at runtime.
Lombok works as long as it is in the project classpath. However, you still need to setup lombok in the IDE if you are using an IDE.
For Intellij, You can install the Lombok plugin. see https://projectlombok.org/setup/intellij
For Eclipse, you can execute the jar file in ~/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-*.jar
Sample Class with Lombok Annotations
1 | import lombok.*; |
@Data
@Data is shortcut for
- @Getter
- @Setter
- @RequiredArgsConstructor
- @ToString
- @EqualsAndHashCode.
@Data is very convenient because it generates most of the boilerplate code for you.
1 |
|
Constructors
use @NoArgsConstructor
, @RequiredArgsConstructor
, @AllArgsConstructor
to generate constructors.
@NoArgsConstructor
- Generates a no-args constructor@RequiredArgsConstructor
- Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as @NonNull.@AllArgsConstructor
- Generates an all-args constructor.
1 |
|
Generate Getter and Setter
use @Getter
and @Setter
to generate default getter and setter.
1 |
|
Accessors
@Accessors was introduced as experimental feature in lombok v0.11.0.
The @Accessors
annotation is used to configure how lombok generates and looks for getters, setters, and with-ers.
fluent attribut - If true, accessors will be named after the field and not include a get or set prefix.
chain attribute - If true, setters return this instead of void.
makeFinal attribute - will create final getters, setters, and with-ers.
Be VERY careful when using fluent attribute. Fluent setters changes the name of the setter methods. Setter methods that starts with set are necessary for some libraries and frameworks. Jackson won’t work with fluent setters.
Class with @Accessors annotation
1 | import lombok.*; |
To use the fluent setter
1 | Book book = new Book().title("my title") |
Equals and hashCode
use @EqualsAndHashCode
to implement equals
and hashCode
method. By default, it uses non-static, non-transient fields. You can also customize the fields to be used.
To exclude a field, use @EqualsAndHashCode.Exclude
annotation.
Alternatively, you can specify exactly which fields or methods you wish to be used by marking them with @EqualsAndHashCode
.Include and using @EqualsAndHashCode(onlyExplicitlyIncluded = true)
.
1 |
|
ToString
generate an implementation of toString()
method.
To skip a field, use @ToString.Exclude
annotation.
1 |
|
Sample output:
1 | Book(title=Java 11 Cookbook, subtitle=null, category=Java, price=49.99) |
With
@With - to construct a clone of the object, but with a new value for this one field.
1 |
|
Demo
1 | Book book = new Book("Title", "Subtitle"); |
Null Check
You use @NonNull annotation to do null checks. If it is put on a method or constructor argument, it will throw NullPointerException when the provided value is null. If it is put on a field, then any generated method will have a null check.
1 | public void setTitle( { String title) |
If null is passed as parameter, then NullPointerException will be thrown. The message looks like this:
1 | Exception in thread "main" java.lang.NullPointerException: title is marked non-null but is null |
Builder Pattern
@Builder annotation provides builder API for your class.
Java class with a Builder
1 |
|
To use the Builder
1 | Book b1 = Book.builder() |
Logger
Lombok provides various annotations for creating log
field.
For Example, if you are using Slf4j, then @Slf4j
annotation will create a log
field of type org.slf4j.Logger
for you
1 |
|
Synchronized
@Synchronized
is a safer variant of the synchronized method modifier. The synchronized method modifier locks on this
, but this annotation locks on a field named $lock
.
1 | import lombok.Synchronized; |
is equivalent to
1 | public class SynchronizedExample { |
SneakyThrows
@SneakyThrows can be used to sneakily throw checked exceptions without actually declaring this in your method’s throws clause.
Checked Exception force the caller to handle or rethrow the exception. This can bubble up and created chunky code. @SneakyThrows tricks the Compiler to
1 | public class App { |
Without Lombok
1 | public class App { |