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
View all comments
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.