支持多个版本的Eclipse

发布于 2024-12-10 06:08:26 字数 520 浏览 0 评论 0原文

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

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

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

任何人都可以帮忙吗?

谢谢, 艾伦

I have an Eclipse plugin and I am aiming for 3.1 or 3.2 as a minimum version to support. The problem is that some of my code only works in version 3.5 and above (see my other question: Is there an alternative to CaretListener in Eclipse?).

Now that I have code that works in the older versions and different code that works in the newer versions, is there a way that I can call the newer code only if my plugin is running in version 3.5 or above and then revert to the old code if running anything older?

As a test, I've created two plugins that have the same class within it (just doing slightly different things). I have marked the org.eclipse.ui dependency as a minimum of 3.5 in one plugin and 3.1 as a minimum in the other but I can't get the one that relies on 3.5 to be ignored in older versions...

Can anyone help?

Thanks,
Alan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-12-17 06:08:26

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

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

如果>=3.5,则注册MyListener,否则注册OldMyListener

编辑:

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

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 中提取所需的对象。

You could use org.eclipse.core.runtime.Platform to get the org.eclipse.ui Bundle and check the version.

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

Register MyListener if >=3.5, and OldMyListener otherwise.

EDIT:

Right, the above is only good for capturing differences in runtime behaviour.

Eclipse supports a couple of tricks for only loading some classes.

The easiest from a development point of view is the trick that @ShiDoiSi mentioned.

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);

Another option that uses more of the eclipse framework would be defining your own extension point so that contributions from other bundles can decide which version to use. Basically it's the same pattern as above, except the version checking is done by the dependency ranges on the plugin the contributes the Executable to run. Depend on org.eclipse.ui [0.0.0,3.5.0) for the old way, and simply specifying org.eclipse.ui 3.5.0 (that's an open ended range on 3.5.0) for the current way. Then you can read your extension and instantiate the class provided.

If you were creating extra plugins for this (a little heavy weight for the 2 differences) you could define a command in your main plugin, and have the extra plugins provide the equivalent handler. The plugins would still have to have dependency ranges so that only one would load for the <3.5 or >=3.5 case. Then using the command API you could execute the command (and the correct handler would run).

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);

Then your implementation of handler would use HandlerUtil.getVariable(event, "toAddListener") to extract the object you need from the ExecutionEvent.

我的黑色迷你裙 2024-12-17 06:08:26

在我看来,您应该提供两个插件,一个支持版本 3.1 到“略低于”3.5,另一个支持 3.5 及以上版本。因此,您实际上不必进行选择,基本上是 Eclipse 插件层根据您的版本范围选择正确的插件。

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

It sounds to me that you should be supplying two plugins, one supporting version 3.1 to "just below" 3.5, and the other one from 3.5 upwards. So you don't really get to choose, it's basically the Eclipse plugin layer choosing the right one based on your version ranges.

Or if you provide just the compiled class files, then of course you could load the required class dynamically based on testing the version of the running Eclipse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文