简体   繁体   中英

Can lombok generate builder based on the type solely?

I have a use case where I don't want to use the @Builder on the class itself , so I created method based builder like this:

  @Builder(builderMethodName = "carBuilder")
  public static Car build(int speed, String brand){
    Car car = new Car();
    car.setSpeed(speed);
    car.setBrand(brand);
    return car;
  }

But how can I handle when the given class has ton of fields (over ~20). Should I really specify them as parameters and invoke the setters by hand?
Couldn't just lombok generate them automatically based on the type?

Currently this is not possible because Lombok avoids inspecting types from elsewhere on the classpath when processing a file.

What's your reason for not adding @Builder to the class itself? If you can describe a common use case for that, you or someone else might be able to add this ability to Lombok. However, currently I can't see any good reasons for this. Most libraries should be relatively easy to use already and if its your own code, why not just add Lombok?

Also the main reason I add @Builder is because I want my classes to be immutable - given that the actual object is still mutable here, why use builder rather than the setters?

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