Skip to content

Latest commit

 

History

History
322 lines (291 loc) · 6.98 KB

File metadata and controls

322 lines (291 loc) · 6.98 KB

Overview

Lombok is a Java API that allows you to generate boilerplate code such as getters, setters, constructors, etc. at compile time. It adds annotations to your code that will be processed by the Lombok library.

Summary

Dependencies

<dependencies>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
</dependencies>

Build

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

Annotations

@Getter

Generates a getter for the annotated field.

private class Person {
    @Getter
    private String name;
//	public String getName() {
//        return this.name;
//    }
}

or getters for all non-static fields of the class

@Getter
private class Person {
    private String name;
    private int age;
//	public String getName() {
//        return this.name;
//    }
//	public int getAge() {
//        return this.age;
//    }
}

@Setter

Generates a setter for the annotated field.

private class Person {
    @Setter
    private String name;
//	public void setName(String name) {
//        this.name = name;
//    }
}

or setters for all non-static fields of the class

@Setter
private class Person {
    private String name;
    private int age;
//  public void setName(String name) {
//        this.name = name;
//  }
//	public void setAge(int age) {
//          this.age = age;
//  }
}

@ToString

Generates a toString method for the annotated class.

@ToString
private class Person {
    private String name;
    private int age;
//	public String toString() {
//        return "Person(name=" + this.name + ", age=" + this.age + ")";
//    }
}

if you want to exclude some fields from the toString method, you can use the @ToString.Exclude annotation

@ToString
private class Person {
    private String name;
    @ToString.Exclude
    private int age;
//	public String toString() {
//        return "Person(name=" + this.name + ")";
//    }
}

if you want to include the super class fields in the toString method, you can use the @ToString(callSuper = true) annotation

@ToString(callSuper = true)
private class Person extends Human {
  private String name;
  private int age;
//	public String toString() {
//        return "Person(super=" + super.toString() + ", name=" + this.name + ", age=" + this.age + ")";
//    }
}

@EqualsAndHashCode

Generates a equals and hashCode method for the annotated class.

@EqualsAndHashCode
private class Person {
    private String name;
    private int age;
//	public boolean equals(Object o) {
//        return o == this || o instanceof Person && 
//                            ((Person)o).name.equals(this.name) && 
//                            ((Person)o).age == this.age;
//    }
//    public int hashCode() {
//        return 1;
//    }
}

if you want to exclude some fields from the equals and hashCode method, you can use the @EqualsAndHashCode.Exclude annotation

@EqualsAndHashCode
private class Person {
    private String name;
    @EqualsAndHashCode.Exclude
    private int age;
//	public boolean equals(Object o) {
//        return o == this || o instanceof Person && 
//                            ((Person)o).name.equals(this.name);
//    }
//    public int hashCode() {
//        return 1;
//    }
}

@NoArgsConstructor

Generates a no-args constructor for the annotated class.

@NoArgsConstructor
private class Person {
    private String name;
    private int age;
//	public Person() {
//    }
}

@AllArgsConstructor

Generates a constructor for the annotated class that takes all fields as arguments.

@AllArgsConstructor
private class Person {
    private String name;
    private int age;
//	public Person(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
}

@RequiredArgsConstructor

Generates a constructor for the annotated class that takes all final fields as arguments.

@RequiredArgsConstructor
private class Person {
    private final String name;
    private int age;
//	public Person(String name) {
//        this.name = name;
//    }
}

@Data

Generates getters, setters, equals, hashCode, required args constructor, and toString methods for the annotated class.

@Data
private class Person {
    private String name;
    private int age;
}

instead of

@Getter
@Setter
@EqualsAndHashCode
@ToString
@RequiredArgsConstructor
private class Person {
    private String name;
    private int age;
}

@Value

Generates getters, equals, hashCode, all args constructor, and toString methods for the annotated class.

@Value
private class Person {
    private String name;
    private int age;
}

instead of

@Getter
@EqualsAndHashCode
@ToString
@AllArgsConstructor
private class Person {
    private String name;
    private int age;
}

FieldDefaults

sets the default access level for fields and static fields. can also be used to set variables as final.

@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
private class Person {
    /*private final*/ String name;
    /*private final*/ int age;
}

There also exists makeStatic, which can be used to set fields as static.

@FieldDefaults(level = AccessLevel.PRIVATE, makeStatic = true)
private class Person {
    /*private static*/ String name;
    /*private static*/ int age;
}

@Builder

Generates a builder for the annotated class.

@Builder
private class Person {
    private String name;
    private int age;
//	public static PersonBuilder builder() {
//		return new PersonBuilder();
//	}
}

@Slf4j

Adds a logger to the class.

@Slf4j
public class MainController {
  public void index() {
    log.info("Hello World");
  }
}

How does lombok work

Lombok uses a Java annotation processor to generate the boilerplate code at compile time. The annotation processor is executed by the Java compiler and generates the code that is then compiled with the rest of the code. Other than other annotation processors, Lombok does not generate a new class file, but modifies the existing class file using the Java ASM library.