Beliebte Suchanfragen
|
//

Less but more expressive code with Project Lombok

19.11.2015 | 3 minutes of reading time

There are libraries and frameworks that belong to the daily tools of a Java developer, e.g. your preferred testing framework or useful libraries like Guava. For some time now, I have used a library that meets three criteria that are essential to my daily work: Project Lombok . It allows me to avoid boilerplate code, it reduces errors, and it plays very well together with other frameworks.

But first, to show you why I’m so thrilled about Lombok, let us have a look at some code that can be found in many Java projects:

1public class Person {
2    private String firstName;
3    private String familyName;
4 
5    public Person(String firstName, String lastName){
6        this.firstName = firstName;
7        this.lastName = lastName;
8    }
9 
10    public String getFirstName() {
11        return firstName;
12    }
13 
14    public void setFirstName(String firstName) {
15        this.firstName = firstName;
16    }
17 
18    public String getFamilyName() {
19        return familyName;
20    }
21 
22    public void setFamilyName(String familyName) {
23        this.familyName = familyName;
24    }
25 
26    @Override
27    public boolean equals(Object o) {
28        if (this == o) return true;
29        if (o == null || getClass() != o.getClass()) return false;
30 
31        Person person = (Person) o;
32 
33        if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return false;
34        return !(familyName != null ? !familyName.equals(person.familyName) : person.familyName != null);
35    }
36 
37    @Override
38    public int hashCode() {
39        int result = firstName != null ? firstName.hashCode() : 0;
40        result = 31 * result + (familyName != null ? familyName.hashCode() : 0);
41        return result;
42    }
43 
44    @Override
45    public String toString() {
46        return "Person{" +
47                "firstName='" + firstName + '\'' +
48                ", familyName='" + familyName + '\'' +
49                '}';
50    }
51}

One thing which stands out immediately: In Java, you sometimes need quite a lot of code to express very little. In fact, many developers let their IDE generate most of this code, and this is the main problem: Once a class must be changed, you have to write new getters and setters, equals, hashCode, and toString. Of course, this is error-prone, especially if you have to adjust a class under time constraints and pressure.

This is where Lombok comes into play: Only a single annotation (@Data) is needed to make the class look clearer:

1@Data
2public class Person {
3    private String firstName;
4    private String familyName;
5 
6    public Person(String firstName, String lastName){
7        this.firstName = firstName;
8        this.lastName = lastName;
9    }
10}

Lombok generates all getters, setters as well as hashCode, equals and toString. This happens at compile time. Thus, Lombok plays well with other frameworks such as JPA.

There is an annotation to generate constructors, too:

1@Data
2@AllArgsConstructor
3public class Person {
4    private String firstName;
5    private String familyName;
6}

Another benefit: Lombok only generates missing methods. If there is a getter or setter in your code that is more specific, you can add it to your normal code. It will then be more noticeable than before.

But Lombok can do even more: Once inheritance comes into play, it is easy to specify whether equals, hashCode, and toString should also take account of fields of the parent class:

1@Data
2@EqualsAndHashCode(callSuper = true)
3@ToString(callSuper = true)
4public class Developer extends Person{
5  private String favoriteFramework;
6}

Lombok is not limited to writing Java POJOs with less code, though. There are other use cases, too:

  • Lombok provides annotations for all common logger frameworks to create a logger in a class.

  • @Builder generates a builder-api for your POJO.

  • more usecases can be found in the documentation .

Use Lombok in your project

The Maven coordinates (or Ivy and Gradle) can be found on the download page . On this page the IDE plugins, offering Lombok support, are listed, too. These are especially necessary for features like autocompletion.

For Eclipse, it is sufficient to download the latest version of Lombok and execute the JAR. This will open a graphical interface that guides you through the installation process. In IntelliJ the plugin can be installed via plugin management.

So if you like to write less code, give Lombok a try. What are your favorite Java frameworks and everyday helpers and why? Leave a comment and tell more about your tools.

|

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.