r/java • • Oct 05 '23

How does the lombok magic work underneath?

https://www.unlogged.io/post/how-does-the-lombok-magic-work-underneath
133 Upvotes

143 comments sorted by

View all comments

Show parent comment

7

u/Practical_Cattle_933 Oct 05 '23

I’m not pron, but basically self-modifying code is a big no-no for a very good reason. It’s hard enough to reason about static code, let alone if it changes. While new classes can still change the behavior of code, this is done in an expected way (if you have a non-final class, you can expect someone else to subclass it - only that is possible with annotation processors as well) - so even the usage of multiple, complex annotation processors in a project may still be feasible.

You can actually see this issue with lombok, even though it only does trivial modifications: an “old”, but infamous mistake many junior engineers make is using lombok carelessly with JPA entities — calling a toString() in a debug print might just make a db connect/throw an exception, because that toString calls a getter now under the hood.

2

u/rzwitserloot Oct 05 '23

The exact same problem (whoops, I tossed a casual annotation out there and now my DB code goes nuts) can also occur in reflective based JPA implementations, and can also occur with non-"edit existing files" annotation processors such as the Immutables project. I don't think "the problem with APs being able to edit existing source files vs. only allowed to make new ones" is that this is somehow 'self modifying code'.

1

u/koflerdavid Oct 09 '23

That's really just a criticism about these project's API design and because these are annotations with runtime effects. While they are undoubtedly powerful and useful, many programmers have become wary about the pitfalls of Hibernate and Spring Data, and how large a difference a stray annotation can make. In the case of Immutables, an error usually results in a compilation failure.