Lombok使用

Lombok下载

官网下载链接:https://projectlombok.org/download

安装

  • 双击刚刚下载的软件
    在这里插入图片描述

  • 选择eclipse所在目录
    在这里插入图片描述目录
    在这里插入图片描述

点击安装,安装成功
在这里插入图片描述

  • 查看eclipse安装目录文件eclipse.ini
    在这里插入图片描述

使用Lombok

导入maven

<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.8</version>
</dependency>

样例展示,加上注解@Data,自动生成了set、get、 equals、hashCode、toString等方法
在这里插入图片描述
通过反编译查看生成的代码
在这里插入图片描述

@GETTER AND @SETTER

Lombok annotated code:

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

Equivalent Java source code:

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;
}
@NONNULL

Lombok annotated code from the class Family:

@Getter @Setter @NonNull
private List<Person> members;

Equivalent Java source code:

@NonNull
private List<Person> members;
 
public Family(@NonNull final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}
 
@NonNull
public List<Person> getMembers() {
    return members;
}
 
public void setMembers(@NonNull final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}
@TOSTRING

Lombok annotated code:

@ToString(callSuper=true,exclude="someExcludedField")
public class Foo extends Bar {
    private boolean someBoolean = true;
    private String someStringField;
    private float someExcludedField;
}

Equivalent Java source code:

public class Foo extends Bar {
    private boolean someBoolean = true;
    private String someStringField;
    private float someExcludedField;
 
    @java.lang.Override
    public java.lang.String toString() {
        return "Foo(super=" + super.toString() +
            ", someBoolean=" + someBoolean +
            ", someStringField=" + someStringField + ")";
    }
}
@EQUALSANDHASHCODE

Lombok annotated code:

@EqualsAndHashCode(callSuper=true,exclude={"address","city","state","zip"})
public class Person extends SentientBeing {
    enum Gender { Male, Female }
 
    @NonNull private String name;
    @NonNull private Gender gender;
 
    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}

Equivalent Java source code:

public class Person extends SentientBeing {
 
    enum Gender {
        /*public static final*/ Male /* = new Gender() */,
        /*public static final*/ Female /* = new Gender() */;
    }
    @NonNull
    private String name;
    @NonNull
    private Gender gender;
    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
 
    @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;
        if (!super.equals(o)) return false;
        final Person other = (Person)o;
        if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
        if (this.gender == null ? other.gender != null : !this.gender.equals(other.gender)) return false;
        if (this.ssn == null ? other.ssn != null : !this.ssn.equals(other.ssn)) return false;
        return true;
    }
 
    @java.lang.Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = result * PRIME + super.hashCode();
        result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
        result = result * PRIME + (this.gender == null ? 0 : this.gender.hashCode());
        result = result * PRIME + (this.ssn == null ? 0 : this.ssn.hashCode());
        return result;
    }
}
@DATA

Lombok annotated code:

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

Equivalent Java source code:

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

Lombok annotated code:

public void testCleanUp() {
    try {
        @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(new byte[] {'Y','e','s'});
        System.out.println(baos.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Equivalent Java source code:

public void testCleanUp() {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            baos.write(new byte[]{'Y', 'e', 's'});
            System.out.println(baos.toString());
        } finally {
            baos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@SYNCHRONIZED

Lombok annotated code:

private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
 
@Synchronized
public String synchronizedFormat(Date date) {
    return format.format(date);
}

Equivalent Java source code:

private final java.lang.Object $lock = new java.lang.Object[0];
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
 
public String synchronizedFormat(Date date) {
    synchronized ($lock) {
        return format.format(date);
    }
}
@SneakyThrows

Lombok annotated code:

@SneakyThrows
public void testSneakyThrows() {
    throw new IllegalAccessException();
}

Equivalent Java source code:

	public void testSneakyThrows() {
    try {
        throw new IllegalAccessException();
    } catch (java.lang.Throwable $ex) {
        throw lombok.Lombok.sneakyThrow($ex);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值