Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live

 

Lombok is a java library for automatically creating code for any class. Using this you just have to focus on state information of class.

Lombok provide following utilities in form of annotations. During compilation time it inject code into class bytecode.

Prerequisites

  • Please include Lombok jar into your class-path.
  • Please install its plugin from here.

Annotations Levels

Lombok annotation can be used at following level.

  • Type
  • Constructor
  • Method
  • Field
  • Local Variable

Getter & Setter

These will inject getter and setter methods for your class so you don’t have to inject them. It is can be placed at class or field

@Getter
@Setter
public class SampleClass{
......

ToString

It will inject more informational toString implementation. It should be placed at class.

@ToString
public class SampleClass{
......

Constructors

Lombok provides three class level annotations for constructors which are as followings

NoArgsConstructor

It is similar to default constructor.

@NoArgsConstructor
public class SampleClass{
......

AllArgsConstructor

It defines constructor with all members.

@AllArgsConstructor
public class SampleClass{
......

RequiredArgsConstructor

It  defines constructor with all required members. Required members are those which are final or have another annotation @NonNull.

@RequiredArgsConstructor
public class SampleClass{
    private final String name;
    
    @NonNull
    private String email;
    
   private int age;

// name and email based constructor will be injected
......

EqualsAndHashCode

For creating equals and hashcode methods of class. It is class level annotation

@EqualsAndHashCode
public class SampleClass{
......

Data

It is a class level annotation which combines multiple annotation into one. By use of following annotations can be eliminated.

  • ToString
  • Getter
  • Setter
  • RequiredArgsConstructor
  • EqualsAndHashCode
@Data
public class SampleClass{
......

// Both have same impact

@ToString
@Getter
@Setter
@RequiredArgsConstructor
@EqualsAndHashCode
public class SampleClass{
......

Value

It is a class level annotation which provide immutable funcionality to class.

@Value
public class SampleClass{
......

It povides all argument constructor so that once state of instance is initialsed it cannot be modifed as it only provide getters.

Advance Level Annotations

 

Synchronized

It is similar to synchronized keyword, but synchronized keyword locks complete object means locking on this. Lombok implements it using synchronized block for method body. It works on both static and instance methods.

Internally it locks Lombok defined properties ($lock for instanceMethod & $LOCK for static methods ) to facilitate synchronisation.

It should be used with any method of class

public class SampleClass{

  @Synchronized
  public void someMethod() {
    System.out.println("Hello World");
  }
........

Builder

It uses builder design pattern. It will inject a static private class into annotated class to provide complex builder functionality. It also inject a static method builder into class.

@Builder
public class SampleClass{
......

How to use

SampleClass obj = SampleClass.builder()
                             .property1("")
                             .property2("")
                             .build();

 

Enjoy writing clean code from now onwards. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s