Reduce Code Noise in Plain Old Java with Lombok

February 18th, 2010 thiamteck Posted in API, Java No Comments »

In dynamic languages such as Groovy, each attribute have its’ own pair of implied getter and setter method.

Lombok offer same convenience to plain old Java, via Annotation. Below is an example:

The only annotation that do the magic is @Data. Getter and setter method for all field are available in JavaBean convention. You may optionally change the access modifier with annotation @Setter and @Getter.

Lombok is design to integrate with Eclipse, after update your Eclipse with Lombok, all methods implied will be available for code completion:

Lombok is more than just getter setter. toString(), equals(), and hashCode() methods are also available. As we know, even experienced developers might confuse about hashCode() and equals().Joshua Bloch spends chapters in his best selling book “Effective Java” to explain them.

So auto generate of hashCode() and equals() methods are really very handy, not just improve productivity, but also accuracy. You may customize the hashCode() and equals() with @EqualsAndHashCode. Lets add in some code to test the output:

And here are the output;

In conclusion, Lombok can help developers to improve productivity and improve readability of code. The implied hashCode() is the killer features of Lombok. The drawback is the JAR file with file size of 1.85MB.

Reference:
[1] http://www.ibm.com/developerworks/java/library/os-lombok/index.html

AddThis Social Bookmark Button

OpenSMPP/SMS Tools - Java Open Source SMPP API

November 14th, 2008 thiamteck Posted in API, Java No Comments »

AddThis Social Bookmark Button

Java Logging Wrapper with Static Binding - SLF4J

September 16th, 2008 thiamteck Posted in API, Java 2 Comments »

AddThis Social Bookmark Button