Lombok: Getting Started

1. Overview

Lombok is a lightweight Java library that makes your programming experience much more fun. With Lombok, you’ll never need to write redundant code constructs like getters or setters again. Lombok will take the responsibility for generating the most common type of redundant programming constructs for you, just by using one or more of the annotations it offers. For example, by using lombok.Getter annotation on your class:

@Getter
public class Message {
    String title;
}

It would make your code just like if it was written like this:

public class Message {
    private String title;
    
    public String getTitle() {
        return title;
    }
}

Seems funny, right? In this tutorial, we’re going to have a quick introduction to Lombok, and how we can use it within our projects. To get started, it’s as simple as:

  1. Adding Lombok support to your IDE.
  2. Adding Lombok dependency to your maven’s project file: pom.xml.
  3. Start using it!

2. IDE Setup

As Lombok’s primary job is to auto-generate code blocks according to some giving annotations, it needs to plug into both: your IDE, and your build tool, in order to be able to do the auto-generation jobs behind the scenes. Therefore, Lombok (or maybe other parties) should provide a plugin to your IDE to support the code generating on the fly required by Lombok. Without this step, you’d go on with many syntax errors upon using auto-generated code blocks, as they wouldn’t be actually existing in the actual source files.

Whatever the IDE you’re using, rest assured that it has Lombok support. From your favorite IDE tab, follow the mentioned steps to get your IDE ready for using Lombok:

2.1. Eclipse

  1. Download lombok.jar.
  2. Run the Eclipse installer using the following command:java -jar lombok.jar.
  3. Click Specify Locations.
  4. Choose the directory where you’ve installed Eclipse.
  5. Click Quick Installer.
  6. Click OK to finish.
  7. Restart Eclipse.

To make sure that the plugin has been installed properly:

  1. Open Eclipse About Dialog.
  2. Check the following text at the end of the copyright text: Lombok v blah blah is installed. https://projectlombok.org.

To enable annotation processing for your project:

  1. In the menus bar, select File > Properties.
  2. Select Java Compiler > Annotation Processing.
  3. Check “Enable project specific settings”.
  4. Check “Enable annotation processing”.
  5. Click Apply and Close.

2.2. IntelliJ

  1. In the menus bar, select File > Settings.
  2. Select Plugins section, then click Browse repositories.
  3. In the search box, write “Lombok”.
  4. Select Lombok from the search results list, then click Install.
  5. Restart IntelliJ.

To enable annotation processing for your project

  1. In the menus bar, select File > Settings.
  2. Select Build, Execution, Deployment >  Compiler > Annotations Processors section.
  3. Check “Enable annotation processing”, then click OK.

2.3. NetBeans

  • There are no specific pre-required setup steps are required.

To enable annotation processing for your project:

  1. Download lombok.jar.
  2. Right-click project from Projects Window, select Properties.
  3. In Libraries section, add lombok.jar.
  4. In Build and Compiling section, check the ‘Enable Annotation Processing in Editor’ checkbox.

3. Maven Setup

Add the Lombok’s dependency to your maven’s project pom.xml:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
</dependency>

4. Generating Getters and Setters

Let’s see how can we use Lombok to apply encapsulation and generating getters and setters for some class on the fly. Assume we have the following class:

public class Message {
    String title;
    String body;
}

By adding lombok.Getterand lombok.Setter annotations to our class:

@Getter @Setter
public class Message {
    String title;
    String body;
}

Guess what? We have just defined getters and setters for all class fields on the fly! Easy right? The following code should work perfectly, in case you have enabled annotation processing support in your IDE:

Message msg = new Message();

msg.setTitle("hello lombok");
msg.setBody("i like auto generated getters/setters!");

System.out.println(msg.getTitle());
System.out.println(msg.getBody());

Now, just run the code, and see how Lombok’s simplicity is amazingly working yourself!

5. What Else Can Lombok Generate?

Rather than generating getters and setters, Lombok is able to generate other amazing things too:

  • Constructors.
  • equals() and hashCode() methods.
  • toString() method.
  • Non-null checking constructs.
  • Lazy loading constructs in getters.
  • Builder pattern implementation.
  • Logger object declaration.
  • Immutable objects implementation.
  • Throwing un-checked exceptions constructs.
  • Logger object declaration.

6. Why is Lombok Worth Using?

There’re two main benefits you’ll gain from using Lombok in your projects:

  • Less code to write: as you have seen earlier, you can avoid writing getters and setters with Lombok, and even mentioned other redundant things Lombok able to generate too.
  • Better readability of code: how many times you got bothered when you have to go into a specific place in your source file where there is some important code to modify, in the middle of a big code full of redundant lines? With Lombok, you’ll gain more readability by being able to understand exactly what a class offers (getters, setters, constructors) by just taking a look at its annotation, rather than having to navigate through its contents. Moreover, it makes the space fully available for only important and interesting code, that worth reading or modifying!

7. Conclusion

In this tutorial, we have learned how to get started with Lombok by setting up your development environment and using it to generate class fields getters and setters. In the next tutorial, we’re going to see in more details how can we use Lombok to generate bean specific features, like constructors, lazy loading, and much more.

8. Source Code

The source code of the examples mentioned here is available on GitHub.

Java Software Architect | Author

4 comments On Lombok: Getting Started

Leave a reply:

Your email address will not be published.

Site Footer