Lombok

Lavish Jain
4 min readFeb 12, 2024

--

Lombok is an open-source library which aims to reduce the efforts of a java developer by providing a set of annotations that can be used for boilerplate code generation.
We can add its maven dependency in the project’s POM-

<dependencies>
...
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
...
</dependencies>

During the build process itself it auto-generates the Java bytecode into our .class files as per a number of project annotations we introduce in our code.
Hence, lombok is required only at compile time and has nothing to contribute during the runtime. So it is compile time dependency and not runtime. For the same reason users of our project’s jar need not to be dependent on lombok.

Lombok Annotations

1. Generating Setters and Getters

Generates getters and setters for non-static fields in the class.

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LombokTest {
private String name;
private boolean isQualified;

}

After compilation-

public class LombokTest {
private String name;
private boolean isQualified;

public String getName() {
return this.name;
}

public boolean isQualified() {
return this.isQualified;
}

public void setName(final String name) {
this.name = name;
}

public void setQualified(final boolean isQualified) {
this.isQualified = isQualified;
}

In the above example setters and getters are generated for all the fields of the class.
It should be noted that if the class contains a method of the same name as the getter or setter to be generated, regardless of parameter or return types, no corresponding method will be generated.

We can also use these annotations at field level and also provide access level of the generated method as an argument.

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class LombokTest {
@Getter @Setter(AccessLevel.PROTECTED) private String name;

After compilation-

public class LombokTest {
private String name;

public String getName() {
return this.name;
}

protected void setName(final String name) {
this.name = name;
}

2. Generating Constructors

@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class LombokTest {
@NonNull private String name;
private boolean isQualified;
}

After Compilation-

public class LombokTest {
private @NonNull String name;
private boolean isQualified;

public LombokTest() {
}

public LombokTest(final @NonNull String name, final boolean isQualified) {
if (name == null) {
throw new NullPointerException("name is marked non-null but is null");
} else {
this.name = name;
this.isQualified = isQualified;
}
}

public LombokTest(final @NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked non-null but is null");
} else {
this.name = name;
}
}
}
  • @NoArgsConstructor generated a no argument constructor.
  • @AllArgsConstructor generated a parametrized constructor with all fields.
  • @RequiredArgsConstructor generates a parametrized constructor with final fields or fields annotated with @NonNull .
  • Note that whenever a field annotated with @NonNull is initialized, either in generated setter or in generated constructor, a null check is placed on it and if the provided value is null then it throws a NullPointerException.
  • Note that static fields will not be included in argument list of constructors.

3. Generating toString() Method

@ToString annotation generates toString() method including all non-static fields in the class.
We can also use @Exclude with fields we want to be excluded in toString() method and use @Include with any static field to include in toString() method.
We can also pass callSuper (default value false) argument with @ToString which determine whether to call the superclass’s toString implementation as part of the generated toString algorithm.

import lombok.*;
import lombok.ToString.Exclude;
import lombok.ToString.Include;

@ToString(callSuper = true)
public class LombokTest {
@Exclude
private int id;
private String name;
private static boolean isQualified;
@Include
private static int count;

After compilation-

public class LombokTest {
private int id;
private String name;
private static boolean isQualified;
private static int count;

public String toString() {
return "LombokTest(super=" + super.toString() + ", name=" + this.name + ", count=" + this.count + ")";
}
}

4. Generating hashCode() and equals()

Generate both equals and hashCode methods, as the two are tied together intrinsically by the hashCode contract.
Like @ToString, By default, any field in the class that is not static or transient will be considered by both methods and similar @Excluded and @Included annotations are provided.

import lombok.*;
import lombok.EqualsAndHashCode.Exclude;
import lombok.EqualsAndHashCode.Include;

@EqualsAndHashCode
public class LombokTest {

private int id;
@Exclude
private String name;
@Include
private static boolean isQualified;
private static int count;
}

After compilation-

public class LombokTest {

private int id;
private String name;
private static boolean isQualified;
private static int count;

public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof LombokTest)) return false;
final LombokTest other = (LombokTest) o;
if (!other.canEqual((Object) this)) return false;
if (this.id != other.id) return false;
if (this.isQualified != other.isQualified) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof LombokTest;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.id;
result = result * PRIME + (this.isQualified ? 79 : 97);
return result;
}
}

5. @Data

@Data is a combination of following annotations-

  • @Getter : Lombok generates getter methods for all non-static fields in the class.
  • @Setter : Lombok generates setter methods for all non-final non-static fields in the class.
  • @EqualsAndHashCode : Lombok generates an equals() method that compares all non-static fields for equality and hashCode() method that uses all non-static fields to calculate the hash code.
    We can also use @Exclude and @Include similarly.
  • @ToString : Lombok generates a toString() method that includes the class name and all non-static fields.
    We can also use @Exclude and @Include similarly.

6. Get Logger

@RestController
@Slf4j // or @Log / @CommonsLog / @Log4j / @Log4j2 / @XSlf4j
public class LoggingController {

@GetMapping("/log-test")
public String loggingTest(){
log.trace("A TRACE Message");
log.debug("A DEBUG Message");
log.info("An INFO Message");
log.warn("A WARN Message");
log.error("An ERROR Message");

After compilation —

@RestController
public class LoggingController {

private static final Logger log = LoggerFactory.getLogger(LoggingController.class);

@GetMapping("/log-test")
public String loggingTest(){
log.trace("A TRACE Message");
log.debug("A DEBUG Message");
log.info("An INFO Message");
log.warn("A WARN Message");
log.error("An ERROR Message");

--

--