How Does Lombok Work

How does Lombok.val actually work?

In order for Lombok to work, the source code needs to parse without errors. That's why, as you already mentioned, @val foo = new ArrayList<String>(); wouldn't work.

Although Lombok uses annotations, and an annotation processor, the annotation processor is only used as a means to get involved by the compiler.

Lombok does not have a registered processor for @val. Instead, it processes all java files, visits the whole AST and replaces val by the type of the initialization expression of the local variables.

For the actual replacement, for Eclipse/ecj see this class and this one. For javac, see this class.

Disclosure: I am a Lombok developer.

Is it safe to use Project Lombok?

It sounds like you've already decided that Project Lombok gives you significant technical advantages for your proposed new project. (To be clear from the start, I have no particular views on Project Lombok, one way or the other.)

Before you use Project Lombok (or any other game-changing technology) in some project (open source or other wise), you need to make sure that the project stake holders agree to this. This includes the developers, and any important users (e.g. formal or informal sponsors).

You mention these potential issues:

Flamewars will erupt in the ##Java Freenode channel when I mention it,

Easy. Ignore / don't participate in the flamewars, or simply refrain from mentioning Lombok.

providing code snippets will confuse possible helpers,

If the project strategy is to use Lombok, then the possible helpers will need to get used to it.

people will complain about missing JavaDoc,

That is their problem. Nobody in their right mind tries to rigidly apply their organization's source code / documentation rules to third-party open source software. The project team should be free to set project source code / documentation standards that are appropriate to the technology being used.

(FOLLOWUP - The Lombok developers recognize that not generating javadoc comments for synthesized getter and setter methods is an issue. If this is a major problem for your project(s), then one alternative is to create and submit a Lombok patch to address this.)

and future commiters might just remove it all anyway.

That's not on! If the agreed project strategy is to use Lombok, then commiters who gratuitously de-Lombok the code should be chastised, and if necessary have their commit rights withdrawn.

Of course, this assumes that you've got buy-in from the stakeholders ... including the developers. And it assumes that you are prepared to argue your cause, and appropriately handle the inevitable dissenting voices.

Related Topics

Leave a reply