加倍提升开发效率,继续深挖一下Lombok的使用

547 阅读8分钟

本篇来接续上一篇文章 Lombok 常规使用介绍,主要介绍一下 Lombok 非常用注解。

@Value

@Value 是 @Data 中不可变的注解,所有的属性默认都是 private 和 final 的,并且不会生成 setters 方法。默认情况下,类也是 final 的。像是 @Data ,会生成 toString() ,也会生成 equals() 和 hashCode() 方法,每个字段都会获得一个 getter 方法,并且还会生成一个覆盖每个参数的构造函数(在字段声明中初始化final 段除外)。

例如

@Value
public class ValueExample<T> {

    String name;

    @Wither(AccessLevel.PACKAGE)
    @NonFinal
    int age;

    double score;

    protected String[] tags;

}

它其实生成的是

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()) + ")";
  }
}

在实际使用过程中,@ Value是简写:final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true,level = AccessLevel.PRIVATE)@Getter,除了明确包括任何相关方法的实现外,只意味着不会发出警告。

例如,如果你编写了一个 toString() 方法,没有出现错误的话,那么 lombok 是不会为你再生成 toString() 方法的。

此外,任何显式构造函数,无论参数列表,都意味着lombok不会生成构造函数。如果你想让lombok生成所有的构造函数,请将 @AllArgsConstructor 标记在类上。你可以使用@ lombok.experimental.Tolerate标记任何构造函数或方法,以便将他们容纳在 lombok 中。

可以使用字段上的显式访问级别或使用 @NonFinal 或 @PackagePrivate 注解来覆盖默认的最终和默认的私有行为。@NonFinal 也可以在类上使用用来移除 final 关键字。

@Builder

@Builder注解为你的类生成复杂的构建器API。

@Builder 允许你使用以下代码自动生成使您的类可实例化所需的代码:

Person.builder().name("Adam Savage")
            .city("San Francisco").job("Mythbusters")
            .job("Unchained Reaction").build();

@Builder 可以被放在类、构造函数或者方法上。虽然 在类上 和 在构造函数上 模式是最常见的用例,但@Builder最容易用 方法 来解释。

在方法上标注 @Builder 遵循以下原则

  • 一个名为FooBuilder 的静态内部类,与静态方法(构建器)具有相同的类型参数。

  • 在 Builder 中,每个目标参数都有一个私有的非静态 、非 final 属性

  • 在 Builder 中,一个包级私有无参数的构造函数。

  • 在 Builder 中,与目标的每个参数类似的 setter方法,它与该参数具有相同的类型和相同的名称。 它返回构建器本身,以便可以链接setter调用,如上例所示。

  • 在 Builder 中,有一个 build()方法,它调用方法并传入每个字段。 它返回与目标相同的类型。

  • 在 Builder 中,一个合理的 toString() 实现

  • 在包含目标的类中,一个 builder() 方法,它创建构造器的新实例

@Builder 有很多冗余的代码,来看下面这个例子

@Builder
public class BuilderExample {

    private Long id;
    private String name;
    private Integer age;

}

它就相当于是下面这段代码

public class User {
    private Long id;
    private String name;
    private Integer age;

    User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public static User.UserBuilder builder() {
        return new User.UserBuilder();
    }

    public static class UserBuilder {
        private Long id;
        private String name;
        private Integer age;

        UserBuilder() {
        }

        public User.UserBuilder id(Long id) {
            this.id = id;
            return this;
        }

        public User.UserBuilder name(String name) {
            this.name = name;
            return this;
        }

        public User.UserBuilder age(Integer age) {
            this.age = age;
            return this;
        }

        public User build() {
            return new User(this.id, this.name, this.age);
        }

        public String toString() {
            return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ")";
        }
    }
}

但是这里面有一个问题,@Builder 不会为你生成无参构造函数,无参构造函数是很需要的,一般常常在反序列化的时候使用,所以必须要加上 @AllArgsConstructor@NoArgsConstructor

@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
}

@Singular

Singular 常常与 Builder 一起使用,通过使用 @Singular注解注释其中一个参数(如果使用@Builder注释方法或构造函数)或字段(如果使用@Builder注释类),lombok会将该构建器视为集合,并且会生成两个 add 方法而不是 setter 方法。其中一个方法会为Collection 增加单个元素,以及将另一个集合的所有元素添加到集合中。

例如下面这段使用了 @Singular 的代码

public class SingularExample {

    private Long id;
    private String name;
    private Integer age;
    @Singular
    private Set<String> Girlfriends;
}

它就如同下面这段代码一样

