Java Project Lombok Tutorial | Getting Started with Lombok

There are libraries and frameworks that are part of the daily tools of a Java developer. Be it the preferred test framework or useful libraries like Guava. For me, Project Lombok has been part of this for a long time . It fulfills three criteria that are very important to me in everyday developer life: Lombok allows me to avoid superfluous boilerplate code, reduces sources of error and works very well with other frameworks.

Why I’m so excited about Lombok can be demonstrated by looking at some code that can be found in many Java projects:

public  class person {     private  string firstName ;     private  string familyName ;     public person ( String firstName, String lastName ) {         this . firstName  = firstName ;         this . lastName  = lastName ;     }     public  String getFirstName ( )  {         return firstName ;     }     public  void setFirstName ( String firstName )  {         this . firstName  = firstName ;     }     public  String getFamilyName ( )  {         return familyName ;     }     public  void setFamilyName ( String familyName )  {         this . familyName  = familyName ;     }     @Override    public  boolean equals ( Object o )  {         if  ( this  == o )  return  true ;         if  ( o ==  null  || getClass ( )  ! = o. getClass ( ) )  return  false ;         Person person =  ( person ) o ;         if  ( firstname =!  null  ?  ! firstname. equals ( person. firstname )  : person. firstname  =!  null )  return  false ;         return  ! ( Family name ! =  Null  ?  ! Family name. Equals ( person. Family name )  : person. Family name  ! =  Null ) ;     }     @Override    public  int hashCode ( )  {         int result = firstName ! =  null  ? first name. hashCode ( )  :  0 ;         result =  31  * result +  ( familyName ! =  null  ? familyName. hashCode ( )  :  0 ) ;         return result ;     }     @Override    public  String toString ( )  {         return  "Person {"  +                "firstName = '"  + firstName +  ' \ ' '  +                ", familyName = '"  + familyName +  ' \ ' '  +                "}" ;     } }

What immediately stands out: In Java, a relatively large amount of code is necessary to express very little. In fact, leave it to many developers the IDE to generate the code and here begins the problem: Once a class is changed, must also equals, hashCodeand toStringtaken into account and new getters and setters are written. Of course, this is especially prone to errors when under time pressure still a class must be adjusted quickly. Languages ​​like Scala and Groovy can definitely do this better.

This is where Lombok comes in, because with a single annotation, the same class looks much clearer:

@Data public  class Person {     private  string firstName ;     private  string familyName ;     public person ( String firstName, String lastName ) {         this . firstName  = firstName ;         this . lastName  = lastName ;     } }

Lombok generates for all of us getters, setters, and hashCode, equalsand toString. Since it all happens at compile time, Lombok also works very well with other frameworks like JPA.

Also for the generation of constructors there are annotations ( @Data) from Lombok:

@data@AllArgsConstructorpublic  class person {     private  string firstName ;     private  string familyName ; }

Another advantage: Lombok complements only the missing methods. So if there is a getter or setter that deviates from the standard, it can easily be added. This is then in the class also much stronger.

@data@AllArgsConstructorpublic  class person {     private  string firstName ;     private  string familyName ;     public  void setFamilyName ( String familyName ) {         if ( familyName ==  null  || familyName. isEmpty ( ) ) {             throw  new  IllegalArgumentException ( "Family name must be set." ) ;         }         this . familyName  = familyName ;     } }

But Lombok can do even more: Once inheritance comes into play, is easy to specify whether hashCode, equalsand toStringshould also take into account the fields of the parent class:

@data@EqualsAndHashCode ( callSuper =  true ) @ToString ( callSuper =  true ) public  class Developer extends Person {     private  String favoriteFramework ; }

The area of ​​application of Lombok is not limited to writing Java POJOs with less code. Also for other things Lombok can be used:

Use Lombok in the project

All necessary information about the Maven coordinates (or Ivy and Gradle) can be found on the download page . This page also lists the IDE plugins that offer Lombok support. This is especially necessary for functions such as autocomplete.

For Eclipse it is enough to load the current version of Lombok and execute the jar. It opens a graphical interface that guides the user through the installation. In IntelliJ the plugin can be installed via the plugin administration.