Hi everyone,
First of all, I'm a big fan of the lombok project. You guys are doing a great job, thanks for the good work!
I was awaiting the @Builder feature in particular and I'm really glad it's been implemented as I find this design pattern really useful. I'd like to share my thoughts on this, because there's one feature that's missing IMHO: The ability to validate the created bean.
What I like to do when implementing builders is to add a validate() method that is executed just before the object gets built (first call in the build() method). Such validation lets you check if the user has provided all parameters in the proper way. This is partially supported by annotating the required fields with @NonNull, but it only lets you check for nulls (and throws a NullPointerException instead of IllegalArgumentException, but that's a different topic).
What would be nice is have support for a validate() method you could implement the way you want. One way to implement this would be:
* Add an optional "validateMethod" parameter to the Builder annotation.
* If the parameter is present, then the provided method is called after the object is created by the builder. It would be cleaner to validate the fields before the builder even creates the object, but since it's autogenerated, we don't have access to it. On the other hand, a validate() method on the object class could be reusable also outside the builder, which is a nice bonus.
* If the object is not properly built (parameters are missing), the validate() method could throw a IllegalArgumentException (or whatever exception the user choses to throw).
I believe this feature is a non-invasive way to add custom validation to the builder pattern. It's purely optional and backwards compatible - if you don't provide the validateMethod parameter, no validation is done.
Let me know what you think.
Thanks,
Wojciech Górski