How to run code after constructor in a Lombok builder

后端 未结 4 1701
庸人自扰
庸人自扰 2021-02-07 01:38

I have a class that I want to use Lombok.Builder and I need pre-process of some parameters. Something like this:

@Builder
public class Foo {
   public String val         


        
4条回答
  •  半阙折子戏
    2021-02-07 02:10

    This works for me, not a complete solution, but quick and easy.

    @Builder
    @AllArgsConstructor
    public class Foo {
       @Builder.Default
       int bar = 42;
       Foo init() {
          // perform values initialisation
         bar = 451;   // replaces 314
         return foo;
       }
       static Foo test() {
           return new FooBuilder()  // defaults to 42
               .bar(314)  // replaces 42 with 314
               .build()
               .init();   // replaces 314 with 451
       }
    }
    

提交回复
热议问题