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:
|
|
It would make your code just like if it was written like this:
|
|
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:
- Adding Lombok support to your IDE.
- Adding Lombok dependency to your maven’s project file: pom.xml.
- 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
- Download lombok.jar.
- Run the Eclipse installer using the following command:java -jar lombok.jar.
- Click Specify Locations.
- Choose the directory where you’ve installed Eclipse.
- Click Quick Installer.
- Click OK to finish.
- Restart Eclipse.
To make sure that the plugin has been installed properly:
- Open Eclipse About Dialog.
- 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:
- In the menus bar, select File > Properties.
- Select Java Compiler > Annotation Processing.
- Check “Enable project specific settings”.
- Check “Enable annotation processing”.
- Click Apply and Close.
2.2. IntelliJ
- In the menus bar, select File > Settings.
- Select Plugins section, then click Browse repositories.
- In the search box, write “Lombok”.
- Select Lombok from the search results list, then click Install.
- Restart IntelliJ.
To enable annotation processing for your project
- In the menus bar, select File > Settings.
- Select Build, Execution, Deployment > Compiler > Annotations Processors section.
- 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:
- Download lombok.jar.
- Right-click project from Projects Window, select Properties.
- In Libraries section, add lombok.jar.
- 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:
|
|
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:
|
|
By adding lombok.Getterand lombok.Setter annotations to our class:
|
|
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:
|
|
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.