Excluding specific field setters from Builder

3,775 views
Skip to first unread message

Kevin Day

unread,
Mar 17, 2016, 6:33:42 PM3/17/16
to Project Lombok
The use case is that I have a class that has 15 or 20 fields on it.  Of those, 2 should be conceptually immutable, even via the builder.  Think about this as the id field in a database - it would be a really bad thing if a user could change that in the object.

So the user shouldn't be able to do this:

myObj.toBuilder().myImmutableField().build();


Is there a way to achieve this?  The idea would be to actually block (or mark private), the builder's setter methods for certain fields...

Thanks!

Martin Grajcar

unread,
Mar 17, 2016, 7:23:24 PM3/17/16
to project...@googlegroups.com
I guess, you can't make them final. When you add a Builder class with private setters for these fields, Lombok won't be able to generate them itself and will skip them. Untested.

--
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.

Reply all
Reply to author
Forward
0 new messages