Search code examples
javaeclipseannotationseclipse-pde

Contributing a Java annotation from an Eclipse plugin


I'm developing an Eclipse plugin that scans and modifies the AST of the currently open Java project.

I want to create a Java annotation that will appear as a known annotation in projects that use the plugin. The annotation's RetentionPolicy will be SOURCE (so it is discarded by the compiler), yet the plugin will be able to identify (using the AST) methods marked with this annotation and handle them accordingly.

For example:

@SkipAnalysis
public void foo() {...}

This annotation will be analyzed by the plugin while traversing the AST, yet it holds no value for the compiler.

How can my plugin contribute annotations to an open project in the workspace?


Solution

  • After some research, it turns out that this is impossible since annotations (and other classes or interfaces) can only be contributed via the build path. Eclipse plugins can change the build path, but cannot contribute their own source code to any project.

    One of the possibilities is to create a library project containing the annotation, and then use the plugin's ability to modify the build path to add that library to the build path. However, this is cumbersome and adds unneeded dependencies to the project, and if the library is not copied to every other developer working on the project, may lead to compilation errors. An exception to that is if the project uses a build automation system (like Maven or Gradle), and the library is stored in a public repository (like Maven Central), assuring that each user that imports the project on every IDE will download that library. Again - possible, but cumbersome.