@Getter @Setter
public class MyClass {
private String name;
}
Project Lombok is an annotation-based Java library that allows you to reduce boilerplate code. It offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write.
Lombok is used by many software developers, including the Spring Framework team. We use it in our examples to cut down the amount of "code noise", making things easier to understand.
Lombok Resources
Some excellent Lombok resources include:
|
This section describes the Lombok annotations we use.
The @Getter & @Setter annotations are used on a class or a property.
This:
@Getter @Setter
public class MyClass {
private String name;
}
produces:
public class MyClass {
private String name;
public String getName() { (1)
return name;
}
public void setName(String name) { (2)
this.name = name;
}
}
1 | The @Getter annotation created this method |
2 | The @Setter annotation created this method |
The @ToString annotation is used on a class or a property.
This:
@ToString
public class MyClass {
private String name;
private String address;
}
produces:
public class MyClass {
private String name;
private String address;
@Override
public String toString() { (1)
return "MyClass(name=" + name + ", address=" + address + ")";
}
}
1 | The @String annotation created this method |
The @EqualsAndHashCode annotation is used on a class.
This:
@EqualsAndHashCode
public class MyClass {
private String name;
private String address;
}
produces:
public class MyClass {
private String name;
private String address;
@Override
public boolean equals(Object o) { (1)
// . . .
}
@Override
public int hashCode() { (1)
// . . .
}
}
1 | The @EqualsAndHashCode annotation created these two methods |
The @NoArgsConstructor annotation is used on a class.
This:
@NoArgsConstructor
public class MyClass {
private String name;
private String address;
}
produces:
public class MyClass {
private String name;
private String address;
public MyClass() { (1)
}
}
1 | The @NoArgsConstructor annotation added this constructor |
The @NoArgsConstructor annotation is used on a class.
This:
@AllArgsConstructor
public class MyClass {
private final String name;
private String address;
}
produces:
public class MyClass {
private final String name;
private String address;
public MyClass(String name, String address) { (1)
this.name = name;
this.address = address;
}
}
1 | The @AllArgsConstructor annotation created this constructor |
The @RequiredArgsConstructor annotation is used on a class.
This:
@RequiredArgsConstructor
public class MyClass {
private final String name;
private String address;
}
produces:
public class MyClass {
private final String name;
private String address;
public MyClass(String name) { (1)
this.name = name;
}
}
1 | The @RequiredArgsConstructor annotation created this constructor |
@Data is a shortcut annotation that bundles the features of @ToString
, @EqualsAndHashCode
, @Getter
/ @Setter
and @RequiredArgsConstructor
together: In other words, @Data
generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toString()
, equals()
and hashCode()
implementations that involve the fields of the class, and a constructor that initializes all final fields.
@Value is the immutable variant of @Data
: all fields are made private and final by default, and setters are not generated. The class itself is also made final by default, because immutability is not something that can be forced onto a subclass. Like @Data
, toString()
, equals()
and hashCode()
methods are also generated, and each field gets a "getter" method.
The @Builder annotation produces complex builder APIs for your classes. Please refer to the Lombok site for details.
@SneakyThrows is used to "sneakily" throw checked exceptions without actually declaring this in your method’s throws
clause. This somewhat contentious ability should be used carefully, of course. The code generated by Lombok does not ignore, wrap, replace, or otherwise modify the thrown checked exception; it simply fakes out the compiler. On the JVM (class file) level, all exceptions, checked or not, can be thrown regardless of the throws clause of your methods, which is why this works.
public class MyClass {
@SneakyThrows
public void doSomethingThatCanThrowAnIOException() { (1)
// . . .
// Some code that can throw an IOException
// . . .
}
}
1 | The @SneakyThrows annotation removes the need to include the "throws" statement |
The @Slf4j annotation is used on a class.
Instead of:
public class MyClass() {
private static final Logger log = Log.getLogger(MyClass.class);
public void doSomething() {
log.info("I'm here with a value of " + 7);
}
}
use:
@Slf4j (1)
public class MyClass() {
public void doSomething() {
log.info("I'm here with a value of {}", 7); (2)
}
}
1 | The @Slf4j annotation automatically adds the private static final log variable |
2 | Notice how variable substitution is handled in the log statement |
Let’s take a look at the IntelliJ "Structure" tool window for a Java class named "UserInfo". As you can see in the following image, the following methods were auto-generated by Lombok:
a no-argument constructor
an all-arguments constructor
getters and setters for all properties
equals() and hashCode() methods
toString() method
That’s a lot of generic code that got automatically generated, which saves time and delivers more-consistent code.
Summary of Lombok
Lombok is an annotation processer used to reduce the amount of tedious and repetitive Java code that developers must write. The KOS engineering team uses Lombok in our core code as well as example code. The annotations we’ve covered include:
The Lombok Project is documented on their website. |