是否有与Eclipse片段项目相同的BundleActivator?

Bri*_*ews 4 java eclipse eclipse-plugin eclipse-fragment

我正在构建一个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)
            {
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

McD*_*ell 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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

要使用这种方法,我必须将我的片段项目转换为插件项目. - 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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

该片段包含以下文件:

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"
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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