A Practical Guide to Project Lombok

A Practical Guide to Project Lombok

Java is a powerful language but often criticized for being too verbose (aka boilerplate – term used to describe code that is repeated in many parts of an application with little alteration). Project Lombok came to help the lazy coder in you. It basically generates code for you. But instead of adding code to your non-compiled classes, like an IDE generally does, Lombok works during the build process by modifying the Java bytecode of your class files.

It’s very simple to configure it, their project page has all you need to help you to add it in your projects.

Lombok Annotations

1. @Getter and @Setter

The @Getter and @Setter annotations are used to generate accessor methods for fields, @Getter annotation respects the convention if the field is a boolean property. Both @Getter and @Setter take an optional parameter to specify the access level for the generated method:

Lombok code:

Equivalent Java native code:

2. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

The @NoArgsConstructor annotation generates constructors that take no arguments, the @RequiredArgsConstructor generates one argument per final field and @AllArgsConstructor generates one argument for every field.

Lombok code:

Equivalent Java native code:

3. @ToString and @EqualsAndHashCode

Here is another good example of core java boilerplate and we can automate this by Lombok class-level annotation.

The @ToString generates an implementation of toString() method including all class attributes.

The @EqualsAndHashCode generates both equals() and hashCode() methods considering all relevant fields.

These annotations accept useful configuration options. For example, if your class belongs to a hierarchy we can use callSuper=true and the parents result will be considered as well.

Lombok code:

Equivalent Java native code:

4. @Data and @Value

Let’s say that you would like to add @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor, Lombok provides to us @Data that is a shortcut to do all that. Pretty cool, uh?

The @Value annotation is a variation of @Data that allows us to create immutable classes in a smooth way. It’s basically a shorthand to @ToString, @EqualsAndHashCode, @AllArgsConstructor, @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) and @Getter.

5. @Builder

The @Builder is an implementation of the object creation pattern Builder. It prevents us to write an extra Builder class and associated fluent setter-like methods by simply adding the @Builder annotation to our class.

Lombok code:

Equivalent Java native code:

6. @NonNull

The @NonNull annotaion creates a null-check statement and can be used in methods or constructors. The statement will be inserted at the very top of your method. For constructors, the null-check will be inserted immediately following any explicit this() or super() calls.

Lombok code:

Equivalent Java native code:

7. @SneakyThrows

How many times have you managed checked exceptions in the client side that you needed to catch or throw them?

@SneakyThrows can be used to sneakily throw checked exceptions without actually declaring this in your method’s throws clause.

Lombok code:

Equivalent Java native code:

8. @Log

The @Log annotation generates a logger field called log and the field’s type depends on which logger you have selected. The options are @CommonsLog, @JBossLog, @Log, @Log4j, @Log4j2, @Slf4j and @XSlf4j.

Lombok code:

Equivalent Java native code:

Conclusion

Apart from the useful features that we presented on this article, Project Lombok provides many other features that worth checking out. We invite you to take a deeper look at the feature overview page for more details and use cases.
Project Lombok is a useful development tool, that help us to boost our productivity, build an elegant code and improve the code readability. We hope you give it a shot and share your feedback with us. Please feel free to leave your comments!