eclipse IDE Plugins JAVA spring boot

Project Lombok – How to reduce Java boiler plate codes

Learn how to install the Lombok opensource library and how to use its useful features to reduce boiler plate codes.

While developing Java applications we may need to write codes that we use repeatedly in multiple locations. We can eliminate these boilerplate codes by using the project Lombok, an open-source java library that generates boilerplate codes for us.

Using the Lombok java library also makes our java classes simple, easy to maintain, and reduces the lines of code.

In this article, we will learn how to install the Lombok library and how to use a few of its useful features.

Table of Contents

Using the lombok library by adding the maven dependency

Add the following maven dependency into the application’s pom.xml file to add the Lombok library into our application.

<dependencies>
        <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <version>1.18.8</version>
        </dependency>
</dependencies>

In addition, we can find the Latest Lombok library version on the official website.

Installing Lombok in Eclipse/STS IDE

Adding the Lombok library to classpath is not sufficient. We also need to add the Lombok plugin to the IDE. For this, we can navigate to the folder where the Lombok library jar resides and execute it.

If we are using the maven build tool, we can find the downloaded Lombok jar under the .m2 maven local repository.

We can execute the jar by Right click > open or using the java -jar lombok-version.xxx.jar command from the command prompt.

Specify the IDE location by selecting the .exe file of the IDE by selecting the Specify Location option on the installer window.

Click on the Install/Update button to install the Lombok plugin. We will get the installation successful message as shown in the below image.

Also, click on the Quit Installer button to close this window.

Finally, after successful installation, close and open the IDE again.

Using the Lombok features on our Java code

In the previous part, we learned how to install the Lombok plugin in Eclipse/STS IDE.

In this section, we will also learn about important features offered by the Lombok library.

@Getter and @Setter annotations

In the normal java code, we will have the getter and setter methods for the fields of a class as shown below.

public class Employee {
	
	private String id;
	private String name;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Now, we can replace this code simply by using the @Getter and @Setter Lombok annotations as shown below.

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Employee {
	
	private String id;
	private String name;
}

@ToString annotation

We may have to override the toString() method if we want to debug and print our object field values to the console or a log file.

Without using Lombok, our class may look like this.

public class Employee {
	
	private String id;
	private String name;
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + "]";
	}
}

Because of the Lombok, we can use the @ToString annotation and we can remove the traditional method as shown below.

Advertisements

We can also exclude fields by using the field annotation @ToString.Exclude and Lombok will not consider the excluded fields while generating the toString() method.

import lombok.ToString;

@ToString
public class Employee {
	
	private String id;
	
	@ToString.Exclude
	private String name;
}

@EqualsAndHashCode annotation

We can also use the @EqualsAndHashCode annotation to override the equal(), and the hashcode() methods of our java class.

Constructor annotations

In addition, Lombok provides annotations to create different types of constructor methods.

The below are the annotations available.

@NoArgsConstructor

We can use this annotation to create a constructor which accepts no arguments.

@AllArgsConstructor

We can use this annotation to create a constructor which accepts all available fields as input arguments.

@RequiredArgsConstructor

We can use this annotation to create constructors with one parameter for each available field, that requires special handling.

@Data annotation

In addition, we can combine the @Setter, @Getter, @EqualsAndHashCode, @ToString, and the @RequiredArgsConstructor by using the convenient Lombok annotation @Data.

Lombok Logging support

The Lombok library also supports different logging libraries by offering different annotations.

A few of the examples are shown below.

@Log annotation

We can use the @Log annotation at any class level to get the Java util logging support as shown below.

This will automatically create a static final log field, that can be used inside the class methods for a logging operation.

import lombok.extern.java.Log;

@Log
public class TestLogger {
	
	public void myMethod() {
		log.info("Java util Logger example using Lombok!!");
	}
}

Below are a few of the other logger annotations supported by the Lombok.

  • @CommonsLog : For Apache commons logging API support.
  • @Flogger : For Fluent logging API support.
  • @JBossLog : For JBoss logging API support.
  • @Log4j : For Apache Log4J logging API support.
  • @Slf4j : For Slf4j logging API support.

@Builder annotation

The Lombok supports @Builder annotation, which we can use to implement the builder pattern for our java class.

We can also apply this annotation to a class or a method of a class.

For more features read the official Lombok documentation.

Conclusion

In conclusion, the Lombok is a very useful java open source library, which can be used to avoid boilerplate codes in our application.

It also provides multiple annotation supports, which reduces the number of codes that need to be written and makes the java code cleaner and better.