Saturday, October 17, 2009

Java Beans without getters/setters using lombok

Project Lombok is probably going to be one of the most useful library for Java. The library (requires Java 1.6) provides annotations that generate getters, setters, toString, hashCode, & equals for your POJOs. For example:
@Data // Lombok annotation
public class Person {
private String firstName;
private String lastName;
}

// Now your class has all the getters, setters,
// equals(), hashCode(), and toString()
C#/.NET has had this syntactic sugar built into the language for years but I was ambivalent about the approach. I'm more of a fan of languages with sparse syntax set such as C but the downside is that you end up writing much more boilerplate code. I think Lombok's approach is a nice compromise between bloated language feature set and having verbose code because of sparse language feature set. And it's supposedly supported in Eclipse.

No comments:

Post a Comment