A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

原文链接:https://www.itsleuth.cn/archives/javatool002

在面向对象编程中必不可少需要在代码中定义对象模型,而在基于Java的业务平台开发实践中尤其如此。相信大家在平时开发中也深有感触,本来是没有多少代码开发量的,但是因为定义的业务模型对象比较多,而需要重复写Getter/Setter、构造器方法、字符串输出的ToString方法和Equals/HashCode方法等。那么今天本文将向大家介绍一款在Eclipse/Intellij IDEA主流的开发环境中都可以使用的Java开发神器替大家完成这些繁琐的操作。

1、Lombok的简介

Lombok是一款Java开发插件,使得Java开发者可以通过其定义的一些注解来消除业务工程中冗长和繁琐的代码,尤其对于简单的Java模型对象(POJO)。在开发环境中使用Lombok插件后,Java开发人员可以节省出重复构建,诸如hashCode和equals这样的方法以及各种业务对象模型的accessor和ToString等方法的大量时间。对于这些方法,它能够在编译源代码期间自动帮我们生成这些方法,并没有如反射那样降低程序的性能。

2、Intellij中安装Lombok的插件

想要体验一把Lombok的话,得先在自己的开发环境中安装上对应的插件。下面先为大家展示下如何在Intellij中安装上Lombok插件。

通过IntelliJ的插件中心寻找Lombok

从Intellij插件中心安装Lombok

另外需要注意的是,在使用lombok注解的时候记得要导入lombok.jar包到工程,如果使用的是Maven的工程项目的话,要在其pom.xml中添加依赖如下:

<dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>    <version>1.16.14</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

好了,就这么几步后就可以在Java工程中开始用Lombok这款开发神器了。下文将会给大家介绍Lombok中一些注解的使用方法,让大家对如何用这些注解有一个大致的了解。

3、Lombok常用注解介绍

下面先来看下Lombok中主要几个常用注解介绍:

  • val:用在局部变量前面,可以将变量声明为final
  • @NonNull:用在方法参数前,会自动对该参数进行非空校验,为空抛出NPE(NullPointerException)
  • @Cleanup:自动管理资源,用在局部变量之前,在当前变量范围内即将执行完毕退出前会清理资源,生成try-finally的代码关闭流
  • @Getter/@Setter:用在属性上,不用自己手写setter和getter方法,还可指定访问范围
  • @ToString:用在类上,可以自动覆写toString方法
  • @EqualsAndHashCode:用在类上,自动生成equals方法和hashCode方法
  • @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:用在类上,自动生成无参构造和使用所有参数的构造函数以及把所有@NonNull属性作为参数的构造函数
  • @Data:用在类上,相当于同时使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstrutor这些注解,对POJO类十分有用
  • @Value:用在类上,是@Data的不可变形式,相当于为属性添加final声明,只提供getter方法,而不提供setter方法
  • @Builder:用在类、构造器、方法上,为你提供复杂的builder API方法
  • @SneakyThrows:自动抛受检异常,而无需显式在方法上使用throws语句
  • @Synchronized:用在方法上,将方法声明为同步的,并自动加锁
  • @Getter(lazy=true):可以替代经典的Double Check Lock样板代码
  • @Log:根据不同的注解生成不同类型的log对象,但是实例名称都是log,有多种可选实现类
4、Lombok的基本使用示例

1. val

用val作为局部变量的声明类型,而不使用真实的类型,编译器将从初始化表达式中推断出该类型并把变量设置为final,这个功能只适用于局部变量和foreach循环中,类的属性不适用此功能,而且初始化表达式是必须的。

使用Lombok注解

