说到Java,其一个明显的特征就是实体类中会有大量的get,set方法。虽然说现在IDE都可以一键生成get,set,construct方法,但是每个类都得花个一分半分的时间。Lombok通过注释的方式帮助我们简化臃肿的Java代码。

(虽然通过Lombok在Java文件中以注释方式代替,但是通过编译器编译后会自动生成方法,因此不会影响代码运行效率)

项目主页:https://projectlombok.org

一、 Lombok安装

IDE需要安装插件,不然IDE会显示找不到方法(虽然能正常编译运行)

IDEA下>File>Setting>Plugins>搜索Lombok plugin,安装后IDE要求重启。重启后并导入Lombok包,就可以正常使用了。

二、 使用

http://jnb.ociweb.com/jnb/jnbJan2010.html

https://projectlombok.org/features/all

在官方的介绍文档中已经很详细的讲解了使用方法和Example,所以只讲常用的。

@Getter and @Setter

@Getter@Setter注释能为属性分别的生成getter和setter方法。在生成返回Boolean数据的时候依照惯例将以isFoo来代替getFoo函数名。需要注意如果Class中使用Lombok注释方法的字段属于一个相同名的getter或setter,不管参数和返回类型,将不会产生对应的getter,setter方法。

@Getter @Setter private boolean employed = true;
@Setter(AccessLevel.PROTECTED) private String name;

以上代码等同于

private boolean employed = true;
private String name;

public boolean isEmployed() {
    return employed;
}

public void setEmployed(final boolean employed) {
    this.employed = employed;
}

protected void setName(final String name) {
    this.name = name;
}    

@Data

@Data注释似乎是最常用的。它结合了@ToString,@EqualsAndHasCode,@Getter,@Setter注释。本质上来讲使用@Data注释相当于使用了默认的@ToString@EqualsAndHasCode以及给每个属性都加上了@Getter@Setter。给一个Class加上@Data的注释同时加上了Lombok的构造方法触发器,这将会加上一个public的构造器并加上@NonNullfinal的字段作为参数。折提供了Plain Old Java Object (POJO)所需要的一切。

@Data(staticConstructor="of")
public class Company {
    private final Person founder;
    private String name;
    private List<Person> employees;
}

等同于

public class Company {
    private final Person founder;
    private String name;
    private List<Person> employees;
    
    private Company(final Person founder) {
        this.founder = founder;
    }
    
    public static Company of(final Person founder) {
        return new Company(founder);
    }
    
    public Person getFounder() {
        return founder;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(final String name) {
        this.name = name;
    }
    
    public List<Person> getEmployees() {
        return employees;
    }
    
    public void setEmployees(final List<Person> employees) {
        this.employees = employees;
    }
    
    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (o == null) return false;
        if (o.getClass() != this.getClass()) return false;
        final Company other = (Company)o;
        if (this.founder == null ? other.founder != null : !this.founder.equals(other.founder)) return false;
        if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
        if (this.employees == null ? other.employees != null : !this.employees.equals(other.employees)) return false;
        return true;
    }
    
    @java.lang.Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = result * PRIME + (this.founder == null ? 0 : this.founder.hashCode());
        result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
        result = result * PRIME + (this.employees == null ? 0 : this.employees.hashCode());
        return result;
    }
    
    @java.lang.Override
    public java.lang.String toString() {
        return "Company(founder=" + founder + ", name=" + name + ", employees=" + employees + ")";
    }
}