Reduce Code Noise in Plain Old Java with Lombok

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:

package test.lombok;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@EqualsAndHashCode(exclude="expired")
public @Data class User {

	private int id;
	private String username;
	private String email;
	@Getter(AccessLevel.PRIVATE) private boolean expired;
	
	public static void main(String args[]){
		// TODO 		
	}
}

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:

package test.lombok;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@EqualsAndHashCode(exclude="expired")
public @Data class User {

	private int id;
	private String username;
	private String email;
	@Getter(AccessLevel.PRIVATE) private boolean expired;
	
	public static void main(String args[]){
		 		
		User u1 = new User();
		u1.setId(10);
		u1.setUsername("Tom");
		u1.setEmail("tom@tom.com");
		u1.setExpired(false);
		
		System.out.println("U1 =====================");
		System.out.println(u1.toString()); // @Data, auto generated to String
		System.out.println(u1.hashCode());
		
		User u2 = new User();
		u2.setId(10);
		u2.setUsername("Tom");
		u2.setEmail("tom@tom.com");
		u2.setExpired(true);
		
		System.out.println("U2 =====================");
		System.out.println(u2.toString());
		System.out.println(u2.hashCode());
		
		System.out.println("U1 equals to U2? :" + u1.equals(u2));
		
		u2.setUsername("Jerry");
		
		System.out.println("U2 =====================");
		System.out.println(u2.toString());
		System.out.println(u2.hashCode());
		
		System.out.println("U1 equals to U2? :" + u1.equals(u2));
		
	}
}

And here are the output;

U1 =====================
User(id=10, username=Tom, email=tom@tom.com, expired=false)
954413294
U2 =====================
User(id=10, username=Tom, email=tom@tom.com, expired=true)
954413294
U1 equals to U2? :true
U2 =====================
User(id=10, username=Jerry, email=tom@tom.com, expired=true)
-1127824222
U1 equals to U2? :false

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


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply