[FEATURE] "simple" @PostBuild annotation
Author: MichelMunozCreated May 26, 2020Updated Sep 1, 2026
Description
The idea is to have a basic, simple, ability to validate (or post-process) an object created by a builder without polluting lombok @Builder with non-building problems ; to do so :
- have the possibility to annotate one instance no-arg-and-no-checked-throw method (of the class decorated with a builder) with a
@PostBuildExample:
@Builder
public MyClass {
private String someField;
@PostBuild
private void validation() {
// do stuff here
}
}- in the lomboked version, that method would be called by the
.build()after creating the object, but before returning it (instead of creation-return currently done)
Precisions/Rationale/Benefits
- separation of concerns: this avoids polluting
@Builderwith validation or other problems - the logic here is strictly build-related - "callback after object is built" (no need to support@Validatorsor@WhateverCheckerby the builder, the validation logic is totally on developper-side in the annotated method ; and the method could be used for other things - eg. filling missing values, etc.) - more expressiveness: the approah presented allow for more elaborate validation because at the time of callback, all fields are already set (eg. check of coherency between multiple interdependent fields)
- the restriction to only one instance no-arg-and-no-checked-throw seems reasonable
- the method to call back should not have arguments
- the method to call back should not have checked exceptions (otherwise this would pollute the signature of the
.build()method) - one callback post build is enough, no need for more than one
- only instance method will have access to the value of instance fields
- maintenability of post-processing code:
- if an unchecked exception is raised from the
@PostBuildannotated method, the stack would be "clean" (readable, leads directly to existing - as opposed to generated - code) - validation (the annotated method) is easy to debug (it's a normal method in normal code)
- if an unchecked exception is raised from the
- minimal impact : the approach is totally compatible with current functioning of
@Builder(eg. works if the constructor is generated by lombok or provided by the developper, etc.)
Background
- There is a recurring (but diverse) need for some checks of values of built object, eg. java - How to run code after constructor in a Lombok builder - Stack Overflow or #674 that has a more ambitious scope. or java - Lombok builder to check non null and not empty - Stack Overflow
- the idea is somehow close to the @After idea, but different (here we have a build-lifecycle callback)
- currently, the less "hacky" way to achieve a somehow similar result is to have an explicit all args constructor (which has to be manually updated every time the object fields change); at the end of the constructor do some validation of all fields.
Stupid-proofing To detect invalid usage at compile-time :
- if a
@PostBuildannotated method is not valid (not instance/no-arg/no throw...) : there is a compilation error - if more than one method is
@PostBuildannotated in a@Builderdecorated class : compilation error - if
@PostBuildis used on a non-@Builderdecorated class: compilation error
Source: projectlombok/lombok