Java syntax sugar with Project Lombok 1

Posted by Jens J├Ąger on June 29, 2013

In one of my last projects I used Project Lombok to reduce boilerplate code. Lombok provides some annotations to remove the noise from the java language.

How does it work

Lombok runs as as an annotation processor which handler modifies the AST (Abstract Syntax Tree) by injecting new nodes such as methods and fields. After the annotation processing stage, the compiler will generate byte code from the modified AST.

How do I use it

Getters and setters like:

Can be replaced by the Lombok annotations:

Or replace the noisy initialization of a logger:

With a fancy annotation:

There are a lot more lombok features. There is also a great tutorial. What lombok does is not very impressive when you look at just a single example. But it helps a lot to remove java syntax noise from your classes and focus on the important parts of your business logic. When you look at the @data example, you can imagine how much code you don’t have to write(generate), read and maintain with Lombok.

Installation

Lombok can be installed with maven or ivy or include the lombok.jar in you build path. You have to teach your IDE about Lombok. Double click on the lombok.jar to install it into eclipse. For IntelliJ there is plugin available. It can also be installed from the IntelliJ plugin repository.

Summary

No more java projekts without Lombok.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Computer Science Thu, 17 May 2018 03:56:31 CET

    Thanks for this informative read, I have shared it
    on Twitter.

Comments

Information about Data protection