1. 程式人生 > >lombok @Builder註解

lombok @Builder註解

這裡對lombok的@Builder@Data組合的使用示例

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class People {
    private String name;
    private String sex;
    private int age;
}
@Test
    public void testBuilderAnnotation(){
        People.PeopleBuilder builder = People.builder();
        People.PeopleBuilder peopleBuilder 
= builder.name("LuoTianyan").age(23); if(true){ peopleBuilder.sex("female"); } People luoTianyan = peopleBuilder.build(); System.out.println(luoTianyan.toString()); //People(name=LuoTianyan, sex=female, age=23) People people = new People("LuoTianyan","female",23); System.out.println(luoTianyan.equals(people));
//true }

class People加上了@Builder@Data註解後,多了一個靜態內部類PeopleBuilder,People呼叫靜態方法builder生成PeopleBuilder物件,PeopleBuilder可以使用".屬性名(屬性值)"的方式進行屬性設定,再呼叫build()方法就生成了People物件,並且如果兩個物件的屬性如果相同,就會認為這兩個物件相等,即重寫了hashCode和equls方法。

 

在Intellij IDEA下,檢視反編譯的檔案People.class;

可以看到,生成的有:

Getter和Setter方法;

訪問型別是private無參構造方法

,訪問型別為defaul的全部引數的構造方法;

重寫hashCodeequals、toString方法,則People可以做為Map的key;

訪問型別為public的靜態方法builder,返回的是People.PeopleBuilder物件,非單例;

訪問型別為public的靜態內部類PeopleBuilder,該類主要有build方法,返回型別是People;

最後還有個canEqual方法,判斷是否與People同類型。

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


public class People {
    private String name;
    private String sex;
    private int age;

    People(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public static People.PeopleBuilder builder() {
        return new People.PeopleBuilder();
    }

    private People() {
    }

    public String getName() {
        return this.name;
    }

    public String getSex() {
        return this.sex;
    }

    public int getAge() {
        return this.age;
    }

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

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof People)) {
            return false;
        } else {
            People other = (People)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label39: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label39;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label39;
                    }

                    return false;
                }

                Object this$sex = this.getSex();
                Object other$sex = other.getSex();
                if (this$sex == null) {
                    if (other$sex != null) {
                        return false;
                    }
                } else if (!this$sex.equals(other$sex)) {
                    return false;
                }

                if (this.getAge() != other.getAge()) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof People;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }

    public String toString() {
        return "People(name=" + this.getName() + ", sex=" + this.getSex() + ", age=" + this.getAge() + ")";
    }

    public static class PeopleBuilder {
        private String name;
        private String sex;
        private int age;

        PeopleBuilder() {
        }

        public People.PeopleBuilder name(String name) {
            this.name = name;
            return this;
        }

        public People.PeopleBuilder sex(String sex) {
            this.sex = sex;
            return this;
        }

        public People.PeopleBuilder age(int age) {
            this.age = age;
            return this;
        }

        public People build() {
            return new People(this.name, this.sex, this.age);
        }

        public String toString() {
            return "People.PeopleBuilder(name=" + this.name + ", sex=" + this.sex + ", age=" + this.age + ")";
        }
    }
}