An easy-to-use Java plug-in - Lombok

Posted by burntheblobs on Tue, 14 Dec 2021 12:14:39 +0100

Author: roast chicken Prince

Source: Hang Seng LIGHT cloud community

Earlier, I introduced a java maven plug-in called maven helper. Many students are still very interested. Today, make persistent efforts and recommend another java plug-in called Lombok. First, let's talk about the main functions of Lombok and how to use it. Later, we'll talk about the principle of Lombok when we have time

What is Lombok

Let's first look at the introduction on Duan's official website

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

lombok is a tool that can help us simplify and eliminate some necessary but cumbersome Java code in the form of simple annotations. In short, for example, we create a new class and write several fields in it. Usually, we need to manually create getter and setter methods and constructors, The function of lombok is to save us the trouble of manually creating these codes. It can automatically generate these methods for us when we compile the source code

Main functions of Lombok

Lombok's main function is through annotation. I sorted and drew a diagram of the core functions used

Use of Lombok

Official website description

There are many introduction methods on the official website. You can refer to the introduction on the official website install

maven introduction

This article only introduces the introduction of maven. lombok is directly introduced into pom files. At present, the latest version of the official website is 1.18 twenty-two

<dependencies>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <version>1.18.22</version>

        <scope>provided</scope>

    </dependency>

</dependencies>

Use example

The use of lombok mainly depends on annotations. There are all annotations in the documents on the official website, which are not listed here one by one. Just take @ Data as an example, and the code is also from the official website.

@Data

@Data annotation on a class will automatically generate setter/getter, equals, canEqual, hashCode and toString methods for all properties of the class. If it is a final property, setter methods will not be generated for the property.

import lombok.AccessLevel;

import lombok.Setter;

import lombok.Data;

import lombok.ToString;



@Data public class DataExample {

  private final String name;

  @Setter(AccessLevel.PACKAGE) private int age;

  private double score;

  private String[] tags;

  

  @ToString(includeFieldNames=true)

  @Data(staticConstructor="of")

  public static class Exercise<T> {

private final String name;

private final T value;

  }

}

If Lombok is not used, the implementation is as follows:

import java.util.Arrays;



public class DataExample {

  private final String name;

  private int age;

  private double score;

  private String[] tags;

  

  public DataExample(String name) {

this.name = name;

  }

  

  public String getName() {

return this.name;

  }

  

  void setAge(int age) {

this.age = age;

  }

  

  public int getAge() {

return this.age;

  }

  

  public void setScore(double score) {

this.score = score;

  }

  

  public double getScore() {

return this.score;

  }

  

  public String[] getTags() {

return this.tags;

  }

  

  public void setTags(String[] tags) {

this.tags = tags;

  }

  

  @Override public String toString() {

return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";

  }

  

  protected boolean canEqual(Object other) {

return other instanceof DataExample;

  }

  

  @Override public boolean equals(Object o) {

if (o == this) return true;

if (!(o instanceof DataExample)) return false;

DataExample other = (DataExample) o;

if (!other.canEqual((Object)this)) return false;

if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;

if (this.getAge() != other.getAge()) return false;

if (Double.compare(this.getScore(), other.getScore()) != 0) return false;

if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;

return true;

  }

  

  @Override public int hashCode() {

final int PRIME = 59;

int result = 1;

final long temp1 = Double.doubleToLongBits(this.getScore());

result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());

result = (result*PRIME) + this.getAge();

result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));

result = (result*PRIME) + Arrays.deepHashCode(this.getTags());

return result;

  }

  

  public static class Exercise<T> {

private final String name;

private final T value;



private Exercise(String name, T value) {

  this.name = name;

  this.value = value;

}



public static <T> Exercise<T> of(String name, T value) {

  return new Exercise<T>(name, value);

}



public String getName() {

  return this.name;

}



public T getValue() {

  return this.value;

}



@Override public String toString() {

  return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";

}



protected boolean canEqual(Object other) {

  return other instanceof Exercise;

}



@Override public boolean equals(Object o) {

  if (o == this) return true;

  if (!(o instanceof Exercise)) return false;

  Exercise<?> other = (Exercise<?>) o;

  if (!other.canEqual((Object)this)) return false;

  if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;

  if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;

  return true;

}



@Override public int hashCode() {

  final int PRIME = 59;

  int result = 1;

  result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());

  result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());

  return result;

}

  }

}

For others, please refer to the address on the official website https://projectlombok.org/fea... , click in to see relevant instructions and usage examples

reference resources

Official website: https://projectlombok.org/

file: https://projectlombok.org/fea...

github project: https://github.com/projectlom...

Topics: Java Lombok