Home / What is Project Lombok and why you should use it?
The Project Lombok’s main goal is to reduce boilerplate code in Java. It provides bunch of annotations making classes much more cleaner and is able to generate required code during compilation. But before we immerse ourselves into the project’s goodies, let’s take a look into how annotations are processed in Java.
Lombok uses Pluggable Annotation Processing API which was introduced in Java 6 (2006 - ten years ago!). It allows programmers to write custom annotation processors that are plugged into compiling process. Processor receives a source code’s AST (abstract sytntax tree) which is a tree of nodes representing variables, operators, statements and others language components; imagine it as a Java files’s DOM. Having it, processor can analyze, validate or generate new code. Through the years multiple libraries started to use this API, the best known are probably JPA and GWT.
It is worth to note that Lombok does more then API allows: it modifies AST of a class. People behind project found an internal API which can be used for this purpose. It goes without saying that this aproach make them vulnerable to future JDK changes, but for now - and for many years - it works.
But let’s go back to Lombok. Below I present three of many available features that I instantly put into my project, which of course is only a subset of available functionalities.
Automatic getter/ setter/ constructor
As I mentioned in the first paragraph, C# already has this feature. Using the annotations programmer marks fields which should be accessed via getter or setter and doesn’t has to generate them with IDE. It may seem as a little gain, but think about the situation when you change parameter name or type - IDE’s refactor or manual work is required for that; Lombok makes it instantly.
Isn’t following code beautifully simple and readable? Note annotations can be used on a field or a class.
What’s more, finally it is possible to create real POJO/ DTO classes in Java:
Writing serious apps requires a lot of logging. The more information you log, the more repetetive declaring logger is, and usual you copy a piece of code from another class. With Lombok, it is dead simple:
Equals & hashcode generation
Equals & hashcode are usually another IDE-generated elements of a class. The more time goes by since a class creation, the less intuitive it is to recognise if some fields should be a part of those methods, or rather are intentionally omitted. With Lombok these problem is marginalised.
It is important to note that those features would be useless without a good code complation of modern IDE-s. Fortunately Intellij Idea - which I use and recommend - has the plugin that make finding Lombok-generated methods almost as easy as using standard ones. Eclipse and Netbeans are also supported. Finding usages on a field can point you to a used getter, and when you use an object Idea prompts methods. Example is shown below:
Lombok is an interesting library that makes Java code more readable and faster to write. There is a lot more features to play with and I encourage you to try it on your own, even with mentioned danger of changing internal API that project uses. Remember, Lombok doesn’t greatly intervene with your code, so abandoning it wouldn’t be a costly operation. There is a lot more to earn during fast development process, where classed are created and discarded, so you should give it a chance.