A possibly less-intrusive variation on this would be to have the initialization mechanism be available only when using @Builder. Having an @Builder(initializerMethod="start") would solve the use case nicely. The builder would call build(), followed by the initializer, if specified.
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
In your case, it could be better to drop filename as it's contained in file. Using @AllArgsConstructor(access=PRIVATE) and adding a static factory method is a possible workaround you didn't mention. Alternatively, you could write a delegating constructor.
In your case, it could be better to drop filename as it's contained in file. Using @AllArgsConstructor(access=PRIVATE) and adding a static factory method is a possible workaround you didn't mention. Alternatively, you could write a delegating constructor.
I may have formulated my proposition backwards...What I'd really like is to keep @Builder and @XArgsConstructor functionality. Writing constructors and factory methods is so 2009...
Also my proposal would not only cover resource initialization, but would allow call external methods too. Like if you need to register the new object in a global map, or clear a cache in response to this new object being built.
Having @InConstructor on any () method would definitely work. I've once hacked my way in Lombok to add varargs (...) setters (or constructor, I dont remember). It worked, but it seems my approach was naive. This doesn't look that complicated either, but I'm sure there'd be subtleties.
How would you go about writing a custom build() method when the whole Builder is generated behind the scenes?
IIRC, you can write an incomplete static inner class and let lombok complete it.
public static class Builder {
Now it looks like it need to also define each field I want my custom setters to set, so I end up doing half the work (yes I am lazy).
Ok, I found a way. I don't if this will be satifying to you.
Which I am pretty comfortable with, even if it means the client can still call unsafeBuilder (or now, unsafeBuild) directly, they'd have to be a bit dumb to do so. Although often the client is me, so it's still a possibility.
So maybe these patterns should simply be documented better? We need a Lombook.