Intellij support for compile time generated classes.

Answered

Hello!

I am developing a little library, which generates some factory classes at compile time. For example, if we annotate class Example, ExampleFactory will be generated. But, Intellij see such classes only after compilation. I want to implement plugin to support this behavior, so that, when users annotate their classes, they can see generated factory classes (not exist yet, but already visible) in any place, when they want to use it.

I looked at documentation, but didn't figure out what to do. Can you help me? How can I do it?

3 comments
Comment actions Permalink

I assume these classes will be generated and put into "generated sources" folder after compilation? Wouldn't this interfere with trying to "mock" them?

0
Comment actions Permalink

I assume these classes will be generated and put into "generated sources" folder after compilation?

Yes, you are right. But user has to run `mvn compile`, for example, to generate those classes. It's not convenient.

Lombok is good example. You put annotations on classes and intellij knows that some methods will be generated in the future. I looked at lombok plugin source code. It uses AugmentProvider to do that. I implemented test provider https://gist.github.com/KhadanovichSergey/4b6849eac4f4d96964dd0c5ebd2ae29b  . And it works :) but, only if class is opened in editor. For example `Payment` class is opened in current editor tab, and if you start typing `Payment`, `PaymentFactory` is visible. But if you do the same thing from other class, `PaymentFactory` is not visible.

and

May be I do something wrong.

Wouldn't this interfere with trying to "mock" them

If I understand correctly, yes. These classes will be generated later, during `clean compile`, for example.

0
Comment actions Permalink

So your AugmentProvider should probably automatically disable itself once it discovers the generated classes are present.

IMHO the easiest solution would be to simply show banner in editor if you discover usage of your annotation(s) and suggest user to build/generate missing classes.

0

Please sign in to leave a comment.