38

I'm trying to run Dagger 2 as well as Lombok on my Java project. Lombok has to run first, of course, but whether it actually does seems to be up to chance. At first I suspected I could specify the order by the respective position of the library jars in the class path, but that order evidently gets ignored.

Is there a way to specify the order for them to run somehow, or do I simply have to live with not being able to combine two APs ?

I have produced an SSCCE test case.

A simple git clone & mvn compile is enough to demonstrate the issue - if you comment line 18 and uncomment lines 20-21 in App.java, it will compile, even though the Lombok notation in line 18 creates an identical constructor. The problem is that Lombok seems to run after Dagger.

1

3 Answers 3

29

After a lot of research and having talked to one of the Lombok developers, it turns out that because javac does class loading based on hashCode(), the order of annotation processors running in this scenario is essentially random, and what's worse, random between multiple runs. There currently does not seem to be a solution to this issue.

I went with the lombok-maven plugin and delomboking the whole thing, which isn't perfect and somewhat hacky, but at least produces a working result. In hopes that it may aid future googlers coming here, I commited the working version to the repo.

3
  • 1
    I personally dropped Lombok due to these fragilities and try to move to frameworks well supported by Javac, or revert to plain Java. For value objects, I use @AutoValue. I'm also experimenting with Groovy. I'd ideally like to have AspectJ weaving working with annotation processing (such as present in Dagger and AutoValue) but came into bugs.eclipse.org/bugs/show_bug.cgi?id=462782. Apr 19, 2015 at 8:40
  • Has there been any new work around to this problem.. May be kotlin will make life easier but that still means a lot of refactoring in code Jun 28, 2017 at 19:34
  • The Lombok v1.16.12 changelog suggests that work has been done to support/improve running both Lombok and Dagger together, but I have not had a chance to try it out.
    – Torque
    Jun 29, 2017 at 20:46
15

Ideally the order shouldn't matter. Annotation processors should only create files - whenever a file is created, another processing round starts and other processors have the chance again to do their thing with the new file. In this case the order doesn't really matter, so I don't think there is an official way to force an order of processors. The problem is that the Lombok processor manipulates existing files instead of creating new ones, which it is not supposed to do. Some compilers might have an option for ordering processors or use the order in which processors are loaded or appear in the command line arguments, but that will depend on the compiler's implementation.

You could try looking at Daggers and Lombok's build process and see which processors are invoked there. Then explicitly set up those processors in your maven build in the correct order and test different compilers and see if any of them run them in this order.

If necessary, you could split the compilation process and run Lombok with -proc:only first and after that another compilation step without Lombok and without overriding the manipulated files (if that is possible, I never tried that).

3
  • Do you have a source stating that annotation processors are not supposed to manipulate existing files?
    – M. Justin
    Dec 29, 2020 at 21:26
  • 1
    @M.Justin It's mentioned in the Filer documentation: "In general, processors must not knowingly attempt to overwrite existing files that were not generated by some processor"
    – kapex
    Dec 30, 2020 at 22:30
  • Exactly what I was looking for, thanks! "In general, processors must not knowingly attempt to overwrite existing files that were not generated by some processor."
    – M. Justin
    Dec 30, 2020 at 22:31
8

It is possible to specify the order of annotation processors in javac using the -processor flag. However, I didn't get the compile even with this parameter set. I suspect that dagger looks at the source code directly, or that the annotation processor API schedules the annotation processor in the same round and the modifications by lombok are not propagated.

I think the most robust solution now is to use delombok to force the order.

Disclosure: I am a Lombok developer.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.