【发布时间】:2011-01-19 05:06:41
【问题描述】:
我能否仅使用 java 代码而不是 plugin.xml 来实现 Eclipse RCP UI?
【问题讨论】:
标签: eclipse-plugin eclipse-rcp
我能否仅使用 java 代码而不是 plugin.xml 来实现 Eclipse RCP UI?
【问题讨论】:
标签: eclipse-plugin eclipse-rcp
虽然这在理论上是可能的(eclipse plugins 是 OSGi bundle 是 read by the extension registry),但我认为它不实用(除非您重新实现扩展注册表生命周期)。
Eclipse Equinox 精确地用extension points 的概念扩展了bundle 的概念,因此plugin.xml 的强制存在。
【讨论】:
您可以以编程方式添加和删除扩展。请参阅以下示例方法(按需调整):
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);
}
}
}
我们将其用于单元测试,添加扩展作为准备,删除扩展进行清理。这样测试就不会相互影响(如果扩展在 plugin.xml 或 fragment.xml 中“硬编码”就会出现这种情况)。
【讨论】: