Search code examples
javanetbeanslombok

How to make custom validations in Project Lombok setters


I have a project(ID3 tagging) that contains dozens of setters and getters. I'm investigating Project Lombok to see how it can help me.

Some of the fields that can be set have very strict requirements that could include character encoding, bit-setting, length checks, character ranges and, so on. I could use a builder patter but, there may be cases where the pattern isn't necessary. What if I want to use validation methods and/or classes rather than annotation? How can I access Lombok's setters in my Netbeans project to add these validations?


Solution

  • The short answer is that you can't.

    The only consistency check supported now is @Nonnull. If you want anything else, you have to write your setters manually. Obviously, no tool can do all the check you listed and describing them via annotations would be cumbersome at best.

    Sometimes, this feature request could help. It might be too course grained for your needs or not. Quite often, all you need is to write some setters manually. This feature is more useful with immutable objects as it will provide the only way to add validation to generated constructors.