Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Project Lombok: Put an End to Java Verbosity : Page 2

The introduction of Project Lombok, a combination compile-time and development-time code generator, has made Java POJO verbosity history. Add Lombok to your daily coding practices, and you won't go back.


advertisement

Project Lombok in Action

To fully grasp how Lombok decimates the number of lines in a typical Java POJO, consider the following typical Person entity:

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap