How to simplify your Java code with Lombok Annotations
If you’re a professional Java developer, you probably use IntelliJ IDEA as your IDE and Lombok as the framework that handles the Java boilerplate. What you probably didn’t know is that not only do these 2 technologies work well on their own, but when combined, they’re even more efficient than when used individually.
IntelliJ IDEA comes bundled with the Lombok plugin which makes Lombok even more useful and convenient. In fact, it’s so highly valued that in the past, when the plugin was not bundled, many users would not upgrade until the plugin was compatible with the newer version of IntelliJ IDEA.
Today, let’s explore Lombok support in IntelliJ IDEA and see how they make a project cleaner, less verbose, easier to maintain, and more pleasant to work with.
Let’s start by cloning the canonical spring-petclinic project.
Then, make sure that the Lombok plugin is enabled:
Now, let’s add Lombok to the project dependencies. We can do that either through the build script, or we can just start using Lombok annotations, and IntelliJ IDEA will suggest adding the missing dependency.
Now that we’ve made sure that the plugin is enabled and the dependency is on the classpath, let’s look at the features.
There are plenty of trivial accessor methods in our project, so let’s start by replacing them. Whenever IntelliJ IDEA recognizes that a member can be replaced with a Lombok annotation, it suggests a quick-fix:
The same works for ubiquitous loggers, trivial
hashCode() implementations, constructors, and more.
Because IntelliJ IDEA knows the corresponding bytecode will be generated at compilation, the Lombok code is not red. All the essential features, like navigation and rename refactoring, also work well with such code.
For example, we can search for the usages of a backing field whose accessor methods were replaced with Lombok annotations. Accessor calls are found just as if the boilerplate was in place:
If there’s a conflict between the annotations, or another Lombok-related problem, IntelliJ IDEA will detect the issue right away. This is handy because otherwise such an error would not be visible until compile-time, which increases the feedback loop and consequently wastes your time.
These checks are implemented in the form of a dedicated inspection set, which you can adjust according to your preferences:
Lombok configuration files
If your project uses the Lombok Configuration System to customize Lombok features and behavior, then IntelliJ IDEA will help you configure this as well:
You can use basic code intelligence features, such as code completion within
Automatic conversion to Lombok and back
Are you new to Lombok? Do you want to quickly evaluate how your project will look with Lombok annotations? It’s easy with the Lombok action, which you can invoke for an entire class.
IntelliJ IDEA will then suggest which annotations are available for this class:
On the other hand, if you want to remove Lombok annotations from your project, this is also possible through the Delombok action. This saves you time and averts potential human error when removing the annotations and trying to preserve the semantics of the original code.
We hope that this post helped you learn more about Lombok support in IntelliJ IDEA. As we’ve just seen, Lombok can make your project cleaner, less verbose, and easier to maintain, while IntelliJ IDEA adds the essential features like refactoring, navigation, and static analysis.
As always, we’d love to hear your feedback. You can leave a comment below or use our issue tracker for feature requests.
Subscribe to Blog updates
Build a Spring Boot App with AI Assistant
In this article, we’ll be building a small Java Spring Boot web application in IntelliJ IDEA with the help of JetBrains AI Assistant. If you’d like to follow along with this tutorial, you’ll need IntelliJ IDEA Ultimate, as well as an active subscription to AI Assistant or the free seven-day trial…
Java Best Practices
Good code follows certain rules, and knowing them increases your chances of success. We’d like to share some Java best practices that will help you on your way. We'll cover the must-know tips and tricks, from broad advice on software development to Java- and project-specific know-how. Let's get star…
The New Terminal (Beta) Is Now in JetBrains IDEs
The new terminal (Beta) is now in JetBrains IDEs and will be available in the new UI starting from the 2024.1 version of the IDEs.
‘HelloWorld’ and ‘main()’ meet minimalistic
"Just ignore the terms class, public, static and arg for now; we'll talk about it later". If you have ever mentioned these lines to a new Java student, who is about to execute their first 'HelloWorld' program, know that this is changing. If you are that student, well, congratulations, getting starte…