1

Can I implement Eclipse RCP UI using java code only and not plugin.xml?

2 Answers 2

1

While it might be possible in theory (eclipse plugins are OSGi bundle which are read by the extension registry), I don't think it is practical (unless you re-implement the extension registry lifecycle).

Eclipse Equinox precisely extends the concept of bundles with the concept of extension points, hence the mandatory presence of plugin.xml.

Sign up to request clarification or add additional context in comments.

Comments

0

You can programmatically add and remove extensions. See following example methods (adapt on demand):

public void addExtension() throws UnsupportedEncodingException {
    String pluginXmlAsString = "<a string with the content of plugin.xml";
    InputStream pluginXmlIs = new ByteArrayInputStream(pluginXmlAsString.getBytes(StandardCharsets.UTF_8.name()));

    IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
    Object token = ((ExtensionRegistry) extensionRegistry).getTemporaryUserToken();
    IContributor contributor = ContributorFactoryOSGi.createContributor(Platform.getBundle("org.acme.mybundle"));

    extensionRegistry.addContribution(pluginXmlIs, contributor, false, null, null, token);
}

public static void removeExtensionsContributedByMe() {
    String extensionPointId = "<ID of the extension point for remove an extension of";
    String extensionContributor = "org.acme.mybundle";

    ExtensionRegistry extensionRegistry = (ExtensionRegistry) Platform.getExtensionRegistry();
    IExtensionPoint extensionPoint = extensionRegistry.getExtensionPoint(extensionPointId);
    IExtension[] extensions = extensionPoint.getExtensions();
    Object token = extensionRegistry.getTemporaryUserToken();

    for (IExtension extension : extensions) {
        if (extensionContributor.equals(extension.getContributor().getName())) {
            extensionRegistry.removeExtension(extension, token);
        }
    }
}

We use this for unit tests which add extensions as preparation and remove extension to clean up. This way the tests do not influence each other (which would be the case if the extensions are "hard coded" in plugin.xml or fragment.xml).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.