3

我有一个 Eclipse 插件,我的目标是 3.1 或 3.2 作为支持的最低版本。问题是我的一些代码仅适用于 3.5 及更高版本(请参阅我的另一个问题:在 Eclipse 中是否有替代 CaretListener 的方法?)。

既然我有在旧版本中工作的代码和在新版本中工作的不同代码,有没有一种方法可以让我只有在我的插件在 3.5 或更高版本中运行然后恢复到旧代码时才能调用新代码如果运行更旧的东西?

作为测试,我创建了两个具有相同类的插件(只是做的事情略有不同)。我在一个插件中将org.eclipse.ui依赖项标记为至少 3.5,在另一个插件中标记为至少 3.1,但我无法在旧版本中忽略依赖 3.5 的那个...

任何人都可以帮忙吗?

谢谢,艾伦

4

2 回答 2

2

您可以使用org.eclipse.core.runtime.Platform获取org.eclipse.uiBundle 并检查版本。

Version ui = Platform.getBundle("org.eclipse.ui").getVersion();
// then do something with that

如果 >=3.5 则注册MyListenerOldMyListener否则。

编辑:

对,以上内容仅适用于捕获运行时行为的差异。

Eclipse 支持一些只加载某些类的技巧。

从开发的角度来看,最简单的是@ShiDoiSi 提到的技巧。

Bundle myBundle = org.osgi.framework.FrameworkUtil.getBundle(this.class);
Version ui = Platform.getBundle("org.eclipse.ui").getVersion();
Version cutOff = new Version(3,5,0);
final Executable processListener;
if (ui.compareTo(cutOff)<0) {
    Class pc = myBundle.loadClass("my.pkg.OldListenerProcess");
    processListener = (Executable) pc.newInstance();
} else {
    Class pc = myBundle.loadClass("my.pkg.ListenerProcess");
    processListener = (Executable) pc.newInstance();
}
processListener.execute(targetObject);

另一个使用更多 Eclipse 框架的选项是定义您自己的扩展点,以便其他包的贡献可以决定使用哪个版本。基本上它与上面的模式相同,除了版本检查是由贡献Executable运行的插件上的依赖范围完成的。旧方式依赖 org.eclipse.ui [0.0.0,3.5.0) ,而当前方式只需指定 org.eclipse.ui 3.5.0 (这是 3.5.0 上的开放范围)。然后你可以阅读你的扩展并实例化提供的类。

如果您为此创建额外的插件(这两个差异有点重),您可以在主插件中定义一个命令,并让额外的插件提供等效的处理程序。插件仍然必须具有依赖范围,以便在 <3.5 或 >=3.5 的情况下只加载一个。然后使用命令 API,您可以执行命令(并且正确的处理程序将运行)。

ICommandService cmdS 
  = (ICommandService) workbenchWindow.getService(ICommandService.class);
Command process = cmdS.getCommand("my.pkg.ListenerProcess");
ParameterizedCommand cmd = new ParameterizedCommand(process, null);
IHandlerService handlerS 
  = (IHandlerService) workbenchWindow.getService(IHandlerService.class);
IEvaluationContext ctx = handlerS.createContextSnapshot(false);
ctx.addVariable("toAddListener", targetObject);
handlerS.executeCommandInContext(cmd, null, ctx);

然后您的处理程序实现将用于HandlerUtil.getVariable(event, "toAddListener")从 ExecutionEvent 中提取您需要的对象。

于 2011-10-27T11:20:54.503 回答
0

在我看来,您应该提供两个插件,一个支持 3.1 版到“略低于”3.5,另一个支持 3.5 版以上。所以你并不能真正选择,它基本上是 Eclipse 插件层根据你的版本范围选择正确的插件层。

或者,如果您只提供已编译的类文件,那么您当然可以根据测试运行的 Eclipse 的版本动态加载所需的类。

于 2011-10-28T11:57:06.580 回答