Generating source files from plugin

Hi,

I need to generate some source files so I'm hooking into PsiAugmentProvider. The generated files need to be in a different folder hierarchy. I'm thinking I'll use PackageUtil.findOrCreateDirectoryForPackage() to first create the directories for the package and then do JavaDirectoryService.getInstance().createClass() to generate the source file. Trouble is I can't get a handle for the current module which findOrCreateDirectoryForPackage() needs. How do I get that?

Also, I'm not very sure how to go about this. Does this approach look right?

Thanks,
Akula.

5 comments
Comment actions Permalink

Please try re-reading the previous thread again. Using the PsiAugmentProvider does _not_ involve generating any source files in any directory.

0
Comment actions Permalink

Well then, what is a good way of generating source files?

0
Comment actions Permalink

Maybe you could start with describing the task you're trying to accomplish in more detail?

0
Comment actions Permalink

I'm writing a library that's got some annotation processors which generate source files based on annotations. Much like lombok, but unlike lombok it doesn't modify existing code rather generates additional source files (in plain java) which'll need to be compiled.

I understand that intellij doesn't do edit-time compile, because of which any code accessing the generated source content will be marked as error (even though it compiles and runs fine). This is something I'd like to prevent, at the very least writing a plugin.

And am I correct in assuming I'll need to return my generated class as a PsiClass from the PsiAugmentProvider in order to make it available to intellij's analysis engine? Going ahead with this assumption, I realized that I need a PsiDirectory to create a PsiClass. I'd also like to have my generated source files inside my target path so they'll be cleaned before compilation. I couldn't figure a way to get a PsiDirectory handle for such a case. Or maybe this approach I'm taking is completely wrong in which case, how do I do it?

0
Comment actions Permalink

Actually if your code generator generates complete classes, then PsiAugmentProvider is not the correct API to use, sorry for misleading you. (PsiAugmentProvider is used for adding methods and fields to existing classes.)

Instead, you need to provide an implementation of the PsiElementFinder extension point, and to implement findClasses() and related methods.

However, you still don't need to generate anything on disk. Instead, you can create the PsiClass from text using the PsiJavaParserFacade.createClassFromText() method and return that class from your PsiElementFinder implementation.

The actual generation of source files on disk will happen during build time - IntelliJ IDEA will call your annotation processor during its build process normally,

0

Please sign in to leave a comment.