r/java • • Oct 05 '23

How does the lombok magic work underneath?

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

143 comments sorted by

View all comments

Show parent comment

8

u/Cell-i-Zenit Oct 05 '23

Short question:

When i wrote my first annotation processor, i wanted to change an existing class. This was obviously not allowed but i really wonder why.

Is it possible you can give me a short rundown on why this is forbidden?

15

u/pron98 Oct 05 '23

It is forbidden because if you can add code to the current file, then the result would be that with your annotation processor you'll be able to compile files that don't conform to the Java spec.

As a general rule, every file that successfully compiles with an annotation processor must also successfully compile without that annotation processor -- and to the same bytecode -- only perhaps with additional files. Annotation processors can implement pluggable type systems and they can implement various code generators, but they cannot implement things akin to macros, and that's by design.

Macros can be very helpful and very harmful, and we may only add things that are macro-like to Java when we've given that much more thought.

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.

2

u/john16384 Oct 05 '23

If you change an existing class without providing a source file for it, how are IDE's supposed to understand code they can't see? This is exactly lomboks problem, the source contains no getter/setters so IDE's won't be able to do code completion for something that isn't there, or the source before modification doesn't compile at all.

Basically not providing a Java source file breaks every IDE out there, unless you hack those as well (which Lombok does by providing plugins for the currently most popular IDE's, but certainly not all of them).

4

u/barking_dead Dec 15 '23

This is why delombok exists.

2

u/artpar Oct 05 '23

The article posted goes into the depth of exactly how you can do that. Just like how lombok does it.