Lombok: Java with Suggar

Java 8 made java better but still not enough, we still dont have some great stuff that is available in scala like pattern matter, case classes, type system and mechanisms like dynamics and other stuff.

So is possible to add some sugar to java code with the Lombok project. We can get rid of several boilerplate code.

The idea behind lombok is very simple it added data on the bytecode but it dont add code in you java class.

Yeah its like a compiler :-) IF you cant use scala yet at leat you can add another jar in your classpath that will make the difference.

So lets go down to the code and see some cool stuff with can do with the Lombok project. You also can check some extensions here: lombok-ext.

Getters / Setters and much more

If you just add the @Getter and @Setter annotations lombok will generate the getter and setters for you.  Check it out on the code.


If you check the outline in eclipse for instance you gonna see the magic. :-)

As you see lombok generate the getters and setters, but wait a moment, there is this: equals, hascode and toString methods as well, this is from the @Data annotation that add getters and setters plus this 3 other method i mentioned. :-)

What about a DSL / Builder ? Done.


You just need add the annotation @Builder and that it. You also can defined as fluent Interface you can do the nested chains of . calls. You can create this coding but is a lot of code and is very annoying to make all method hold a variable and also return this. You can configure some method names of the builder if you like as well. @ToString just added the toString method for all fields, very handy as well.

You can get my eclipse code here in my github and also check it the maven config to make it build lombok proper. When you download lombok jar you need run $ java -jar lombok.jar and point to your eclipse installation when a windows pop up this will make sure your eclipse knows how to deal with lombok. 

Cheers,

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java