|What is the best pattern for plugin code depending on optional dependency at runtime [message #1849915]
||Tue, 08 February 2022 12:44
| Julien HENRY
Registered: July 2009
In the SonarLint plugin, we have the need to customize our plugin behavior depending on what is available at runtime in the user IDE.|
For example, if JDT is there, we will enable Java analyzer. If CDT is there, we will enable C/C++ analysis.
We not only need to know if a third party plugin is there, but we also need to access its APIs (for example, when JDT is there, we want to get the project classpath to configure our Java analyzer).
In the past, we were using different features, and so it was the responsibility of the user to install the appropriate one. It has proved to be error prone, and we had many support cases where people didn't had Java analysis working because they had not installed the appropriate feature.
So what we would like:
* keep a single feature
* declare a compile time dependency on some third party plugins (like jdt, egit, ...)
* this dependency should not be resolved when our plugin is installed (we don't want to have CDT installed in all Java flavors of Eclipse)
* at runtime, if the dependency is there, some extra code should be run
Our current solution is to use optional dependencies in our bundles MANIFEST, but then at runtime we are forced to use reflection (or catch classnotfoundexception). This is a bit ugly.
Is there a way to have a bundle always installed by p2 when our feature is installed, but have this bundle only loaded/activated when a dependency is present? Something a bit similar to execution environment constraint (Require-Capability), but for dependencies?
Say differently, I need an optional dependency at installation time, but mandatory dependency at runtime (= Eclipse should not load our bundle when the dependency is not there).
Powered by FUDForum
. Page generated in 0.02020 seconds