Skip to main content

Lombok

Lombok

Introduction

EasyRPA engine comes with built-in Lombok library. It is compile-time code generator which helps to get rid of repetitive code structures as well as unify the development practice across the team.

Any popular IDE today supports Lombok through plugins, read more about this in Install Lombok Plugin.

Lombok API includes stable and experimental features. It is recommended to stick to the stable since it is well tested, long supported and less likely to be changed.

We only introduce the most popular Lombok applications, More of them can be found in the official documentation

Stable Features

@Slf4j

Generates slf4j logger field in a target class

@Slf4j
public class CalculatorMainPage extends ScreenPage {

	public void displayText() {
		log.info("Displaying text from calculator page");
	}
}

@Getter and @Setter

Generates getter and setter methods respectively for a target field

class Person {
	@Getter
	@Setter
	private String name;

	Person (String name) {
		this.name = name;
	}
}

class AppMain {
	public static void main(String[] args) {
		Person person = new Person("Ellie");
		System.out.println(person.getName());

		person.setName("Abby");
		System.out.println(person.getName());
	}
}

Annotations can also have modifications. One of them is @Getter(lazy=true) which allows lazy loading.

It means the underlying field will only be initalized when the getter is called first time and the result will be cached for later reuse.

@AllArgsConstructor

Generates the constructor receiving all the class members as arguments

@AllArgsConstructor
class Cat {
	private String name;
	private int age;
}

public static void main(String[] args) {
	Cat cat = new Cat("Felix", 8);
}

Lombok provides other annotations to generate constructors: 

@NoArgsConstructor creates the default constructor i.e. without parameters

@RequiredArgsConstructor creates the constructor with only final and @NonNull fields

@ToString

As follows from its name, @ToString generates self-titled method.

class LocalRunner {
	@ToString
	static class Product {
		String name = "Butter";
		int price = 2;
	}

	public static void main(String[] args) {
		Product product = new Product();
		System.out.println(product.toString());
	}
}

If we run the above code, the output will look something as LocalRunner.Product(name=Butter, price=2) instead of eu.ibagroup.easyrpa.lombok.LocalRunner$Product@121714c

@Builder

The builder pattern simplifies the construction of class with large number of parameters especially when most of them are optional.

@Builder
public class NutritionFacts {
	private int servingSize = -1;
	private int servings = -1;
	private int calories = 0;
	private int fat = 0;
	private int sodium = 0;
	private int carbohydrate = 0;

	public static void main(String[] args) {
		NutritionFacts facts = NutritionFacts.builder()
				.calories(200)
				.fat(40)
				.carbohydrate(120)
				.build();
	}
}

As you see whith @Builder applied we don't have to declare all possible constructors of NutritionFacts covering different set of parameters. Instead we instantiate the class builder and only pass the needed parameters.

There is much more of Lombok features you can read about in the offcial documentation.

Experimental Features

@UtilityClass

Utility class can't be instantiated and can only have static methods.

@UtilityClass
public class MathUtils {
	public int multiply(int a, int b) {
		return a * b;
	}
}

This is how this class looks after decompilation. The private constructor is added, the multiply method turned static and class itself turned final.

public final class MathUtils {
	private MathUtils() {
		throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
	}

	public static int multiply(int a, int b) {
		return a * b;
	}
}

We won't dive any deeper into experimental features since they are subject to change. but you can read more on them in the official documentation.

Delombok

Lombok annotations can be 'delomboked' i.e. unfolded to the actual code.

You can do it either by applying IDE plugin command or executing a command line.

To delombok in Intellij IDEA simply call Refactor/Delombok/All lombok annotations on a target class. Keep in mind that Lombok plugin must be installed.