Never write in Java a getter or equals method again

The lombok project helps you to be more productive

Lombok is a great tool. Your code often gets verbose for common tasks. So what does lombok? The framework is plugged into the build process. It autogenerates bytecode into your class fields through a couple of annotations.

Here comes the build.gradle:

dependencies {
  compileOnly 'org.projectlombok:lombok:1.18.8'
  annotationProcessor 'org.projectlombok:lombok:1.18.8'

  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}

So let’s have a look at the lombok annotations

package de.claudioaltamura.java.lombok;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter 
@Setter
@EqualsAndHashCode
@ToString
public class SuperHero {

  private String name;

  private String city;

}

I put all annotations on class level. I added to constructor annotations here: first @NoArgsConstructor. This annotation adds a constructor with no arguments. And the second annotation is @AllArgsConstructor, which generates a constructor with every field.

With @Gettter and @Setter lombok generates default getter and setter for your class. And of course @EqualsAndHashCode and @ToString annotations are very nice too.

These are only a few examples which features lombok provides. Have a look a the project for more informations.

The example project is on github :-).

Consent Management Platform von Real Cookie Banner