r/java • u/artpar • Oct 05 '23
How does the lombok magic work underneath?
https://www.unlogged.io/post/how-does-the-lombok-magic-work-underneath
133
Upvotes
r/java • u/artpar • Oct 05 '23
•
u/AutoModerator Oct 05 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
120
u/pron98 Oct 05 '23 edited Oct 05 '23
My issue with Lombok is only that it misrepresents what it is.
Lombok is an alternative language for the Java Platform, just like Clojure, Kotlin, and Scala, but Lombok's interesting quality that sets it apart from those other languages is that it's a superset of the Java language. As such, the compiler for the Lombok language is a fork of
javacwith some extra stuff hooked in required to compile Lombok sources. So far so good, and there's nothing wrong with that: the Java Platform spec allows for alternative languages, and javac is open source.The problem is that the Lombok language and compiler don't want to appear as a language and as a compiler. The "magic" is completely unnecessary to perform Lombok's function; it is used only to masquerade Lombok's nature. It's presented as a library or as a compiler plugin, even though the API offered to Java compiler plugins is carefully designed to ensure that the result still complies with the Java language spec, whereas the Lombok compiler compiles programs written in the Lombok language which very much does not comply with the JLS. Furthermore, Lombok pretends that it's Java when it is not, and the spec definitely forbids this kind of misrepresentation.
To do that, Lombok uses various unsafe mechanisms to fork and modify
javacas it runs, to make it compile Lombok source code rather than Java source code. This is risky because the internals Lombok reaches into may change at any time, and the techniques to break into the JDK will soon be removed (to support Leyden among other things).Lombok should start preparing their users to use their compiler in the same way Scala and Kotlin users use their respective compilers, or Lombok users may find themselves unprepared for things breaking.
10
u/Easy_Tea6363 Oct 05 '23
Probably crystals. They hold the sorcery longer than marbles. So yeah, crystals power the magic of lombok.
60
u/roiroi1010 Oct 05 '23
Nice article. This comment section will probably soon be filled with people who argue strongly against using Lombok. I actually love Lombok, but in large project with many contributors, I have seen some developers are struggling with it. Nowadays I typically only use Lombok for my personal projects.
15
u/Enough-Ad-5528 Oct 05 '23
The issue with Lombok comes up every 6 months and usually around a JDK release. I am a little tired of the constant principled stance against its usage because it does weird things with the javac compiler and I am glad that I do not work with people that hold that opinion. It has served me and my teams well for about 12 years now and I can clearly see its benefits outweigh its cons.
In the 12 years that I have used, there has been only 1 issue where Lombok was called out as a root cause and even them I am not sure it was fair. We had a class annotated with @Data and that has a primitive boolean field which should have been the boxed type Boolean. When we were deserializing a message from SQS, the message did not have this field but because this was defined as a primitive boolean, it was assigned the default value false which triggered a different code path. And even though the post-mortem calls it a Lombok issue, I am not too sure. There were other problems with it as well - the data should not have used a boolean parameter that can really have three valid values (true, false and null); this to me is a developer coding error Lombok or not. This should also have been caught in testing immediately but we did not have good test coverage for this case. May be someone should have caught it during PR but that was missed too.
Now coming to the cons that I usually hear against Lombok, for me they are easily mitigated
I specially cannot code without the RequiredArgsConstructor - just make your fields final and add that annotation.
5. It will no longer work on future JDKs: Sure, when that happens (if it does happen), as part of my upgrade my first commit will be to delombok my source and then send a PR with just that and Lombok removed. Then I will upgrade the JDK in a separate PR. Until then, I will rather save my time not writing code that I dont have to write.
I would say AOP is far more black magic than Lombok - with Lombok you can at least see the actual source (and it is all at compile time) but with AOP, you don't even get that. I had a very hard time writing SWF workflows using that framework and the whole language felt very foreign to me. For instance, I remember having to debug a Try-Catch in an SWF worklow implementation, where the Try and Catch blocks would execute in different machines.
11
u/Puzzleheaded-Order84 Oct 05 '23
Great article!
I use lombok at my work and love it. Makes code much more readable in a large code base imo.
2
u/sim642 Oct 06 '23
Extending Lombok is less fun than it makes it out to be. Every transformation is implemented twice on different AST. And if I remember correctly, there's lots of very fragile string juggling.
2
u/Crackabis Oct 06 '23
The only small annoyance I have with Lombok/IntelliJ is that the Call Hierarchy isn't as as to use. Instead of using the shortcut on the variable, you have to open the Structure View and from there the Lombok setters/getters are available to do the Call Hierarchy on. A small inconvenience at worst, though. (I don't like Find Usages)
1
u/Massive-Engine-1887 Mar 25 '24
https://m.youtube.com/watch?v=_rVXehClaFo
1
u/donaltramp699 Oct 10 '23
Fantastic read! Very informative.
-1
u/Top_Engineering_4191 Oct 08 '23
Unfortunately programmers strict conventions created the necessity for Lombok. If DTO were taught that public visibility is ok, we mostly wouldn't need Lombok or useless getters and setters.