3

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.

Fu Cheng
  • 3,385
  • 1
  • 21
  • 24

1 Answers1

1

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

Community
  • 1
  • 1
Fredrik
  • 10,626
  • 6
  • 45
  • 81
  • 2
    The ExtensionRegistry returns extensions in a non-determistic fashion. i.e. installing plugins in a different order on a different computer could return the override one first ... but there's no guarantee. It's the extension point consumer's job to sort/prioritize/pick the extension they want. – Paul Webster Oct 28 '11 at 13:08