Eclipse片段项目是否有类似于BundleActivator的等效物?

4
我正在构建一个Eclipse插件,提供一组核心功能,并通过片段项目提供可选功能。但我需要在启动时让这些片段向主插件注册自己。
我的片段项目中不能有Bundle-Activator。所以我想知道是否有其他机制可以声明入口点或挂接某个回调函数?
如果没有其他选择,只能将片段项目转换为常规插件项目,那么我需要注意哪些缺点?
以下是我使用的解决方案,基于被接受的答案:
final IExtensionRegistry registry = Platform.getExtensionRegistry();
final IExtensionPoint extensionPoint = registry.getExtensionPoint("myextensionid");
final IExtension[] extensions = extensionPoint.getExtensions();
for (int j = 0; j < extensions.length; ++j)
{
    final IConfigurationElement[] points = extensions[j].getConfigurationElements();
    for (int i = 0; i < points.length; ++i)
    {
        if ("myelementname".equals(points[i].getName()))
        {
            try
            {
                final Object objImpl= points[i].createExecutableExtension("class");
                objImplList.add(provider);
            }
            catch (CoreException e)
            {
            }
        }
    }
}

这个问题应该作为如何使用stackoverflow的示例。谢谢! - Urs Reupke
1个回答

6
您可以定义一个扩展点,通过扩展调用查找您的片段类。
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint extensionPoint = registry
            .getExtensionPoint("myplugin.myextension");
    IConfigurationElement points[] = extensionPoint
            .getConfigurationElements();
    for (IConfigurationElement point : points) {
        if ("myextensionFactory".equals(point.getName())) {
            Object impl = point.createExecutableExtension("class");
            if (impl instanceof IMyExtension) {
                ((IMyExtension) impl).foo();
            }
        }
    }
}

编辑:

要使用这种方法,我必须将我的片段项目转换为插件项目。- bmatthews68

你不应该这样做。例如,在我的测试代码中,我在宿主插件中有以下文件:

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Myplugin Plug-in
Bundle-SymbolicName: myplugin;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: myplugin.Activator
Require-Bundle: org.eclipse.core.runtime
Eclipse-LazyStart: true
Export-Package: myplugin

plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
    <extension-point id="myextension" name="myextension"
        schema="schema/myextension.exsd" />
</plugin>

该片段包含以下文件:

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Myfragment Fragment
Bundle-SymbolicName: myfragment;singleton:=true
Bundle-Version: 1.0.0
Fragment-Host: myplugin;bundle-version="1.0.0"

fragment.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<fragment>
   <extension
         point="myplugin.myextension">
      <myextensionFactory
            class="myfragment.MyExtension1">
      </myextensionFactory>
   </extension>
</fragment>

这些项目是使用 Eclipse 3.3.1.1 生成的。


通过您的回复,我成功地在第一次尝试中使我的插件工作了。感谢您的帮助。 - Brian Matthews

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接