Validation of object at build time (@Builder)

1,615 views
Skip to first unread message

JamieH

unread,
Sep 7, 2015, 7:01:27 AM9/7/15
to Project Lombok
I'd like to use @Builder to create objects, but often objects require validation at construction time beyond simple null checks, eg an integer field should only be greater than 4 if a boolean field is true.

Would anyone else find it useful to have a @Validator annotation that could be applied to methods with void return and zero arguments? All methods with this annotation in a class would be called at construction time when building an object, to give them an opportunity to throw something.


Marco Servetto

unread,
Sep 7, 2015, 7:28:48 AM9/7/15
to project...@googlegroups.com
Yes, there was some discussion about this in the past.
I think it would be a fundamental feature, with simple semantic and
many potential usages.
More that @Validator
I would call it @Init since it can also be usefull to do post
processing to complete an object initialization, like registering it
in a framework.
> --
> 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-lombo...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Martin Grajcar

unread,
Sep 7, 2015, 10:37:41 AM9/7/15
to project...@googlegroups.com
There's already a feature request for this: https://github.com/rzwitserloot/lombok/issues/674

And there have been several discussions, e.g.,
https://groups.google.com/forum/#!searchin/project-lombok/validator/project-lombok/uRViUbFeXLA/71sG5ArovTwJ, which is convoluted by an idea of mine, namely optionally moving code normally executed before constructor (initializer expressions and blocks) to the end of the constructor.

Even the name has been discussed and found not perfect as @Validator has tons of meanings unrelated to construction and as they're other things unrelated to validations you may want to use it for.

Some other proposal might have been @AfterConstruct, which doesn't fit the part about invocation after setters. I guess, @Init sounds better. Concerning the invocation after setters, it could be @Init(alsoAfterSetters=boolean), with false being the default.

A combo like @AfterConstruct and @AfterSetter could be nice, but these are two annotations in place of one.

The real problem is not the name, but the low priority. If only someone could pick it up (I currently can't and hacking on Lombok is not easy for me)....

Christian Sterzl

unread,
Sep 8, 2015, 10:45:04 AM9/8/15
to Project Lombok
There is already a Standard Name: @PostConstruct

Martin Grajcar

unread,
Sep 10, 2015, 3:00:30 AM9/10/15
to project...@googlegroups.com
And that's why we should avoid the name @PostConstruct, isn't it? It has already a meaning, which differs from what Lombok would do. There are constrains contradicting the Lombok RFE, e.g.,
  • Only one method can be annotated with this annotation.
  • The return type of the method MUST be void.
  • The method MUST NOT throw a checked exception.
  • "executed after dependency injection is done" - this means not in the constructor and not just after it (because of field and setter injection)
What's worse: It could lead to confusion: What should Lombok do, if it sees @javax.annotation.PostConstruct? Probably nothing, or maybe issue a warning as it may be because of someone confusing the two. OTOH, I can imagine people using both annotations in a single project, @lombok.PostConstruct for value objects and the other for dependency injected ones.
Reply all
Reply to author
Forward
0 new messages