public User.UserBuilder Girlfriend(String Girlfriend) {
    if (this.Girlfriends == null) {
        this.Girlfriends = new ArrayList();
    }
    this.Girlfriends.add(Girlfriend);
        return this;
    }

public User.UserBuilder Girlfriends(Collection<? extends String> Girlfriends) {
    if (this.Girlfriends == null) {
        this.Girlfriends = new ArrayList();
    }

    this.Girlfriends.addAll(Girlfriends);
    return this;
}

public User.UserBuilder clearGirlfriends() {
    if (this.Girlfriends != null) {
        this.Girlfriends.clear();
    }

    return this;
}

@Synchronized

@Synchronized 是 synchronized 修饰方法的一个安全的变体。就像 synchronized,这个注解仅仅能够用在静态和实例方法上。它的操作类似于 synchronized 关键字,但它锁定在不同的对象上。如果你把此注解放在了一个私有属性上面,它会为你自动锁定 $lock 对象,如果没有这个属性,它将为你自动创建。如果你把此注解放在了静态方法上面,它将为你自动锁定 $LOCK对象。

你可以创建自己的锁,如果你已经自己创建了$ lock$ LOCK字段,那么它们当然不会生成。你还可以选择锁住其他字段,方法是将其指定为@Synchronized注解的参数,在此用法的变体中,不会自动创建字段,你必须自己显式创建它们,否则将会抛出错误。

锁定这个或你自己的类对象会产生其他影响,因为不受你控制的其他代码也可以锁定这些对象,这可能会导致竞争条件和其他线程错误。

下面是一个示例

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");  }}

它就相当于如下

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");    }  }}

@Cleanup

你可以使用 @Cleanup 注解在代码块执行完之前自动清理给定的资源。你可以通过使用@Cleanup注解来注释任何局部变量声明来执行此操作,如下所示:

@Cleanup InputStream in = new FileInputStream(“some / file”);

它会在代码块的结尾处自动执行 in.close()方法,lombok 能确保此方法在try…finally…块内执行

如果你要清理的对象类型没有 close() 方法,而是其他一些无参数方法,则可以指定此方法的名称,如下所示:

@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);

默认情况下,清除方法假定为close()。不能通过@Cleanup调用带有1个或多个参数的清理方法。下面是一个示例

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);        }    }}

就相当于是如下

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();      }    }  }}

@Getter(lazy=true)

你可以让lombok生成一个getter,它将计算一次值,这是第一次调用这个getter,并把它缓存起来。如果计算该值占用大量CPU,或者该值占用大量内存,则此功能非常有用。

要使用此功能,请创建一个私有final变量,并使用@Getter(lazy = true)注解进行标注。缓存起来的 cache 是线程安全的,你无需担心,下面是一个例子

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[i] = Math.asin(i);        }        return result;    }}

它就相当于如下示例

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[i] = Math.asin(i);    }    return result;  }}

@Log

你把 @Log 的变体放在你的类上(无论哪一个适用于你使用的日志系统)。然后,你有一个静态的最终日志字段,初始化为你的类的名称,然后你可以使用它来编写日志语句。

下面是不同的日志选择:

加倍提升开发效率,继续深挖一下Lombok的使用

下面是一个示例

import lombok.extern.apachecommons.CommonsLog;import lombok.extern.java.Log;import lombok.extern.slf4j.Slf4j;@Logpublic class LogExample {    public static void main(String[] args) {        log.severe("Something's wrong here");    }}@Slf4jclass LogExampleOther {    public static void main(String... args) {        log.error("Something else is wrong here");    }}@CommonsLog(topic="CounterLog")class LogExampleCategory {    public static void main(String... args) {        log.error("Calling the 'CounterLog' with a message");    }}

就相当于如下示例

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.severe("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");  }}

var 和 val

可以表示任何类型!

var 可以用来表示变量,类似其他语言中的 let

val 可以用来表示常量(final),类似其他语言中的 const

var str = "hello world";val list = Arrays.asList(1,2,3,4);System.out.println(str);for(val item : list){    System.out.printf("%dt",item);}

等价于

String str = "hello world";
final List<Integer> list = Arrays.asList(1,2,3,4);
System.out.println(str);
for(final Integer item : list){
    System.out.printf("%dt",item);
}

相关链接:

https://www.hellojava.com/a/74973.html
https://www.projectlombok.org/features/all

(点击即可跳转阅读)

1. SpringBoot内容聚合

2. 面试题内容聚合

3. 设计模式内容聚合

4. Mybatis内容聚合

5. 多线程内容聚合

看到这里了,关注一个?

加倍提升开发效率,继续深挖一下Lombok的使用