1

能做什么?

在使用lombok之前:

public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String[] getAuthors() {
        return authors;
    }

    public void setAuthors(String[] authors) {
        this.authors = authors;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Book)) return false;
        if (!super.equals(o)) return false;

        Book book = (Book) o;

        if (getId() != null ? !getId().equals(book.getId()) : book.getId() != null) return false;
        if (getTitle() != null ? !getTitle().equals(book.getTitle()) : book.getTitle() != null) return false;
        if (getReleaseDate() != null ? !getReleaseDate().equals(book.getReleaseDate()) : book.getReleaseDate() != null)
            return false;
        if (getPrice() != null ? !getPrice().equals(book.getPrice()) : book.getPrice() != null) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(getAuthors(), book.getAuthors())) return false;
        return getPublisher() != null ? getPublisher().equals(book.getPublisher()) : book.getPublisher() == null;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (getId() != null ? getId().hashCode() : 0);
        result = 31 * result + (getTitle() != null ? getTitle().hashCode() : 0);
        result = 31 * result + (getReleaseDate() != null ? getReleaseDate().hashCode() : 0);
        result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
        result = 31 * result + Arrays.hashCode(getAuthors());
        result = 31 * result + (getPublisher() != null ? getPublisher().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", releaseDate=" + releaseDate +
                ", price=" + price +
                ", authors=" + Arrays.toString(authors) +
                ", publisher='" + publisher + '\'' +
                '}';
    }
}

在使用之后:

@Data
public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;
}

并且从功能上,使用后=使用前。
说的简单点:从此以后不需要写GetterSettertoStringhashCodecanEqualconstructor这些大部分相似,却又不得不写的代码了。

怎么用?

一共有三个步骤

  1. 为你的开发工具安装插件。

    • IntelliJ IDEA: settings->Plugins->Browse Repositories->搜索Lombok Plugin->install

    • Eclipse / MyEclipse / Spring Tool Suite / JBoss Developer Studio: 看这里

    • Netbeans: 看这里

  2. 下载并导入Jar包,下载地址

  3. 在需要的类上标注注解

目前我使用的版本是1.16.16,有下面几个常用注解:

@Date

生成GetterSettertoStringequalshashCode方法。

可标注:Class

@Value

生成GettertoStringequalshashCode、还有全字段的构造方法。并且会将字段全都添加final

可标注:Class

@Builder

在类内部生成一个名为类名+Builder的内部类,用于快速构建。

可标注:Class

你可以这样使用它:

@Builder
public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;

}
public static void main(String[] args) {
    Book book = Book.builder().id(1).title("书名").build();
}

@Getter/@Setter

生成GetterSetter方法。如果标注在类上则会生成这个类中所有字段的GetterSetter方法。

可标注:Field,Class

@NonNull

为你的构造函数、Setter添加空值校验。标注的字段赋值为空时,会抛出NullPointerException

可标注:Field,Method,Parameter,Local Variable

为什么?

自从Java 6起,Javac就支持了一个名叫JSR 269 Pluggable Annotation Processing API的规范,它的作用就是:如果你实现了这个API,在Javac编译时就会得到调用。而lombok正是运用了这一点。

流程是这样的:

  1. Javac编译源代码,并生成语法树(AST)

  2. Javac寻找实现了JSR 269 .... API的代码(lombok),并调用。

  3. lombok寻找被标注了注解的类,修改Javac生成的语法树(AST)。

  4. Javac将语法树生成为字节码(.class)

就到这里了

它还具备很多好用的功能,你可以去这里看看。


Francis_Lee
217 声望7 粉丝

Don't let your dreams be dreams.