Uninstall plugin programmatically before startup of Eclipse
<p>I have RCP based product. And I want to uninstall some eclipse plugins on start up of RCP on checking some license file and depending on license type.</p>

<p>I have achieved that through subclass of <code>WorkbenchAdvisor</code> and overriding method.</p>

<p>Below is the my <code>WorkbenchAdvisor</code> class. </p>

<pre class="lang-java prettyprint-override"><code>public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

  boolean isLicenseAvailable = true;

  /**
   * {@inheritDoc}
   */
  @Override
  public void preStartup() {
    LicenseManager.getInstance();
  }

  private static final String PERSPECTIVE_ID = "com.example.perspective"; //$NON-NLS-1$

  @Override
  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(final IWorkbenchWindowConfigurer configurer) {
    return new ApplicationWorkbenchWindowAdvisor(configurer);
  }

  @Override
  public String getInitialWindowPerspectiveId() {
    return PERSPECTIVE_ID;
  }
}
</code></pre>

<p>Then Below is the snippet which uninstalls the plugins in <code>LicenseManager</code> class depending on license type.</p>

<pre class="lang-java prettyprint-override"><code>  Bundle plugin = Platform.getBundle("com.example.test");
Bundle bundle = plugin.getBundleContext().getBundle(0);
FrameworkWiring adapt = bundle.adapt(FrameworkWiring.class);
try {
  if (plugin != null) {
    ExtensionRegistry registry = (ExtensionRegistry) Platform.getExtensionRegistry();
    Field privateStringField = ExtensionRegistry.class.getDeclaredField("masterToken"); //$NON-NLS-1$
    privateStringField.setAccessible(true);
    Object masterToken = privateStringField.get(registry);

    IContributor contributor = ContributorFactoryOSGi.createContributor(plugin);
    IExtension[] points = registry.getExtensions(contributor);
    for (IExtension point : points) {
      point.getExtensionPointUniqueIdentifier();
      Platform.getExtensionRegistry().removeExtension(point, masterToken);
    }
    adapt.refreshBundles(adapt.getRemovalPendingBundles());
    plugin.uninstall();
    Thread.sleep(5000);
  }
}
catch (BundleException | SecurityException | IllegalArgumentException e) {
  e.printStackTrace();
}
catch (NoSuchFieldException | IllegalAccessException w) {
  w.printStackTrace();
}
catch (InterruptedException e) {
  e.printStackTrace();
}
super.preStartup();
</code></pre>

<p>Now the plugins are getting uninstalled and menu contributions are disappearing but the not all of them. If I have a menu "Test" and under it two commands "c1 and C2". c1 and c2 disappears but not the menu "Test".</p>

<p>And also I'm getting  below exception each time I click on that menu. And the pop up menus are also going for toss. None of the popup menus under navigator are coming. There also I get the same exception.</p>

<pre>
org.eclipse.e4.core.di.InjectionException: org.eclipse.core.runtime.InvalidRegistryObjectException: Invalid registry object
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:68)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:252)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:234)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
    at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.setEnabled(HandlerServiceHandler.java:80)
    at org.eclipse.core.commands.Command.setEnabled(Command.java:875)


</pre>