While developing Java applications we may need to write codes which are repeatedly used in multiple locations like getters and setters, toString methods, etc. These boiler plate codes can be eliminated by using project lombok, an opensource java library which generates boiler plate codes for us.

Using the lombok java library makes our java classes simple, easy to maintain and reduces the lines of code. In this article, we will learn how to install lombok library and how to use few of its useful features.

Using lombok library by adding maven dependency

To add lombok library into our application, add the following maven dependency into application’s pom.xml. Latest Lombok library version can be found in official website.

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

Installing Lombok in Eclipse/STS IDE

Adding the lombok library to class path is not sufficient. We need to add the lombok plugin to our IDE. For this, we can navigate to the folder where lombok library jar resides and execute it.

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

We can execute the jar by Right click > open or using java -jar lombok-version.xxx.jar command from command prompt. This will open up the installer window. Specify the IDE location by selecting .exe file of IDE by selecting the Specify Location option as shown below.

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

After successful installation, close and open the IDE again, so that the changes gets applied.

Using the Lombok features on our Java code

In the previous part we learned how to install Lombok plugin in Eclipse/STS IDE. In this section, we will learn about important features offered by the lombok library.

@Getter and @Setter annotations

In normal java code, we will have getter and setter methods for the fields of a class as shown below. These methods are used to access or set field value using object reference from outside of the class.

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;
	}
}

This code can be simply replaced by using @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 toString() method, if we want to debug and print our object field values to 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 + "]";
	}
}

With Lombok, we can use @ToString annotation as shown below. We can also exclude fields by using field level annotation @ToString.Exclude. Excluded fields are not considered by lombok while generating the toString() method.

import lombok.ToString;

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

@EqualsAndHashCode annotation

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

Constructor annotations

Lombok supports annotations to create different types of constructor methods. Following are the annotations available.

@NoArgsConstructor

This annotation can be used to create constructor which accepts no arguments.

@AllArgsConstructor

This annotation can be used to create constructor which accepts all available fields as input arguments.

@RequiredArgsConstructor

This annotation can be used to create constructors with one parameter for each available field, that requires special handling.

@Data annotation

We can combine @Setter, @Getter, @EqualsAndHashCode, @ToString and @RequiredArgsConstructor by using convenient Lombok annotation @Data.

Lombok Logging support

Lombok library supports different logging libraries by offering different annotations. Few of the examples are shown below.

@Log annotation

We can use this 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 logging operation.

import lombok.extern.java.Log;

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

Few of the other logger annotations supported by Lombok are :

  • @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

Lombok supports @Builder annotation, which can be used to implement builder patter for our java class. This annotation can be applied to a class or a method of a class. For more features read the official Lombok documentation.

Conclusion

Lombok is a very useful java open source library, which can be used to avoid boiler plate codes in our application. It provides multiple annotation supports, which reduces number of codes needs to be written and makes the java code cleaner and better. Let us all use this Lombok library in our code 🙂 Happy coding.. 🙂 🙂

You May also Interested In