import java.util.ArrayList;import java.util.HashMap;import lombok.val;public class ValExample {  public String example() {    val example = new ArrayList<String>();    example.add("Hello, World!");    val foo = example.get(0);    return foo.toLowerCase();  }  public void example2() {    val map = new HashMap<Integer, String>();    map.put(0, "zero");    map.put(5, "five");    for (val entry : map.entrySet()) {      System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

相当于Java

import java.util.ArrayList;import java.util.HashMap;import lombok.val;public class ValExample {  public String example() {    final ArrayList<String> example = new ArrayList<String>();    example.add("Hello, World!");    final String foo = example.get(0);    return foo.toLowerCase();  }  public void example2() {    final HashMap<Integer, String> map = new HashMap<Integer, String>();    map.put(0, "zero");    map.put(5, "five");    for (final Map.Entry<Integer, String> entry : map.entrySet()) {      System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2. @NonNull

注解能够为方法或构造函数的参数提供非空检查

使用Lombok注解

import lombok.NonNull;public class NonNullExample extends Something {  private String name;  public NonNullExample(@NonNull Person person) {    super("Hello");    this.name = person.getName();  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

相当于Java

import lombok.NonNull;public class NonNullExample extends Something {  private String name;  public NonNullExample(@NonNull Person person) {    super("Hello");    if (person == null) {      throw new NullPointerException("person is marked @NonNull but is null");    }    this.name = person.getName();  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3. @Cleanup

该注解使用在属性前,该注解是用来保证分配的资源被释放。在本地变量上使用该注解,任何后续代码都将封装在try/finally中,确保当前作用于中的资源被释放。默认@Cleanup清理的方法为close,可以使用value指定不同的方法名称。

使用Lombok注解

import lombok.Cleanup;import java.io.*;public class CleanupExample {  public static void main(String[] args) throws IOException {    @Cleanup InputStream in = new FileInputStream(args[0]);    @Cleanup OutputStream out = new FileOutputStream(args[1]);    byte[] b = new byte[10000];    while (true) {      int r = in.read(b);      if (r == -1) break;      out.write(b, 0, r);    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

相当于Java

import java.io.*;public class CleanupExample {  public static void main(String[] args) throws IOException {    InputStream in = new FileInputStream(args[0]);    try {      OutputStream out = new FileOutputStream(args[1]);      try {        byte[] b = new byte[10000];        while (true) {          int r = in.read(b);          if (r == -1) break;          out.write(b, 0, r);        }      } finally {        if (out != null) {          out.close();        }      }    } finally {      if (in != null) {        in.close();      }    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

4. @Getter/@Setter

用在属性上,自动生成setter和getter方法,默认生成的方法是public的,当然也可以通过AccessLevel指定访问范围。
用在类上,相当于在所有非静态属性上添加了注解。
如果想要在生成的方法上添加注解可以使用onMethod=@__({@AnnotationsHere})
如果想要在生成方法的参数上添加注解可以使用onParam=@__({@AnnotationsHere})

使用Lombok注解

import lombok.AccessLevel;import lombok.Getter;import lombok.Setter;public class GetterSetterExample {  /**   * Age of the person. Water is wet.   *    * @param age New value for this person's age. Sky is blue.   * @return The current value of this person's age. Circles are round.   */  @Getter @Setter private int age = 10;  /**   * Name of the person.   * -- SETTER --   * Changes the name of this person.   *    * @param name The new value.   */  @Setter(AccessLevel.PROTECTED) private String name;  @Override public String toString() {    return String.format("%s (age: %d)", name, age);  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

相当于Java

public class GetterSetterExample {  /**   * Age of the person. Water is wet.   */  private int age = 10;  /**   * Name of the person.   */  private String name;  @Override public String toString() {    return String.format("%s (age: %d)", name, age);  }  /**   * Age of the person. Water is wet.   *   * @return The current value of this person's age. Circles are round.   */  public int getAge() {    return age;  }  /**   * Age of the person. Water is wet.   *   * @param age New value for this person's age. Sky is blue.   */  public void setAge(int age) {    this.age = age;  }  /**   * Changes the name of this person.   *   * @param name The new value.   */  protected void setName(String name) {    this.name = name;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

5. @ToString

用在类上,可以自动覆写toString方法,该注解默认打印类名加上所有非静态字段以名称-值的形式输出

使用Lombok注解

import lombok.ToString;@ToStringpublic 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;    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

相当于Java

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.getName();  }  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) + ")";  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6. @EqualsAndHashCode

用在类上,自动生成equals方法和hashCode方法

使用Lombok注解

import lombok.EqualsAndHashCode;@EqualsAndHashCodepublic 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;    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

相当于Java

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;    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

7. @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

用在类上,自动生成无参构造和使用所有参数的构造函数以及把所有@NonNull属性作为参数的构造函数,如果指定staticName = “of”参数,同时还会生成一个返回类对象的静态工厂方法,比使用构造函数方便很多

使用Lombok注解

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;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

相当于Java

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() {    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

8. @Data

用在类上,相当于同时使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstrutor这些注解,对POJO类十分有用

使用Lombok注解

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;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

相当于Java

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;    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

9. @Value

用在类上,是@Data的不可变形式,相当于为属性添加final声明,只提供getter方法,而不提供setter方法

使用Lombok注解

import lombok.AccessLevel;import lombok.experimental.NonFinal;import lombok.experimental.Value;import lombok.experimental.Wither;import lombok.ToString;@Value public class ValueExample {  String name;  @Wither(AccessLevel.PACKAGE) @NonFinal int age;  double score;  protected String[] tags;  @ToString(includeFieldNames=true)  @Value(staticConstructor="of")  public static class Exercise<T> {    String name;    T value;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

相当于Java

import java.util.Arrays;public final class ValueExample {  private final String name;  private int age;  private final double score;  protected final String[] tags;  @java.beans.ConstructorProperties({"name", "age", "score", "tags"})  public ValueExample(String name, int age, double score, String[] tags) {    this.name = name;    this.age = age;    this.score = score;    this.tags = tags;  }  public String getName() {    return this.name;  }  public int getAge() {    return this.age;  }  public double getScore() {    return this.score;  }  public String[] getTags() {    return this.tags;  }  @java.lang.Override  public boolean equals(Object o) {    if (o == this) return true;    if (!(o instanceof ValueExample)) return false;    final ValueExample other = (ValueExample)o;    final Object this$name = this.getName();    final Object other$name = other.getName();    if (this$name == null ? other$name != null : !this$name.equals(other$name)) 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;  }  @java.lang.Override  public int hashCode() {    final int PRIME = 59;    int result = 1;    final Object $name = this.getName();    result = result * PRIME + ($name == null ? 43 : $name.hashCode());    result = result * PRIME + this.getAge();    final long $score = Double.doubleToLongBits(this.getScore());    result = result * PRIME + (int)($score >>> 32 ^ $score);    result = result * PRIME + Arrays.deepHashCode(this.getTags());    return result;  }  @java.lang.Override  public String toString() {    return "ValueExample(name=" + getName() + ", age=" + getAge() + ", score=" + getScore() + ", tags=" + Arrays.deepToString(getTags()) + ")";  }  ValueExample withAge(int age) {    return this.age == age ? this : new ValueExample(name, age, score, tags);  }  public static final 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;    }    @java.lang.Override    public boolean equals(Object o) {      if (o == this) return true;      if (!(o instanceof ValueExample.Exercise)) return false;      final Exercise<?> other = (Exercise<?>)o;      final Object this$name = this.getName();      final Object other$name = other.getName();      if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;      final Object this$value = this.getValue();      final Object other$value = other.getValue();      if (this$value == null ? other$value != null : !this$value.equals(other$value)) return false;      return true;    }    @java.lang.Override    public int hashCode() {      final int PRIME = 59;      int result = 1;      final Object $name = this.getName();      result = result * PRIME + ($name == null ? 43 : $name.hashCode());      final Object $value = this.getValue();      result = result * PRIME + ($value == null ? 43 : $value.hashCode());      return result;    }    @java.lang.Override    public String toString() {      return "ValueExample.Exercise(name=" + getName() + ", value=" + getValue() + ")";    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120

10. @Builder

用在类、构造器、方法上,为你提供复杂的builder API方法

使用Lombok注解

import lombok.Builder;import lombok.Singular;import java.util.Set;@Builderpublic class BuilderExample {  @Builder.Default private long created = System.currentTimeMillis();  private String name;  private int age;  @Singular private Set<String> occupations;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

相当于Java

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 + ")";    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

11. @SneakyThrows

自动抛受检异常,而无需显式在方法上使用throws语句
我们知道,java对于检查异常,需要在编码时进行捕获,或者throws抛出。
该注解的作用是将检查异常包装为运行时异常,那么编码时就无需处理异常了。
提示:不过这并不是友好的编码方式,因为你编写的api的使用者,不能显式的获知需要处理检查异常。

使用Lombok注解

import lombok.SneakyThrows;public class SneakyThrowsExample implements Runnable {  @SneakyThrows(UnsupportedEncodingException.class)  public String utf8ToString(byte[] bytes) {    return new String(bytes, "UTF-8");  }  @SneakyThrows  public void run() {    throw new Throwable();  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

相当于Java

import lombok.Lombok;public class SneakyThrowsExample implements Runnable {  public String utf8ToString(byte[] bytes) {    try {      return new String(bytes, "UTF-8");    } catch (UnsupportedEncodingException e) {      throw Lombok.sneakyThrow(e);    }  }  public void run() {    try {      throw new Throwable();    } catch (Throwable t) {      throw Lombok.sneakyThrow(t);    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

12. @Synchronized

用在方法上,将方法声明为同步的,并自动加锁,而锁对象是一个私有的属性<span class="MathJax" id="MathJax-Element-1-Frame" tabindex="0" data-mathml="lock或" role="presentation" style="box-sizing: border-box; outline: 0px; display: inline; line-height: normal; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; word-break: break-all; position: relative;">lock或lock或LOCK,而java中的synchronized关键字锁对象是this,锁在this或者自己的类对象上存在副作用,就是你不能阻止非受控代码去锁this或者类对象,这可能会导致竞争条件或者其它线程错误

使用Lombok注解

import lombok.Synchronized;public class SynchronizedExample {  private final Object readLock = new Object();  @Synchronized  public static void hello() {    System.out.println("world");  }  @Synchronized  public int answerToLife() {    return 42;  }  @Synchronized("readLock")  public void foo() {    System.out.println("bar");  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

相当于Java

public class SynchronizedExample {  private static final Object $LOCK = new Object[0];  private final Object $lock = new Object[0];  private final Object readLock = new Object();  public static void hello() {    synchronized($LOCK) {      System.out.println("world");    }  }  public int answerToLife() {    synchronized($lock) {      return 42;    }  }  public void foo() {    synchronized(readLock) {      System.out.println("bar");    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

12. @Getter(lazy=true)

这个注解的作用相当于缓存,就是我在第一次调用后这个值会一直存在,不在浪费资源去重复生成了,使用了getter这个annotation可以在实际使用到cached的时候生成cached,同时,Lombok会自动去管理线程安全的问题,不会存在重复赋值的问题

使用Lombok注解

import lombok.Getter;public class GetterLazyExample {  @Getter(lazy=true) private final double[] cached = expensive();  private double[] expensive() {    double[] result = new double[1000000];    for (int i = 0; i < result.length; i++) {      result = Math.asin(i);    }    return result;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

相当于Java

public class GetterLazyExample {  private final java.util.concurrent.AtomicReference<java.lang.Object> cached = new java.util.concurrent.AtomicReference<java.lang.Object>();  public double[] getCached() {    java.lang.Object value = this.cached.get();    if (value == null) {      synchronized(this.cached) {        value = this.cached.get();        if (value == null) {          final double[] actualValue = expensive();          value = actualValue == null ? this.cached : actualValue;          this.cached.set(value);        }      }    }    return (double[])(value == this.cached ? null : value);  }  private double[] expensive() {    double[] result = new double[1000000];    for (int i = 0; i < result.length; i++) {      result = Math.asin(i);    }    return result;  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

12. @Log

在任意类上添加此注解,Lombok会生成一个final的基于此类的logger实例,实例对象的名称为log。有以下几种可选实现类:
- @CommonsLog
- private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
- @Flogger
- private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
- @JBossLog
- private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
- @Log
- private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
- @Log4j
- private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
- @Log4j2
- private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
- @Slf4j
- private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
- @XSlf4j
- private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

使用Lombok注解

import lombok.extern.java.Log;import lombok.extern.slf4j.Slf4j;@Logpublic class LogExample {  public static void main(String... args) {    log.error("Something's wrong here");  }}@Slf4jpublic class LogExampleOther {  public static void main(String... args) {    log.error("Something else is wrong here");  }}@CommonsLog(topic="CounterLog")public class LogExampleCategory {  public static void main(String... args) {    log.error("Calling the 'CounterLog' with a message");  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

相当于Java

public class LogExample {  private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());  public static void main(String... args) {    log.error("Something's wrong here");  }}public class LogExampleOther {  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);  public static void main(String... args) {    log.error("Something else is wrong here");  }}public class LogExampleCategory {  private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");  public static void main(String... args) {    log.error("Calling the 'CounterLog' with a message");  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23


版权声明:本文为博主原创文章,未经博主允许不得转载。        https://blog.csdn.net/jiangjiandecsd/article/details/81114704


4 个回复

倒序浏览
优秀,奈斯
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马