设置@Builder和@NoArgsConstructor时,变量不会被初始化

时间:2017-07-07 17:55:11

标签: lombok

假设有一个A类:

exports.sendPush = functions.database.ref('/groups/{groupId}').onWrite(event => {

const groupId = event.params.groupId;

... // defining constants like msg

const participators = admin.database().ref('/groups/' + groupId + '/users').once('value');
let getDeviceTokensPromise = []
for (let part in participators) {

    getDeviceTokensPromise.push(admin.database().ref('/users/' + part + '/notificationtoken')).once('value');

}

return Promise.all([getDeviceTokensPromise, participators]).then(results => {

    const tokensSnapshot = results[0];
    const follower = results[1];

    // Check if there are any device tokens.
    if (!tokensSnapshot.hasChildren()) {

        return console.log('There are no notification tokens to send to.');

    }

    console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
    console.log('Fetched follower profile', follower);

// Notification details.
    const payload = {
        notification: {
            title: 'New meeting!',
            body: msg
        }
    };

// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());

// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
  // For each message check if there was an error.
  ...

当我通过调用@Getter @Setter @Builder @NoArgsConstructor @AllArgsConstructor public class A extends X implements Y, Z { private B b = new B(); private List<C> cList = new ArrayList<>(); } 创建对象时,没有变量被初始化,因此A a = new A();bcList

如果删除了null注释,则可以正常工作。

我在项目lombok文档中找不到任何解决方案。

我应该怎么做才能初始化它们?

1 个答案:

答案 0 :(得分:0)

根据https://github.com/rzwitserloot/lombok/pull/1429

&#34;目前,解决方法是键入一个no-args构造函数,并在构造函数中使用相同的表达式初始化字段。&#34;

@Getter @Setter @Builder @AllArgsConstructor
public class A extends X implements Y, Z {

    private B b = new B();

    private List<C> cList = new ArrayList<>();

    public A() {
        b = new B();
        cList = new ArrayList<>();
    }

}