超级全面的 Lombok 注解介绍,学一波!

2021-06-29 11:36:09 浏览数 (1)

重磅干货,第一时间送达

作者:riemann

blog.csdn.net/riemann_/article/details/105374987

一、@Getter and @Setter

使用@Getter和/或@Setter注释任何字段,以使lombok自动生成默认的getter / setter

默认的getter只是返回该字段,如果该字段被称为foo,则名为getFoo(如果该字段的类型为boolean,则为isFoo)。

默认生成的 getter / setter方法是公共的,除非你明确指定一个AccessLevel。合法访问级别为PUBLIC,PROTECTED,PACKAGE和PRIVATE

你还可以在类上添加@Getter和/或@Setter注释。在这种情况下,就好像你使用该注释来注释该类中的所有非静态字段一样。

你始终可以使用特殊的AccessLevel.NONE访问级别来手动禁用任何字段的getter / setter生成。这使你可以覆盖类上的@Getter,@Setter或@Data注释的行为。

With Lombok:

代码语言:javascript复制
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  
  @Getter 
  @Setter 
  private int age = 10;
  
  @Setter(AccessLevel.PROTECTED) 
  private String name;
  
  @Override 
  public String toString() {
    return String.format("%s (age: %d)", name, age);
  }
}

Native Java:

代码语言:javascript复制
public class GetterSetterExample {

  private int age = 10;

  private String name;
  
  @Override 
  public String toString() {
    return String.format("%s (age: %d)", name, age);
  }
  
  public int getAge() {
    return age;
  }
  
  public void setAge(int age) {
    this.age = age;
  }
  
  protected void setName(String name) {
    this.name = name;
  }
}

二、@ToString

任何类定义都可以使用@ToString注释,以使lombok生成toString()方法的实现。

默认情况下,将打印所有非静态字段。如果要跳过某些字段,可以使用@ ToString.Exclude注释这些字段。或者,可以通过使用@ToString(onlyExplicitlyIncluded = true),然后使用@ToString.Include标记要包含的每个字段,来确切指定希望使用的字段。

通过将callSuper设置为true,可以将toString的超类实现的输出包含到输出中。请注意,java.lang.Object中toString() 的默认实现几乎毫无意义。

With Lombok:

代码语言:javascript复制
import lombok.ToString;

@ToString
public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  @ToString.Exclude 
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  @ToString(callSuper=true, includeFieldNames=true)
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
  }
}

Native Java:

代码语言:javascript复制
import java.util.Arrays;

public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
    
    @Override 
    public String toString() {
      return "Square(super="   super.toString()   ", width="   this.width   ", height="   this.height   ")";
    }
  }
  
  @Override 
  public String toString() {
    return "ToStringExample("   this.getName()   ", "   this.shape   ", "   Arrays.deepToString(this.tags)   ")";
  }
}

三、@EqualsAndHashCode

任何类定义都可以使用@EqualsAndHashCode进行注释,以使lombok生成equals(Object other)hashCode()方法的实现。默认情况下,它将使用所有非静态,非瞬态字段,但是您可以通过使用@EqualsAndHashCode.Include标记类型成员来修改使用哪些字段(甚至指定要使用各种方法的输出)。 @EqualsAndHashCode.Exclude。或者,可以通过使用@ EqualsAndHashCode.Include标记并使用@EqualsAndHashCode(onlyExplicitlyIncluded = true)来精确指定要使用的字段或方法。

如果将@EqualsAndHashCode应用于扩展另一个类的类,则此功能会有些棘手。通常,为此类自动生成equalshashCode方法是一个坏主意,因为超类还定义了字段,该字段也需要equals / hashCode代码,但不会生成此代码。通过将callSuper设置为true,可以在生成的方法中包括超类的equalshashCode方法。

搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典.pdf

With Lombok:

代码语言:javascript复制
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class EqualsAndHashCodeExample {
  private transient int transientVar = 10;
  private String name;
  private double score;
  @EqualsAndHashCode.Exclude 
  private Shape shape = new Square(5, 10);
  private String[] tags;
  @EqualsAndHashCode.Exclude 
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  @EqualsAndHashCode(callSuper=true)
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
  }
}

Native Java:

代码语言:javascript复制
import java.util.Arrays;

