How does Lombok generate java source files

1,949 views
Skip to first unread message

dodtsair

unread,
Sep 10, 2013, 12:59:22 PM9/10/13
to project...@googlegroups.com
How does Lombok generate java source files?  I would assume it has some utility api to make such a task easy. 

From my brief reading APT provides a writer and the rest is up to the caller.  I would assume that Lombok doesn't just start call a bunch of println statements with java fragments inside strings. 

Roel Spilker

unread,
Sep 10, 2013, 2:06:53 PM9/10/13
to project...@googlegroups.com

We don't actually use APT. We only use it in javac as a way in. We "just" modify the AST, creating java objects to represent the code. The AST is directly compiled into bytecode. Or, when using delombok, pretty printed to java source code.

On Sep 10, 2013 6:59 PM, "dodtsair" <mpower...@alumni.calpoly.edu> wrote:
How does Lombok generate java source files?  I would assume it has some utility api to make such a task easy. 

From my brief reading APT provides a writer and the rest is up to the caller.  I would assume that Lombok doesn't just start call a bunch of println statements with java fragments inside strings. 

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reinier Zwitserloot

unread,
Sep 10, 2013, 2:12:16 PM9/10/13
to project-lombok
Lombok does not generate source files at all. Annotation processors can generate source files, but lombok is not an annotation processor. We happen to use the mechanism of annotation processors to 'inject' ourselves into the java compiler when using javac, but for example lombok's eclipse implementation has nothing at all to do with annotation processing.

More to the point, perhaps, while we probably can generate entire sourcefiles (it's trivial in the javac implementation as we're already an annotation processor there!), there are zero lombok features that need to do this. Even our @Builder implementation just adds an inner class to an already existing one; we do NOT make an entirely new file.

Now, the thing is, the annotation processor API __ONLY__ lets you make entirely new ones. You can't even replace existing ones. Lombok does something quite different: It makes modifications to existing ones. But, we don't operate at the 'raw characters in a big buffer' level. We operate at the so-called AST and LST level. (An AST is source code, parsed into a tree. Once you start building symbol tables so that you can tell that for example the 'String' in 'String x = "Hello;"' must be referring to java.lang.String, and adding the ability to list all methods in java.lang.String so you can check if a call on x is in fact a valid reference, it's become an LST). Mostly at the AST level, because creating trees and such is very expensive and you get into a chicken and egg issue rather quickly.

So, that's how lombok works; We take the AST and modify it directly. We create an instance of JCMethodDecl for example, and add it to an existing JCClassDecl instance's 'defs' variable (which is a list, of members of the class declaration). The JCMethodDecl instance has a modifiers field, which we fill (with for example the flag 'public', and we fill the field that lists annotations on it with @SuppressWarnings, which is itself another instance of the JCAnnotation class, and so on and so forth).

Unfortunately, eclipse uses an entirely different compiler with an entirely different ast model, so that's unfortunately a lot of code duplication.

It would have been awesome if java shipped with an official, open source implementation of an AST which all compilers and tools and code checkers and such all use, but that's not the case, and javac has some hairy copyright issues (it's not LGPL), so everyone is making their own. We're sort of working on this (the lombok.ast project), but it's still work in progress. At any rate, that is how lombok works.


 --Reinier Zwitserloot


On Tue, Sep 10, 2013 at 6:59 PM, dodtsair <mpower...@alumni.calpoly.edu> wrote:
How does Lombok generate java source files?  I would assume it has some utility api to make such a task easy. 

From my brief reading APT provides a writer and the rest is up to the caller.  I would assume that Lombok doesn't just start call a bunch of println statements with java fragments inside strings. 

--
Reply all
Reply to author
Forward
0 new messages