简体   繁体   中英

@Delegate and @Builder combination in lombok

I have a class Acc.java from Third party jar as follows

@Data
public class Acc{

private String a;

private String b;

}

I am using class Acc in my class which Bcc.java as Instance variable. and I want to create builder for Acc instance variables in Bcc.java using lombok.

I tried in following way

@Data
@Builder
public class Bcc{

@Delegate
private Acc acc;
}

from above code I see Bcc.builder().acc() which is builder having method to build Acc object.

But I need Bcc.builder.a("").b("").build(). "a" and "b" are which are part of Acc.java class.

Could some one help me..?

I don't think lombok supports this out of the box. But here is some code to support this.

@Data
@Builder
public class Bcc {

    @Delegate
    private Acc acc;
    
    public static class BccBuilder {

        public Bcc.BccBuilder a(String a) {
            if (this.acc == null) this.acc = new Acc();
            this.acc.setA(a);
            return this;
        }

        public Bcc.BccBuilder b(String b) {
            if (this.acc == null) this.acc = new Acc();
            this.acc.setB(b);
            return this;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM