Javaparser: Is there a way to use this tool together with Lombok?

Created on 9 Jun 2019  路  3Comments  路  Source: javaparser/javaparser

I'm sorry if this is already answered somewhere (this sounds like a common problem people would face using the parsing feature with existing codebases), but I couldn't find anything out there.

I have a codebase that uses Lombok features like @Data. If I give this codebase to JavaParser as a source root, it obviously isn't going to find elements yet to be generated by Lombok (e.g. getters). My question is if there is a way to work around this limitation, i.e. some way to hook annotation processors before the AST generation takes place. How would you approach such situations? Should I just give up given the architecture of both tools?

Question (JP usage)

Most helpful comment

Delombok worked for me. Not the cleaniest solution (there's no API so a little bit of reflection was necessary here and there) but it gets the job done, i.e. I was able to delombok a very complex and large code base with multiple modules and send it to Java Parser immediately after, without the issues I was facing before. Posting here so it might help others in the future.

All 3 comments

I've looked into Lombok a while ago, and the problem with the approach you mention above is that Lombok is using and modifying an AST Object "owned" by the Javac parser/IDE whilst intercepting annotation processors.
How Lombok works

to understand the distinction

  • With JavaParser, we start with txt code and return an AST is created from the text
  • With Lumbok, the Javac compiler calls rounds of Annotation processing and hands a reference to an Object representing the AST (created by javac) to Lumbok for manipulation

It MAY be possible to just ensure that some code could run AFTER Lombok does its processing (i.e. in a subsequent "annotation processing round")... however, to my knowledge, the AST (that javac built and was manipulated by Lombok) is not in a form that can be "read" by JavaParser (JavaParser accepts all manner of Strings and Streams, but not an Object/AST reference)

Basically the only way to manipulate the code (the lombok way) is to make API calls through these objects, that then get compiled into bytecode after annotation processing is completed, and the JavaParser AST is not compatible with the Lombok AST.

Might want to look into this (in depth discussion of building into Lombok)
Writing your own Lumbok Annotation

Delombok worked for me. Not the cleaniest solution (there's no API so a little bit of reflection was necessary here and there) but it gets the job done, i.e. I was able to delombok a very complex and large code base with multiple modules and send it to Java Parser immediately after, without the issues I was facing before. Posting here so it might help others in the future.

Was this page helpful?
0 / 5 - 0 ratings