Introduction
Have you ever thought of writing a small piece of Java code providing greater functionality? Yes, of course, you would like to do that. It may be a good framework or a good algorithm to do so. If you can write small Java code then it will increase the readability, portability and has fewer bugs and less maintenance. In this article, I present a great framework called "Lombok" that provides an efficient way to write smaller Java code to achieve greater functionality. Lombok is a framework that provides a boilerplate structure to minimize your code to perform more. Lombok provides certain annotations that help us to write better code and eliminates the burden of repetitive code in any project. Let us dive into the usage of the Lombok framework.
Technicalities
There are certain situations where developers complain about the vast code and find it difficult to make a walkthrough. It is the absolute reality that not all code can be minimized depending on the functionalities whether they are simple or complex. In modern days, new frameworks have evolved to make the developer's life easier. Lombok is one of the frameworks to help developers to write less code and avoid rewriting repetitious code. Think about a situation where you have a bean or POJO that contains fields like id, first name, last name, salary, designation and so on and you will have all the getter and setter methods for this. It is also true that modern developers never write getter and setter methods manually. It is only because of the benefit of the sophisticated style of the modern Java IDEs, developers generate getter and setter methods and some other generic methods also. However, the code finally looks vast. There may be a POJO or a bean that may contain 2 to 3 hundred lines of code. Now the simple POJO looks bulky. It may look complex for a novice Java developer. What if we simply write the fields and let all the relevant getter and setter methods be generated at runtime? That is the power and flexibility provided by Lombok. Lombok provides the following widely used annotations.
@NonNull,@Cleanup,@Getter/@Setter,@ToString,@EqualsAndHashCode,
@NoArgsConstructor,@RequiredArgsConstructor,@AllArgsConstructor,
@Data,@Value,@Slf4j,@Log4j
Let us discuss each annotation very precisely.
@NonNull
This annotation generates a NullPointerException for null checking. This annotation can be applied in the field of a class or at the method parameter level. Let us see the code below.
- package com.ddlab.rnd.lombok;
- import lombok.NonNull;
- /**
- * The Class NonNullCheck is used to provide an example on the generation of
- * NullPointerException with the help of Lombok @NonNull annotation.
- *
- * @author <a href="mailto:[email protected]">Debadatta Mishra</a>
- * @since 2013
- */
- public class NonNullCheck
- {
- /** The value. */
- @NonNull
- private String value;
- /**
- * Instantiates a new non null check.
- * @param value
- * the value
- */
- public NonNullCheck(@NonNull String value) {
- this.value = value;
- }
- /**
- * Show.
- */
- public void show()
- {
- System.out.println("Value--->" + value);
- }
- /**
- * The main method.
- *
- * @param args
- * the arguments
- */
- public static void main(String[] args)
- {
- NonNullCheck nc = new NonNullCheck(null);
- nc.show();
- }
- }
@Cleanup
This is a very useful and handy annotation that saves our life. You have may have seen due to work pressure or for some other reason, developers forget to invoke the close method for file operations or database operation. If you provide this annotation then the code will automatically provide a close() method at runtime. It is also helpful while making code reviews so you can easily provide this annotation if the developer has forgotten to write it. Let us see the code below.
- package com.ddlab.rnd.lombok;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import lombok.Cleanup;
- import lombok.NonNull;
- /**
- * The Class FileReader is used as a utility to read a file by minimizing the
- * code. In this class @NonNull and @Cleanup annotations have been used for
- * null check and close operations respectively.
- *
- * @author <a href="mailto:[email protected]">Debadatta Mishra</a>
- * @since 2013
- */
- public class FileReader {
- /**
- * Read file.
- * @NonNull annotation will generate a NullPointerException code at runtime
- * @Cleanup annotation will generate the close() method at run time
- * @param filePath
- * the file path
- * @throws Exception
- * the exception
- */
- public static void readFile(@NonNull String filePath) throws Exception {
- File file = new File(filePath);
- byte[] buffer = new byte[(int) file.length()];
- @Cleanup
- InputStream inStream = new FileInputStream(file);
- inStream.read(buffer);
- System.out.println("File Contents :\n" + new String(buffer));
- }
- /**
- * The main method.
- * @param args
- * the arguments
- */
- public static void main(String[] args) {
- try {
- FileReader.readFile("data/data.txt");
- //FileReader.readFile(null);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("I got the exception ........");
- }
- }
- }
@Getter/@Setter
It is a common practice that whenever we write a POJO with some fields that we benefit from the intelligence of the Java IDE to generate the getter and setter methods. In this case, Lombok provides @Getter and @Setter annotations to generate the getter and setter methods at runtime. Let us look at the code below.
- package com.ddlab.rnd.lombok;
- import lombok.Getter;
- import lombok.Setter;
- /**
- * The Class Employee is used as a POJO for the employee having id and name. Lombok
- * annotation will generate all the getter and setter methods for properties.
- *
- * @author <a href="mailto:[email protected]">Debadatta Mishra</a>
- * @since 2013
- */
- public class Employee {
- /**
- * Specifies the id of the employee.
- *
- * @Getter annotation will generate all the getter method for id.
- * @Setter annotation will generate all the setter method for id.
- */
- @Getter
- @Setter
- private int id;
- /**
- * Specifies the name.
- *
- * @Getter annotation will generate all the getter method for name.
- * @Setter annotation will generate all the setter method for name.
- */
- @Getter
- @Setter
- private String name;
- }

Join the conversation! Your thoughts help the community grow.