Photo by Tikkho Maciel on Unsplash

There are many issues with writing boilerplate code: It’s hard to identify code that deviates from normal, it adds visual complexity to a file, and it’s not fun to write. Unfortunately in Java there are constructors, getters, and setters that need to be written for almost any class.

public class Hotel {
	private final String name;
	
	public Hotel(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}
}

Lombok provides a cleaner interface for adding these functions with annotations. When you add an annotation to your Java class, Lombok will generate code for you behind the scenes.

Getters and Setters

You can instruct Lombok to create getters and setters by adding a @Getter and @Setter annotation on the private field. This is equivalent to writing out those methods yourself.

public class Hotel {
	@Getter
	private final String name;

	@Getter @Setter
	private boolean open;
}

hotel.getName()
hotel.isOpen()
hotel.setOpen(false)

Lombok will prevent you from creating a setter for a final field.

@Getter @Setter // The final field Hotel.name cannot be assigned
private final String name;

NonNull

If you want to add null checks where appropriate, you can do so with the @NonNull annotation. Lombok then will add null checks where appropriate and throw a null pointer exception if a null is passed.

//Hotel.java
@Getter @Setter @NonNull
private String managerOnDuty;

//App.java
hotel.setManagerOnDuty(null) // NullPointerException

Constructors

Lombok provides three annotations for creating a constructor for a class. These can be used simultaneously to create multiple constructors if desired.

@NoArgsConstructor - Creates a constructor with no arguments. This will result in a compiler error if not possible due to final fields.

@RequiredArgsConstructor - Creates a constructor with arguments for required (final, or non initialized @NonNull) fields.

@AllArgsConstructor - Creates a constructor with arguments for all fields.

@NoArgsConstructor
public class Hotel {
	private final String name;
    
 	@NonNull
	private String managerOnDuty;
    
	private String restaurantSpecial;
}

Hotel hotel = new Hotel();

@RequiredArgsConstructor
public class Hotel {
	private final String name;
    
	@NonNull
	private String managerOnDuty;
    
	private String restaurantSpecial;
}

Hotel hotel = new Hotel(“Sheraton”, “Sally”);
Hotel hotel2 = new Hotel(“Sheraton”, null); // NullPointerException

@AllArgsConstructor
public class Hotel {
	private final String name;
    
	@NonNull
	private String managerOnDuty;
    
	private String restaurantSpecial;
}

Hotel hotel = new Hotel(“Sheraton”, “Sally”, “Lobster Roll”);

Other Utility Methods

Quite frequently when writing a class you’ll want a toString method to help debug and equals and hashCode methods to better support object equality. Implementing these methods by hand can be burdensome, and if done incorrectly may lead to bugs. Luckily Lombok provides @ToString and @EqualsAndHashCode annotations to auto generate these functions.

Tying it all together

With the annotations mentioned above, you may end up with a class that looks like this fairly often.

@RequiredArgsConstructor
@ToString
@EqualsAndHashCode
public class LombokHotel {
	@Getter
	private final String name;
	
	@NonNull @Getter @Setter
	private String managerOnDuty;
	
	@Getter @Setter
	private String restaurantSpecial;
}

The developers of Lombok realized this combination of annotations is fairly common together, so they provided a @Data annotation that does all of this for you!

@Data
public class LombokHotel {
	private final String name;
    
	@NonNull
	private String managerOnDuty;
    
	private String restaurantSpecial;
}

Conclusion

Lombok offers annotations that reduce the amount of boilerplate code that needs to be for your Java classes. For more information you can find the Lombok documentation here.

Check out last week's blog post on reducing null pointer exceptions with Optional.