Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project Lombok - Value annotation: is it possible to suppress @Getter?

Tags:

java

lombok

After scouring the Lombok feature-list and in particular the documentation for the Getter/Setter and @Value annotations I have not been able to find any setting that suppresses the code generated by @Getter.

In practice, @Value is shorthand for: final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter

This is important as I do not want to leak references to objects that are themselves mutable.

Effective Java references this type of issue in "Item 39: Make defensive copies when needed". It seems that @Wither could partly solve this issue by making actual defensive copies but I want to avoid leaking attributes what so ever, regardless of them being mutable.

While it is possible to roll one's own @Value annotation that omits the @Getter I would, of course, prefer not to as that would add unwarranted complexity to the codebase if such a setting already exists.

like image 851
Filip Allberg Avatar asked Jan 04 '23 21:01

Filip Allberg


1 Answers

You could use: @Value @Getter(AccessLevel.NONE)

AccessLevel.NONE instructs Lombok to not generate the getters. That's the best you can do right now.

like image 183
Roel Spilker Avatar answered Jan 07 '23 11:01

Roel Spilker