How does lombok work?
Asked Answered
W

4

193

I met lombok today.
I'm very anxious to know how it works.
A Java Geek Article gives some clues but it's not perfectly clear to me:

Java 6 removes apt and make javac able to manage annotations, streamlining the process to obtain a simpler single step computing. This is the path taken by Lombok.

Maybe with Java 6 the compile process will be: javac -> apt -> lombok apt process -> read class files and add set/get methods using ASM?

Could you show me more details about the mechanism?

Wish answered 24/5, 2011 at 7:41 Comment(1)
Check this article : stackextend.com/java/first-step-lombok-annotationsPeracid
B
178

Lombok does indeed code against internal API, as Sean Patrick Floyd said. However, as lombok is ONLY involved in the compilation phase, its misleading to claim Lombok will only run on a sun VM. It'll only compile on ecj or sun's javac. However, the vast majority of VMs out there, if they ship a compiler at all, are one of those two. For example, the Apple VM ships with stock sun javac, and as such lombok works just fine on macs. Same goes for the soylatte VM, for example.

While for javac we really do have to stick with their updates, partly because of a lot of ongoing work on their compiler right now, we've had to make just 1 minor adjustment to our eclipse support over many many versions of eclipse. So, while we do code against internal API, they are relatively stable bits.

If what lombok does could be done without resorting to internal API, we'd have done something else, but it can't be done, so we resort to internal API usage.

NB: I'm one of the lead developers of lombok, so, I'm probably a little biased :P

Benzine answered 24/5, 2011 at 23:4 Comment(4)
Great to hear from the source (+1). I admit, my statement about running was misleading. I meant Lombok can only run on Sun VMs, but the resulting code is of course platform neutral.Illegible
I sort of wonder if the annotation processor can delegate everything to the eclipse compiler even if it is being run via JavaC that way there is only one processor to call.Alphonso
@Benzine : that's why I really like SO. Genuine answers from the core dev himself.Rudy
Is Lombok still using internal APIs or has this situation changed since 2011? This comment claims that Lombok uses official APIs these days.Tricky
C
117

It uses JSR 269 Pluggable Annotation Processing API available in Java 6.

Note that lombok.jar contains a file named /META-INF/services/javax.annotation.processing.Processor. When javac sees this file in a compilation classpath, it runs annotation processors defined there during compilation.

Coneflower answered 24/5, 2011 at 9:36 Comment(2)
The answer is incomplete.Cephalonia
Thanks! Was looking for unveiling of lombok annotation processing mechanism when it is not defined as annotation processor in build.gradle and found the answer here.Sarcasm
I
71

In addendum to axtavt's answer: Lombok uses a lot more than the JSR 269 api exposes. Lombok codes against a) internal javac apis and b) internal eclipse apis (in a separate processor). JSR 269 does not let you modify existing source code, but when you cast an Element to the underlying AST node, you can actually modify the AST (which is what project Lombok does).

So Lombok is a huge hack that will only compile using javac or eclipse's compiler. It's a great piece of software, but it's also hated by many for being such a non-standard hack.

Illegible answered 24/5, 2011 at 9:47 Comment(3)
@SeanPatrickFloyd Addendum: I've had no problems yet compiling Lombok annotations with OpenJDK 11.Clavicembalo
@Clavicembalo yes, that should work. unless you introduce a second annotation processor, and suddenly run into race conditions because Lombok is changing the AST the other processor expects to find.Illegible
Thank you. I've been looking all over for this explanation. I've written JSR 269 processors to add new classes, but never to modify existing ones. I could not figure out how Lombok pulled this off. Now I know I shouldn't try to take inspiration from their approach.Smallminded
W
58

Project Lombok: Creating Custom Transformations is some helpful.

Wish answered 25/5, 2011 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.