Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » What is the best pattern for plugin code depending on optional dependency at runtime
What is the best pattern for plugin code depending on optional dependency at runtime [message #1849915] Tue, 08 February 2022 12:44 Go to next message
Julien HENRY is currently offline Julien HENRYFriend
Messages: 11
Registered: July 2009
Junior Member
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).
Re: What is the best pattern for plugin code depending on optional dependency at runtime [message #1849924 is a reply to message #1849915] Tue, 08 February 2022 16:24 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 32823
Registered: July 2009
Senior Member
In EMF we have used reflection hell for such things:

But in this plugin I did not want a PDE compile-time dependency. It's horrible though!

In other places we take a simpler approach. E.g, we have this simple guard:

And can then use it like this:

Then you just have to be a bit aware of what exactly causes classes to actually load and to guard such things.

In the end, you can't have a plugin that just silently/conditionally doesn't activate, but you can guard the functionality it provides so that the optional dependencies truly are optional. As is the case for the org.eclipse.emf.ecore bundle which can even run standalone...

Ed Merks
Professional Support:
Previous Topic:Cannot access with DAVE IDE
Next Topic:Missing PDE and JDT Runtime Binary from Eclipse 4.16 to 4.22
Goto Forum:

Current Time: Fri Jun 02 14:15:04 GMT 2023

Powered by FUDForum. Page generated in 0.02020 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top