Lombok Java
What is Lombok java?
A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may enhance the clarity of the program code and reduce space simply by utilizing annotations. It integrates automatically with build tools and IDEs to spruce up our Java programs.
Why is the Lombok Used?
Consider that we are creating a POJO file with several private fields as part of a Java application. To give access, we must provide getters, setters, and accessor methods for these fields. The number of lines of code is increased by creating getters as well as setters for each field.Additionally, adding a function Object() and a function toString() method will result in a lot more code and chaos. What happens when we use Java objects that must be closed after use? In that case, we would need to implement a finally-block or even use try-with-resources to guarantee that the object is closed. Coding can become rather cluttered when closing objects with a finally-block boilerplate. As a result, we work with a lot of boilerplate code.
To solve a similar issue,The Lombok project is established.
Properties of the Lombok Project
- It lessens boilerplate.
- It introduces simple-to-use annotations in place of boilerplate code.
- It reduces error-proneness and tends to make code easier to read.
- The developers became more productive by using Lombok.
- All well-known IDEs are compatible with it.
- Furthermore offers delombok functionality
- The annotation should be provided to verify null values.
- short data objects
- simple cleaning
- securing properly
- Uncomplicated logging
Packages in Lombok Java
The following packages are part of the Lombok Java API and can be used in various ways.
- Lombok
- experimental
- extern.apachecommons
- extern.flogger
- extern.java
- extern.jbosslog
- extern.log4j
- extern.slf4j
Java Package for Lombok
All of the annotations as well as classes needed to use Lombok are included in the package. Except for the following two packages, all additional packages are solely applicable to individuals who are upgrading Lombok for their personal uses.
lombok.extern.*: The Lombok annotations in the packages help libraries by reducing boilerplate problems. It is not a component of the actual JRE.
lombok.experimental:Before committing to long-term support, the package lombok.experimental includes Lombok features that are either new or expected to change.
- Classes
ConfigurationKeys: A container class called ConfigurationKeys holds all Lombok configurable keys that aren't associated with any particular annotation.
Lombok: useful utilities for manipulating code produced by Lombok.
- Annotations
- AllArgsConstructor : Creates an all-args function Object() using the AllArgsConstructor class.
- Builder: The Builder annotation adds a "builder" aspect to the annotated class or to a class that includes a member annotated with the annotation. Default If a field is not specifically set during building, the initializing expression for the field marked with the annotation Default will be used instead.
- Builder.ObtainVia: Put on a field (for @Builder on a type), or an argument (for @Builder on a function Object() or static function), to tell Lombok how to get a value for this field or argument was given an occurrence; this is only important if toBuilder is true.
- Cleanup: Assures that, regardless of what transpires, the variable declaration you annotated will be wiped up by calling its closure function.
- CustomLog: instructs Lombok to create a logger variable based on a distinctive implementation of a logger.
- Data: creates achievers for all fields, a practical function toString() method, and implementations of hashCode and equals that examine all non-transient fields.
- EqualsAndHashCode: Based on relevant fields, EqualsAndHashCode produces functionality for the equivalence and hashCode functions that were already inherited by all objects.
- EqualsAndHashCode.Exclude: If this field is present, the equality and hashCode methods that are created will not include it.
- EqualsAndHashCode.Include:Configure how this member behaves in the equality and hashCode implementations; if this member is a function, have included the method's return value in the hashCode/equality calculations.
- Generated:This annotation will eventually be added automatically by Lombok to all constructors, methods, fields, and types that are produced.
- Getter:To force Lombok to develop a standard getter, placed on any field.
- NoArgsConstructor :creates a function Object() with no arguments.
- NonNull:If applied to a parameter, Lombok will add a null-check at the beginning of the body of the method or function Object() and throw a NullPointerException with the name of the argument as the message.
- RequiredArgsConstructor:Creates a function Object() with the necessary parameters using the RequiredArgsConstructor.
- Setter:Any field can be used to force Lombok to develop a standard setter.
- Singular:The singular comments are combined with @Builder to create single component "add" methods for collections.
- SneakyThrows:Javac will not require you to catch or pass onward any verified exceptions that declarations in your function body say they generate when you use SneakyThrows @SneakyThrow.
- Synchronized: Similar to adding the "synchronized" keyword to a method, except that it synchronizes on a private local object instead of the method's instance, preventing other code that is not under your control from interfering with your thread management.
- ToString:creates an implementation of the function toString() function that is inherited by all types and prints the contents of the appropriate fields.
- ToString.Exclude: If present, ToString.Exclude excludes this field from the resulting function toString()
- ToString.Include: If this member is on a method, ToString.Include allows you to specify whether the method's final result should be included in the output.
- Val: Any local variable declaration that uses val as the type (including those in for-each statements) will have the type determined by the initializing expression.
- Value:generates a large amount of code that is appropriate for a class that represents an immutable entity.
- Var: When a local variable is declared with var as its type (including in a for expression), the initializing expression will be used to determine the type
- With:Make Lombok generate a "with" method by calling "with Put" on just about
Does project Lombok perform the same tasks as IDEs?
No, IDEs as well as Lombok perform different tasks, yet they are very similar to one another.Although we save time by using IDEs to create these boilerplate programs (getters and setters), the fact that they are actually present in the source code increases the number of lines, lowers maintainability, and makes the code more difficult to read and comprehend. The solution Lombok adds all of these boilerplate lines at the building moment in the class file rather than including them in the primary source code.
The Lombok Project's Goal
Using Lombok has several benefits, but a few of them are as follows:
- Verify for nulls
It's the most fundamental service that Lombok provides. The @NonNull annotation provided by the library can be used to produce a blank check on such a setter field. If the declared class field has a null value, it raises the NullPointerException. Please take note that the primitive parameter cannot be annotated. with the annotation @NonNull. Think about the following line of code as an example.
- Short Data Object
Creating getters and setters might be time-consuming if the POJO file contains a lot of private fields. Using @Getter and @Setter annotations makes it simple to complete the work with the Lombok. Think about the following code, for instance.
Project Lombok Usage
We notice that now the code becomes cleaner and less prone to errors. Keep in mind that the @Getter and @Setter annotations also let the designation of an ideal parameter for the access level, if necessary. One advantage is that it handles the naming convention. Again for a Boolean field that starts with the letter is, for instance, it produces an accessor method rather than the get method. Getters as well as setters are created for every non-static field in the class if they are used at the class level.
Getters and Setters creation
All of the annotations' utilities can be applied using the @Data annotation. In other words, when a class is annotated with @Data annotation, Lombok creates getters but instead setters for everyclasses with non-static fields and class constructors. The function toString() , equivalents(), and hashCode() methods are comparable to this one. It makes POJO coding straightforward.
Set up Lombok in the Eclipse IDE
The steps listed below must be carried out to configure the Lombok program in the Eclipse IDE:
Step 1: Download with lombok.jar file first,
Step 2: Double-click the acquired JAR file to run the aforementioned JAR file. On the screen, a GUI displays where we must choose the IDE about which we want to set up the Lombok project.Bali Java
Step 3: To find the directory where the Eclipse IDE is installed, click the Specify location button. Choose the eclipse.exe file from the folder.
Step 4: Select the Update/Install button.
Once the aforementioned procedure is finished, verify whether project Lombok was installed successfully.
Step 5: Open the Eclipse IDE, then select Help. relating to Eclipse IDE. If the Lombok projectis listed, it was installed correctly.
Step 6: Clicking the Close button completes the process.
The Eclipse IDE and the Lombok project have been successfully merged.
Java Program with Lombok
We've utilized the popular Lombok annotation in the program below to keep our code short.
Employee.java
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok. NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee
{
private @Getter @Setter Integer EmployeeId;
private @Getter @Setter String EmployeeName;
private @Getter @Setter String EmployeeOrganisation;
private @Getter @Setter String EmployeeEmailId;
}