public class EqualsAndHashCodeExample {
  private transient int transientVar = 10;
  private String name;
  private double score;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  @Override 
  public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof EqualsAndHashCodeExample)) return false;
    EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;
    if (!other.canEqual((Object)this)) return false;
    if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
    if (Double.compare(this.score, other.score) != 0) return false;
    if (!Arrays.deepEquals(this.tags, other.tags)) return false;
    return true;
  }
  
  @Override 
  public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final long temp1 = Double.doubleToLongBits(this.score);
    result = (result*PRIME)   (this.name == null ? 43 : this.name.hashCode());
    result = (result*PRIME)   (int)(temp1 ^ (temp1 >>> 32));
    result = (result*PRIME)   Arrays.deepHashCode(this.tags);
    return result;
  }
  
  protected boolean canEqual(Object other) {
    return other instanceof EqualsAndHashCodeExample;
  }
  
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
    
    @Override 
    public boolean equals(Object o) {
      if (o == this) return true;
      if (!(o instanceof Square)) return false;
      Square other = (Square) o;
      if (!other.canEqual((Object)this)) return false;
      if (!super.equals(o)) return false;
      if (this.width != other.width) return false;
      if (this.height != other.height) return false;
      return true;
    }
    
    @Override 
    public int hashCode() {
      final int PRIME = 59;
      int result = 1;
      result = (result*PRIME)   super.hashCode();
      result = (result*PRIME)   this.width;
      result = (result*PRIME)   this.height;
      return result;
    }
    
    protected boolean canEqual(Object other) {
      return other instanceof Square;
    }
  }
}

四、@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor

@NoArgsConstructor将生成没有参数的构造函数。如果字段由final修饰,则将导致编译器错误,除非使用@NoArgsConstructor(force = true),否则所有final字段都将初始化为0 / false / null。对于具有约束的字段(例如@NonNull字段),不会生成任何检查。

@RequiredArgsConstructor为每个需要特殊处理的字段生成一个带有1个参数的构造函数。所有未初始化的final字段都会获取一个参数,以及所有未声明其位置的未标记为@NonNull的字段。

@AllArgsConstructor为类中的每个字段生成一个带有1个参数的构造函数。标有@NonNull的字段将对这些参数进行空检查。

搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典.pdf

With Lombok:

代码语言:javascript复制
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;

@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
  private int x, y;
  @NonNull 
  private T description;
  
  @NoArgsConstructor
  public static class NoArgsExample {
    @NonNull 
    private String field;
  }
}

Native Java:

代码语言:javascript复制
public class ConstructorExample<T> {
  private int x, y;
  @NonNull 
  private T description;
  
  private ConstructorExample(T description) {
    if (description == null) throw new NullPointerException("description");
    this.description = description;
  }
  
  public static <T> ConstructorExample<T> of(T description) {
    return new ConstructorExample<T>(description);
  }
  
  @java.beans.ConstructorProperties({"x", "y", "description"})
  protected ConstructorExample(int x, int y, T description) {
    if (description == null) throw new NullPointerException("description");
    this.x = x;
    this.y = y;
    this.description = description;
  }
  
  public static class NoArgsExample {
    @NonNull 
    private String field;
    
    public NoArgsExample() {
    }
  }
}

五、@Data

@Data是一个方便的快捷方式批注,它将@ToString@EqualsAndHashCode@ Getter / @Setter@RequiredArgsConstructor的功能捆绑在一起:换句话说,@Data生成通常与简单POJO关联的所有样板(普通的旧Java对象)和bean:所有字段的getter,所有非final字段的setter,以及涉及类字段的适当的toStringequalshashCode实现,以及初始化所有final字段以及所有非final字段的构造函数没有使用@NonNull标记的初始化程序,以确保该字段永远不会为null

With Lombok:

代码语言:javascript复制
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;
  }
}

Native Java:

代码语言:javascript复制
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;
    }
  }
}

六、@Value

@Value注解和@Data类似,区别在于它会把所有成员变量默认定义为private final修饰,并且不会生成set方法。

七、@Builder

构建者模式

只能标注到类上,将生成类的一个当前流程的一种链式构造工厂,如下:

代码语言:javascript复制
User buildUser = User.builder().username("riemann").password("123").build();

