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

-8

u/rzwitserloot Oct 05 '23

Lombok is no more a different language than any java project with an annotation processor that generates java code is.

19

u/peripateticman2023 Oct 05 '23

Did you miss the part about modifying javac on the fly?

21

u/rzwitserloot Oct 05 '23

We do not modify javac on the fly. Our plugins and such add an --add-opens line. And we modify the AST. That's modify, whereas plain jane annotation processors only make new ASTs, they don't modify existing ones.

Point is, we modify them as an AST, we don't create new nodes. We don't invent new language constructs that java does not have.

8

u/SirYwell Oct 05 '23

whereas plain jane annotation processors only make new ASTs, they don't modify existing ones.

No. Annotation processors might create files via the Filer API, but they don't make new ASTs.

6

u/rzwitserloot Oct 06 '23

java source files. Which are textual representations of ASTs.

3

u/krzyk Oct 05 '23

Well, how about val? Sounds like new language construct. And SneakyThrows, Value?

10

u/rzwitserloot Oct 06 '23

val

is just short for final var. That's not a new construct.

SneakyThrows

See this same comment thread, someone else mentioned it. It's not - you'd think so, but you can make plain jane javac without any modification or third party deps or APs throw sneakily.

@Value

Just generates a boatload of boilerplate, stated in plain java (as in, lombok generates java code, it doesn't generate class files. That's e.g. a difference between lombok and for example kotlin or any other 'JVM-based language'. They turn kotlin source files into class files. Lombok turns java code into more verbose java code, but still java code.

The java code that lombok produces, if you were to write that out manually and compile it, the class file you would get when you pass that source file to javac is bit for bit identical to what the class file emitted from lombok would provide. The API you end up with is identical to one you would get if you use vanilla Annotation Processors like immutables. Lombok just makes it more convenient - no need for separate class files, no need for a full compile run before you see your API appear in your project.

5

u/krzyk Oct 06 '23

So, how is it different from kotlin, Scala , etc.? You create language based on Java, that is close to Java but slightly different, like groovy or maybe J#?

6

u/agentoutlier Oct 07 '23

Because it only adds to the AST. It’s like a macro language for a language that doesn’t officially have one.

Scala and others have a completely different AST that gets compiled to bytecode.

2

u/krzyk Oct 07 '23

Ok, and Groovy? It has very similar language, AFAIR every java program is also a working groovy one.

6

u/agentoutlier Oct 07 '23 edited Oct 07 '23

It is a matter of which perspective you want to look at it at.

You asked how it is different: Groovy does not alter Java AST. Groovy is not a compiler plugin. Groovy might add constructs that are not possible in Java language (this I'm not sure of but Scala and Kotlin for sure do).

This back and forth of whether or not Lombok is a different language is really stupid IMO because pron and /u/rzwitserloot can argue one way or the other depending on what level of meta we want to go into.

Lombok is a like an unofficial preprocessor.

/u/rzwitserloot argument is that every annotation processor is like an unofficial preprocessor and thus on the same grounds is at a very abstract level kind of true but mostly not. Their argument is that code that depends on generated annotated code will not work in the same way lombok will not work.

However regular APT libraries do not require special IDE plugins. Lombok does so I say what it does is rather unofficial and not canonical regardless of framing of whether it is a new language or not.

1

u/wildjokers Oct 06 '23

We don't invent new language constructs that java does not have.

@SneakyThrows?

→ More replies (2)
→ More replies (1)

28

u/pron98 Oct 05 '23 edited Oct 05 '23

It is different in the most basic sense: Annotation processors conform to the JLS, hence they're Java (they are very carefully designed to do that, and that's why they can only generate code in different files); Lombok does not even remotely conform to the JLS, hence it's a different language, like Clojure, even though it's a language that's more similar to Java than Clojure is.

Maybe Lombok is great — it's an interesting concept for a language and it has a cleverly-constructed compiler — but it's not Java so you shouldn't say it is; it is not an annotation processor, so you shouldn't say it is; it is not a plugin so you shouldn't say it is.

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'.

→ More replies (1)

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.

11

u/john16384 Oct 05 '23

Why does it require an IDE plug-in then? If it was Java, a Java IDE would understand it. As it is, any new Java IDE would have to add a Lombok plugin to support these "Java" source files.

5

u/rzwitserloot Oct 06 '23

In eclipse? Because eclipse's annotation processing stuff isn't all that great. Not really its fault, AP's APIs aren't quite entirely suitable to an on-the-fly-recompile-as-you-save concept. We can do better, and make your code just act like you wrote out all that boilerplate continuously and instantly, no need to save (which is a requirement if you want to use eclipse's AP).

For netbeans? We don't have a plugin. For intellij? You'd have to ask the plugin maintainer, but as I understand it, intellij uses its own compilation system, which doesn't make full class files (it uses javac for that, and thus no plugins would be needed whatsoever), but it does pick up all signatures e.g. for code completion and on-the-fly error reporting. And there it's a similar story to eclipse: The experience is suboptimal, with the plugin it's all a lot nicer.

You can write a plugin that makes e.g. Immutables or some other vanilla AP project run smoother too. If such a plugin existed I'd strongly recommend you use it.

7

u/cal-cheese Oct 05 '23

I don't get what is hard to understand regarding the non-conformance of Lombok. Java is a language, anything that conforms to the JLS is Java and anything that does not conform to the JLS is not Java. It seems you are trying to create a straw definition of Java and argue the conformance based on that definition, which is not only fallacious but also really misleading.

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).
→ More replies (1)