如何使用 lombok 配置 getter-only-lazy-initializing 集合字段?

How can I configure getter-only-lazy-initializing collection field with lombok?

如何让 lombok 生成遵循 getter 方法?

// getter only
// lazy initialization
public List<Student> getStudents() {

    if (students == null) {
        students = new ArrayList<Student>();
    }

    return students;
}

private List<Student> students;

一个简单的 @Getter 能做到吗?

我不确定这样做是否正确。

@Getter(lazy = true, @__({@XmlElement}))
private final List<Student> students = new ArrayList<Student>();

并且 lombok 生成以下方法。

@XmlElement
@java.lang.SuppressWarnings("all")
@javax.annotation.Generated("lombok")
public List<Student> getStudents() {
    Object value = this.students.get();
    if (value == null) {
        synchronized (this.students) {
            value = this.students.get();
            if (value == null) {
                final List<Student> actualValue = new ArrayList<Student>();
                value = actualValue == null ? this.students : actualValue;
                this.students.set(value);
            }
        }
    }
    return (List<Student>)(value == this.students ? null : value);
}

private final AtomicReference<Object> students = new AtomicReference<Object>();