Provide custom java annotations with plugin? Follow
Answered
Hi,
is it possible to provide custom java annotations along with a plugin which analyzes the annotated java source code? The scenario could be as follows:
- User installs the plugin XYZ which allows the use of annotation @ABC
- User annotates elements of his/her java source code with @ABC
- The plugin allows certain methods of analysis, quick fixes, syntax HL, source code transformations, etc. based on the annotated code
Is this possible?
How to provide the annotations?
How to import those annotations into users java project
Best regards
Kai
Please sign in to leave a comment.
The plugin can assist or add automatically required library containing the annotations. Implement com.intellij.codeInsight.daemon.quickFix.ExternalLibraryResolver that will suggest adding repository library when certain classes/packages are being used in code.
See com.intellij.jarRepository.RepositoryAddLibraryAction#addLibraryToModule for explicit adding repository library.
Hi Yann,
thank you very much for your reply but I need some further advice on this. There are 3 players in this game:
1. The annotations classes
2. My plugin analysing code using 1. (the plugin depends on 1., too)
3. Some 3rd party code using 1. and being analysed by 2.
It's still not clear to me how to provide my annotations (1.) to my plugin (2.) AND to the code importing the annotations (3.). Do I have to pack an annotations jar (artifact)? Is it possible to create a separate plugin only carrying the annotation classes and add an dependency to my main plugin?
And further on: When someone imports my annotations (3. -> import ...) how do I provide a quick fix or something else to help adding the missing dependency/jar/... to the project/module?
Maybe I should admit that I'm still at the beginning of plugin development and need a helping hand to guide me.
Thx in advance
Kai
You can pack the annotations into your plugin (if the plugin code has a runtime/compilation dependency on them, otherwise they would be available via the project).
ExternalLibraryResolver will just do that - suggest to import the "pre-defined" library for the annotations into the current user project.
Hi Yann, thx again. I've tried ExternalLibraryResolver right after your 1st reply. ExternalLibraryResolver requires groupId and artifactId to identfy the library. My annotations are in a jar in the libs folder of my plugin without any Id. There's a "Add 'xyz-annotation' to classpath'" popup (As I used 'xyz-annotation' as artifactId to give it a try) and downloading from maven repository will fail as expected.
Well then your fix should copy the annotation.jar file from plugin resources/lib folder into project location specified by user and then add it as library to current module/project. See http://www.jetbrains.org/intellij/sdk/docs/reference_guide/project_model/library.html and http://www.jetbrains.org/intellij/sdk/docs/reference_guide/project_model/module.html
Hi Yann. I am jumping in on this thread since I havea simmilar issue. I have a plugin that makes use of some annotations defined by me which are to be used by clients of the plugin to annotate some code. Here are the steps I did:
My question is: shouldnt the annotations be available at runtime to the plugin via the dependencies declared in the test project I created?
If not then what should the approach be here? Make sure to pack the annotation dependencies inside the plugin jar? Not even sure how to do that in gradle, but I guess I will figure it out if indeed that is what is required.
Hello Dragos Barosan, sorry this got lost. Is this still an issue? Could you please share a link to your project to reproduce?