n my never-ending search for interesting new ideas in the IT world, every now and then I come across something truly innovative and brilliant. Such was the case when I first stumbled upon the initial announcement of Project Lombok. In the two months since then, Project Lombok has become an indispensable part of my Java tool chain and I can't imagine coding without it.
Basically, Lombok integrates directly into the Eclipse compilation cycle (by manipulating the abstract syntax tree of your code as you type) and generates code immediately based on annotations. The generated code is visible to all other classes instantly.
What type of code can Lombok generate from annotations? Most importantly, it generates the basic boilerplate stuff that makes Java classes look so verbose, namely:
At the same time, Lombok provides automatic resource management. For example, it always closes your streams safely, without the need for try/catch/finally statements.
![]() | |
| Figure 1. The Lombok Installation Wizard: Upon running the lombok.jar file, a simple wizard will pop up. |
Just point to your Eclipse executable and press "Install/Update." You will need to do this for every new version of Lombok.
To include Maven support, just add the Project Lombok repository and dependencies to your pom.xml, as per the instructions on the project web site. After that, Lombok will work with Maven's compilation lifecycle out of the box.
public class Person {
private Long personId;
private String salutation;
private String firstName;
private String middleName;
private String lastName;
private String phoneNumber;
private String email;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String country;
private Calendar birthDate;
}
In order to make this class a proper Java POJO, you have to code the getters, setters, toString(), equals(), and hashCode() for it. If you used Eclipse's automatic code generation features, the Person POJO would mushroom to more than 240 lines of code, most of which is just plumbing (see Listing 1).
Here's what the same POJO would look like after being "Lombok-omized":
@Data
public class Person {
private Long personId;
private String salutation;
private String firstName;
private String middleName;
private String lastName;
private String phoneNumber;
private String email;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String country;
private Calendar birthDate;
}
That's it! When Lombok is installed into your Eclipse runtime, a simple @Data annotation makes all the standard plumbing code magically appear. This makes defining JPA/Hibernate entities incredibly easy and fast.
But wait, there's more! All the getters and setters appear in the outline, as if the code were actually there (see Figure 2). What's more, all the generated methods are available as if they were actually there. They show up in code completion (see Figure 3).
![]() Figure 2. Auto-Generated Getters, Setters, equals(), hashCode() and toString(): All the getters and setters appear in the outline, as if the code were actually there. |
Figure 3. Auto-Generated Methods Visible via Code Completion: All the generated methods are available as if they were actually there. They show up in code completion. |
If you need more fine-grained control, Project Lombok offers @Getter, @Setter, @ToString, and @EqualsAndHashCode annotations as well. The previously mentioned @Data annotation combines them all into one. In our production code, @Data is the annotation I use 99% of the time.
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
This feature offers automatic resource management in Java 6 today; you don't have to wait for Java 7.
@Synchronized
public static void hello() {
System.out.println("world");
}
@SneakyThrows
public void run() {
throw new Throwable();
}
Whether or not you choose to employ this feature (I have mixed feelings about it), it's a useful option to have.
The long-term Lombok plans are far more grandiose. The two Project Lombok authors (Reinier Zwitserloot and Roel Spilker) want to intercept the Eclipse compilation process to the point where they can actually add new features to Java, particularly real closures. Read more about this ambitious goal at this Google Groups thread.
However, all Eclipse shops should look into adding Lombok into their daily tool chains today.
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |