认识lombok 的@Builder注解对初始化的影响

  • 先上结论:如果类中用了@Builder注解,而属性没有任何注解话,那么在你初始化这个类的时候,如果你的属性赋值了默认值,则在你初始化该类后,属性的默认值则无效即获取会产生空指针异常

接下来我们来剖析下这中间发生了什么

image
image
  从上面的例子,可以发现Teacher 的address属性为空,这正是我们很常规初始化操作,获取这个address,接着对它进行操作, 如果此时它是null,则会出现空指针异常;
  比较Student中的address则是我们理想中的正常执行过程,是有一个默认值的对象,同时观察Student中的name 和age两个属性值默认值也出现了如同Teacher中的address现象,默认值消失了;
  **从表面来看,Student多了@Builder.Default的注解,这个注解确实就是解决这个问题关键,让你想要赋值的默认值来进行正确的初始化了。知道了这个注解的使用只是做到了知其然,我们要做做知其所以然,所以来看看下他们生成的class有什么区别? 以下代码反编译删除了equal和hashcode方法**

Teacher.class

public class Teacher {
private String name;
private List address = new ArrayList();

Teacher(String name, List address) {
    this.name = name;
    this.address = address;
}

public static Teacher.TeacherBuilder builder() {
    return new Teacher.TeacherBuilder();
}

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

public List getAddress() {
    return this.address;
}

public void setName(String name) {
    this.name = name;
}
public void setAddress(List address) {
    this.address = address;
}
protected boolean canEqual(Object other) {
    return other instanceof Teacher;
}

public String toString() {
    return "Teacher(name=" + this.getName() + ", address=" + this.getAddress() + ")";
}

public static class TeacherBuilder {
    private String name;
    private List address;

    TeacherBuilder() {
    }

    public Teacher.TeacherBuilder name(String name) {
        this.name = name;
        return this;
    }

    public Teacher.TeacherBuilder address(List address) {
        this.address = address;
        return this;
    }

    public Teacher build() {
        return new Teacher(this.name, this.address);
    }

    public String toString() {
        return "Teacher.TeacherBuilder(name=" + this.name + ", address=" + this.address + ")";
    }
}

Student.class
public class Student {
private String name = "c";
private int age = 25;
private long num;
private List address;

private static List $default$address() {
    return new ArrayList();
}

Student(String name, int age, long num, List address) {
    this.name = name;
    this.age = age;
    this.num = num;
    this.address = address;
}

public static Student.StudentBuilder builder() {
    return new Student.StudentBuilder();
}

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

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

public long getNum() {
    return this.num;
}

public List getAddress() {
    return this.address;
}

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

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

public void setNum(long num) {
    this.num = num;
}

public void setAddress(List address) {
    this.address = address;
}
protected boolean canEqual(Object other) {
    return other instanceof Student;
}

  public String toString() {
    return "Student(name=" + this.getName() + ", age=" + this.getAge() + ", num=" + this.getNum() + ", address=" + this.getAddress() + ")";
}

public static class StudentBuilder {
    private String name;
    private int age;
    private long num;
    private boolean address$set;
    private List address;

    StudentBuilder() {
    }

    public Student.StudentBuilder name(String name) {
        this.name = name;
        return this;
    }

    public Student.StudentBuilder age(int age) {
        this.age = age;
        return this;
    }

    public Student.StudentBuilder num(long num) {
        this.num = num;
        return this;
    }

    public Student.StudentBuilder address(List address) {
        this.address = address;
        this.address$set = true;
        return this;
    }

    public Student build() {
        List address = this.address;
        if(!this.address$set) {
            address = Student.$default$address();
        }

        return new Student(this.name, this.age, this.num, address);
    }

    public String toString() {
        return "Student.StudentBuilder(name=" + this.name + ", age=" + this.age + ", num=" + this.num + ", address=" + this.address + ")";
    }
}
}

看两个类的build方法,Student类在调用builde方法时,会判断this.addressset 这个变量是否为false,如果为false,则为这个address对象进行赋值默认值,这个变量就是由@Builder.Default注解产生的. 而如果你直接对address方法进行赋值话,则会将这个this.addressset进行赋值,这样调用build方法时,就不会再对address进行赋值了.
比较Teacher,没有对address属性增加@Builder.Default注解,所以在调用build方法时候,就不会产生判断是否要对address进行默认值的初始化了,所以你获取到的address就是null.

这下你知道你程序为什么会出现空指针异常了,为什么添加@Builder.Default注解就能解决问题了。所以对你用的东西进行深入了解,出现问题才能做到知其然知其所以然
image

作者:打伞的Fish
链接:https://www.jianshu.com/p/4e4cef2e82e1
来源:
著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(认识lombok 的@Builder注解对初始化的影响)