Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override existing Eclipse plugin extension

I have an existing Eclipse plugin that provides an extension point. The plugin uses standard Eclipse mechanism to find the extensions. In this plugin's code, following code is used to get the extension.

IConfigurationElement[] config = Platform.getExtensionRegistry()
            .getConfigurationElementsFor(extensionPoint);
if (config.length > 0) {
    return config[0];
}

As you can see in the code, only the first found extension is used. This plugin already provides an extension and this extension is used in the default case.

Now I need to override the behavior of the default extension, so I created a new plugin and extends the same extension point. But it turns out that the default extension is always the first one in the IConfigurationElement array, so it's always picked up.

How can I make my own plugin appear first in the found IConfigurationElement array, then my own plugin is used instead of the default one?

The existing plugin is written by others and I don't want to make changes to it until it's absolutely necessary.

like image 704
Alex Cheng Avatar asked Mar 27 '26 11:03

Alex Cheng


1 Answers

I'd say this is a bad way to get extensions from an extension point, either way. If they just want the use the pluginsystem to load a specific extension they have created, they could use the getConfigurationElementsFor(String namespace, String extensionPointName, String extensionId) method instead and close off the possibility for others to use the extension point. As of now there is no sure way of knowing which extension they will get. Chances are, there are instances in the code later on that assumes they will get their extension and when they don't get the extension they expect, Mr ClassCastException comes knocking on the door. (Had a bug like this in a system once)

Of course the best way is to change the code to handle many extensions!

But to your question; I dont know how the ExtensionRegistry fills the array, the API doesnt say. Perhaps there is a way to perhaps set a specific version of your extension that will allow it to be placed first in the array. You would have to look in the code of the ExtensionRegistry to know exactly how the extensions are found. I think it may be in alphabetical order, but im not sure.

Another way is to overload the existing plugin with your plugin and replace functionality. A very dirty approach, but in some cases it is doable. See one of my questions regarding this

like image 83
Fredrik Avatar answered Mar 29 '26 01:03

Fredrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!