A solution to this problem exists, in the form of a compiler plugin that is compatible with Lombok and that issues a compile-time error if a builder client might fail to set a mandatory field.
The tool is called the
Called Methods Checker. It is distributed as part of the
Checker Framework and it has
built-in support for Lombok builders. It does not require you to change your coding style and it does not change the code that Lombok emits. It does require some programmer-written annotations -- in experiments, about one per 1600 lines of non-comment, non-blank Java code. The plugin is sound: if it issues no errors, then your code never fails to supply all mandatory fields. Like any static analysis tool, it can issue false positive warnings that you suppress with a
@SuppressWarnings annotation. It works best together with the io.freefair.lombok Gradle plugin.
Full disclosure: I am not the author of the Called Methods Checker, but I did contribute to it. Please give it a try and submit a
bug report if you have any problems.
-Mike