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

4

u/rzwitserloot Oct 06 '23

Ron says it's not java. That's different from "it does not conform to spec X".

If Ron said: Lombok as an annotation processor does not conform to the AP spec, I would agree with that statement. But that's not what Ron is saying. What he does say is misleading at best. Wrong is more likely.

Java is a language, anything that conforms to the JLS is Java and anything that does not conform to the JLS is not Java.

The JLS just covers the language itself, it does not cover the standard library. Nowhere in the JLS will you find, for example, any notes whatsoever about java.util.FileOutputStream. Does writing new FileOutputStream() somehow make your code non-java? Surely not. Lombok does not modify the meaning of any java constructs at all, and the code you write even with lombok in your project must be syntactically valid java. If you remove lombok from the compilation process and then try to compile code written with lombok in mind, sure, it won't compile. But then, the exact same thing happens if you try to use, say, Immutables. That's an inherent part of the annotation processor spec: During a compile run, some code can refer to constructs that do not (yet) exist; that's why the AP system runs in rounds, so that round 1 can add the stuff that other code is referring to that doesn't exist yet.

Annotation Processing isn't in the JLS either I think, I think that's a different spec. Regardless, I see only two options:

  • Ron is claiming that Annotation Processors as a whole are 'not java' or 'not comforming to the JLS spec' (in which case, if he wants to say that, sure, lombok is then 'not conforming' either, but that's a bizarre definition nobody I know of would hold).
  • Ron is wrong. Java code with lombok annotations in them is java. As anybody would understand that word. Lombok as a tool 'does not conform to the AP spec'.
  • Any non-comformity of any spec means 'it is not java'. However, that would mean this code: class ThisIsEvidentlyNotJava { public boolean equals(Object other) { return true; }} - is not java either. Because it breaks a spec. Not the JLS, but the contract as stipulated in the equals method's javadoc. I think that's a bizarre definition of what 'not java' is understood to mean. (it breaks a few things from the equals spec, one of them: That a.equals(b) must necessarily mean a.hashCode() == b.hashCode() which would not be true for instances of ThisIsEventlyNotJava).

1

u/koflerdavid Oct 09 '23

If I write a Java source file that refers to its own Lombok-generated builder, then there is no way to make that file compile with a mere annotation processor. One really has to somehow inject the definition into the current file.

With Immutables, the source file is by itself perfectly compilable because the only unresolved references point to another class. This is the true line in the sand.

FileInputStream is a library class, and ThisIsEventlyNotJava is also a library class from the perspective of the current source file.

APs are libraries, and no library is supposed to change the current source file. The code generated by APs mostly behaves like a library that is generated at compile time. Annotations can have huge implications on runtime semantics, but by these are choices made by the application developer who has to specifically enable these semantics by using specific libraries.