Annotation processing through a plugin

Hi,
I'm writing a library that does some code generation using an annotation processor. Unfortunately, IntelliJ doesn't run annotation processors on edit, so I want to write a plugin that will do the same. All I need is a hook in to a file-save event from where I can process annotations in the file and do the necessary code generation. What's a good way to do this?

I had a look at registering a BulkFileListener. I get the file-save events all right, but I can only access the plain text of the file. I was looking for something where I can get access to the syntax tree of the source file so that I can scan for annotations, methods... etc.

I wonder if there's extension point I can hook into, but I can't find any documentation around it. All I got was an xml with all the extension points available, but no information.

Any help appreciated.

Thanks,
Akula.

5 comments
Comment actions Permalink

If you don't actually want the code generated by your annotation processor to be included in the source code the user is editing, then the approach you're considering is wrong. Instead you should use the PsiAugmentProvider API to make the declarations that would be generated at build time visible to IntelliJ IDEA's code analysis engine. Look at the source of the Lombok plugin to see how it can be used.

0
Comment actions Permalink

I couldn't find any documentation for PsiAugmentProvider (or for extensions for that matter, save this, which I thought was a bit brief).

I presume it is some kind of a callback. Do you happen to know when it is called? Also, will I be able to generate source files and make them available for compilation this way?

0
Comment actions Permalink

There is no documentation for PsiAugmentProvider. You can refer to the source code of IntelliJ IDEA Community Edition to see when and how it's called.

As I said, using PsiAugmentProvider does not involve generating source code. Code generation happens only at build time.

0
Comment actions Permalink

Code generation happens only at build time.


Could you please elaborate on that?

0
Comment actions Permalink

IntelliJ IDEA's code analysis engine, unlike that of the other IDEs, does not rely on compiled class files. Because of that, there is no reason to generate source or class files while the code is being edited; information about elements that would be generated can be directly and dynamically reported to IntelliJ IDEA's code analysis engine. That's what PsiAugmentProvider is for.

0

Please sign in to leave a comment.