Provide custom java annotations with plugin?

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:

  1. User installs the plugin XYZ which allows the use of annotation @ABC
  2. User annotates elements of his/her java source code with @ABC
  3. 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

7 comments
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

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.

0
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

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:

  • I extracted the annotations into a separate project and uploaded the resulting har in Maven Central.
  • I added as an implementation dependency in the plugin project the annotations. This results in everything compiling but the resulting jar for the plugin does not contain any annotation classes.
  • I implemented ExternalLibraryResolver to have a quickfix for the annotations dependency
  • I then openend a separate test project where I installed the plugin and also I added in my build.gradle as a implementation dependency the annotations required by the plugin.
  • Yet when the plugin runs it throws a NoClassDefFound error for the annotations.

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.

 

1
Comment actions Permalink

Hello Dragos Barosan, sorry this got lost. Is this still an issue? Could you please share a link to your project to reproduce?

0

Please sign in to leave a comment.