DEV Community

Cover image for Cleaner Data classes with Project Lombok
pazvanti
pazvanti

Posted on • Updated on • Originally published at petrepopescu.tech

Cleaner Data classes with Project Lombok

Article originally posted on my personal website under How to use Lombok in your Java projects

One thing that I like about Java is the vast amount of libraries that are freely available. There is a library or code snippet for almost anything that you could think of or any mundane or repetitive task out there. Data classes are no exception. All applications rely on data and the presence of data classes is widespread, however, they are usually quite simple, but can take up a lot of time, especially if you have many fields. Luckily, there is Project Lombok, which makes things easier to maintain, read and code.

Play Framework Java Course

What is Project Lombok

Lombok is a library that will automatically generate getters, setters, constructors, and many more for your data classes. You only need to import the library and use the annotations to achieve the desired effect. The code is added at build-time, so your classes remain small and are easier to read.

It may not seem as much, but for data classes that have many fields, it can save a lot of coding and maintenance time. Furthermore, it can do more, like generating a toString method that includes the values of all the fields, null checks and a builder for your class. These are extremely useful if your data classes evolve over time because the needed functionality is there just by adding or removing the variable of interest.

A quick example in Lombok

Let us look at a quick example that demonstrates the power of Project Lombok. We will be creating a “Student” class that has a few fields: surname, given name, the city where he was born, and a few grades. We will need to have a builder for the student, but also a more traditional way of instantiating it by calling the constructor. Also, since this is a data class, we will need getters and setters for all the fields.

Regardless of the number of fields, Lombok makes this really easy. We just create our class, define the fields and add the needed Annotations.

@Data // generates the getters and setter
@NoArgsConstructor // generates the constructor without any arguments
@AllArgsConstructor // generates the constructor that has all the fields as arguments
@Builder // generates a builder for the class
@ToString // generates a toString() method that has the values of all the fields
public class Student {
    private String surname;
    private String givenName;
    private String city;
    private Double mathGrade;
    private Double computerScienceGrade;
    private List<Double> labGrades;
}
Enter fullscreen mode Exit fullscreen mode

If after that we want to add a new field or remove an existing one, we don’t need to change the builder or the toString method in any way. Everything is done by Lombok. Furthermore, the toString that is being generated makes things really convenient from a logging point of view. It automatically includes all the defined fields and can even print complex fields, like the List of lab grades. Here is an example and the log output below:

public static void main(String[] argv) {
    Student student = Student.builder()
            .surname("Popescu")
            .givenName("Petre")
            .city("Paris")
            .mathGrade(8.25)
            .labGrades(Arrays.asList(9.0, 8.7, 9.4))
            .build();

    LOGGER.info(student);
}
Enter fullscreen mode Exit fullscreen mode

There are more features available and you can read all about them on Project’s Lombok website.

Using Lombok in IntelliJ

Since the code for the additional features is generated during build time, your IDE will probably not recognize the getters, setter and features that are added by the annotations. At least, not out of the box. However, it is fairly easy to fix this. If IntelliJ does not recognize Lombok’s features, you will have to download the Lombok plugin from the IntelliJ market. It is completely free.

After that, you will have to enable Annotation processing. This will tell IntelliJ to pre-process annotations so that the features that they add are available for the IDE and it can offer auto-complete and code analysis for it. To do this, go to Settings -> Build, Execution, Deployment -> Annotation Processor (or simply search for Annotation in the top search box) and enable it

That’s it. You can now use Project Lombok in your code and the IDE will properly recognize its features.

Article originally posted on my personal website under How to use Lombok in your Java projects

Top comments (0)