可配合@Singular注解使用,@Singular注解使用在jdk内部集合类型的属性,Map类型的属性以及Guavacom.google.common.collect 的属性上。例如 未标注@Singular的属性,一般setter时,会直接覆盖原来的引用,标注了@Singular的属性,集合属性支持添加操作,会在属性原来的基础上增加。

With Lombok:

代码语言:javascript复制
import lombok.Builder;
import lombok.Singular;
import java.util.Set;

@Builder
public class BuilderExample {
  @Builder.Default 
  private long created = System.currentTimeMillis();
  private String name;
  private int age;
  @Singular 
  private Set<String> occupations;
}

Native Java:

代码语言:javascript复制
import java.util.Set;

public class BuilderExample {
  private long created;
  private String name;
  private int age;
  private Set<String> occupations;
  
  BuilderExample(String name, int age, Set<String> occupations) {
    this.name = name;
    this.age = age;
    this.occupations = occupations;
  }
  
  private static long $default$created() {
    return System.currentTimeMillis();
  }
  
  public static BuilderExampleBuilder builder() {
    return new BuilderExampleBuilder();
  }
  
  public static class BuilderExampleBuilder {
    private long created;
    private boolean created$set;
    private String name;
    private int age;
    private java.util.ArrayList<String> occupations;
    
    BuilderExampleBuilder() {
    }
    
    public BuilderExampleBuilder created(long created) {
      this.created = created;
      this.created$set = true;
      return this;
    }
    
    public BuilderExampleBuilder name(String name) {
      this.name = name;
      return this;
    }
    
    public BuilderExampleBuilder age(int age) {
      this.age = age;
      return this;
    }
    
    public BuilderExampleBuilder occupation(String occupation) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }
      
      this.occupations.add(occupation);
      return this;
    }
    
    public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }

      this.occupations.addAll(occupations);
      return this;
    }
    
    public BuilderExampleBuilder clearOccupations() {
      if (this.occupations != null) {
        this.occupations.clear();
      }
      
      return this;
    }

    public BuilderExample build() {
      // complicated switch statement to produce a compact properly sized immutable set omitted.
      Set<String> occupations = ...;
      return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations);
    }
    
    @java.lang.Override
    public String toString() {
      return "BuilderExample.BuilderExampleBuilder(created = "   this.created   ", name = "   this.name   ", age = "   this.age   ", occupations = "   this.occupations   ")";
    }
  }
}

八、@Accessors

链式风格

@Accessors批注用于配置lombok如何生成和查找gettersetter

默认情况下,lombok遵循针对gettersetterbean规范:例如,名为Pepper的字段的gettergetPepper。但是,有些人可能希望打破bean规范,以得到更好看的API@Accessors允许您执行此操作。

可标注在类或属性上,当然最实用的功能还是标注到类上。

标注到类上,chain属性设置为true时,类的所有属性的setter方法返回值将为this,用来支持setter方法的链式写法。如:

代码语言:javascript复制
new User().setUsername("riemann").setPassword("123");

fluent属性设置为true时,类的所有gettersetter方法将省略getset前缀,获取属性值直接使用属性名相同的无参方法,设置属性值使用属性名相同的有参方法,并且返回值为this。如:

代码语言:javascript复制
User user = new User().username("riemann").password("123");
String username = user.username();
String password = user.password();

标注到属性上,使用prefix设置需要省略的属性生成gettersetter方法时的前缀,且属性必须为驼峰式命名。

代码语言:javascript复制
@Accessors(prefix = "r")
@Getter
@Setter
private String rUsername = "riemann";

编译之后为

代码语言:javascript复制
public String getUsername() {
    return rUsername;
}
public void setUsername(String rUsername) {
    this.rUsername = rUsername;
}

九、@Slf4j and @Log4j

在需要打印日志的类中使用,项目中使用slf4jlog4j日志框架

十、@NonNull

该注解快速判断是否为空,为空抛出java.lang.NullPointerException

十一、@Synchronized

注解自动添加到同步机制,生成的代码并不是直接锁方法,而是锁代码块, 作用范围是方法上。

十二、@Cleanup

注解用于确保已分配的资源被释放(IO的连接关闭)。

原文始发于微信公众号(全栈程序员社区):超级全面的 Lombok 注解介绍,学一波!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/102404.html原文链接:https://javaforall.cn

0 人点赞