Eclipse plugins and Groovy : when binary compatibility is not enough

One of my current responsibilities is to maintain an internally developed plugin, used by various members of the team to generate code from the analysis model. As far as I can tell by the webstats of the update site, every version is downloaded by 18 people, a small but heterogeneous user base.

My biggest problem is the Eclipse version. The analysts are not exactly Java geeks waiting anxiously for nightly builds of Eclipse, they use a ‘standard’ 2.1.2, mainly because it’s stable and well internationalized. Things go wilder in the programmers team : versions ranging from conservative (2.1.x) to liberal (3.0M4) and even the occasional dumbass with the latest integration build (that would be me, of course).

The ‘enhanced binary compatibility’ in 3.0M7 came as a relief, diminishing the need to switch between Eclipse versions in order to develop the plugin or work on other tasks. Well, I still have to briefly test the damn thing on Eclipse 2.1.x before releases. However, running simultaneously two or three Eclipse instances is no piece of cake for my 512Mb laptop (I still haven’t found who I have to kill here in order to be awarded a memory upgrade). Unfortunately, checking out the plugin source into M7 has shown the invisible ugly face of ‘binary compatibility’: the plugin doesn’t compile.

There are just a handful of lines of code, some emphasizing differences in Eclipse API which are somehow hidden in ‘compatibility mode’, some effectively showing small bugs in plugin behavior. But the real issue here is that I cannot really develop the plugin in M7 until I manage somehow to compile it, while not losing downwards compatibility.

Let’s dissect one of the compilation issues. The bummer concerns automatic opening of an editor (or focus if already opened) when clicking on its reference (somehow similar to what happens when you Ctrl+click on a class name in JDT). In the older API it was a question of page.openEditor(file); where page is a IWorkbenchPage and file is an IFile. This simple stuff worked well until 3.0M4, then (M5) things changed to page.openEditor(new org.eclipse.ui.part.FileEditorInput(file),editorId); where FileEditorInput implements (among others) an IEditorInput. While this is certainly nice because you may directly link editors to something other than files***, obviously the old code does not compile under M7.

Maintaining different projects for ‘old’ and ‘new’ style projects for 10 or so lines of code is obviously overkill. Second solution – via reflection, but it would mean more than few lines of code and the result would not exactly be comprehensible nor maintainable. Only thing left : use a scripting language.

Of course I could have taken any decent scripting language embedded in Java. Decision to go with Groovy was taken mainly because of its coolness factor, but I am sure the idea will apply easily with Jython (big favorite of mine) or the performance-aware Pnuts, for instance.

In a nutshell, you have to execute a line of code depending of the current Eclipse version (it’s a little bit trickier, but we’ll discuss later about it).

groovy.lang.Binding binding = new Binding();binding.setVariable(“page”, page);binding.setVariable(“file”, file);groovy.lang.GroovyShell groovyShell = new GroovyShell(getClass().getClassLoader(), binding);if (newPlatform){ return groovyShell.evaluate(“page.openEditor(new org.eclipse.ui.part.FileEditorInput(file),editorId);”, someExpressionId);}else{ return groovyShell.evaluate(“page.openEditor(file);”, someExpressionId);}

It’s basically a vanilla flavored ripoff of the Groovy embedding example from the docs. The boring part : caching the binding, hiding everything behind a nice facade, is left as an exercise for the [interested] reader. Remember to pass the classLoader of the current class, do not create a GroovyClassLoader out of nowhere or you’ll end up dealing with Eclipse own class loader, which means trouble for simple tasks like these.

How do we know that the Eclipse version is the ‘new’ or the ‘old’ one is not that simple because remember : ‘old’ means 2.x up to 3.0M4. So finding out Eclipse SDK version is not enough, you have to find out another discriminant which in our case is the ‘org.eclipse.ui.ide’ plugin. Result:

boolean newPlatform;//find out if we are inside a new or an old platformPluginVersionIdentifier pvi = Platform.getPluginRegistry().getPluginDescriptor(“org.eclipse.platform”).getVersionIdentifier();newPlatform = pvi.getMajorComponent() >= 3 && Platform.getPluginRegistry().getPluginDescriptor(“org.eclipse.ui.ide”) != null;

No, we are not ready to deploy yet. A small trick has to be performed or the plugin won’t start under older versions of Eclipse. We had to add some new plugins to dependencies (in the pligin descriptor) such as the aforementioned ‘org.eclipse.ui.ide’, obviously the older versions of Eclipse will not find it, hence block our plugin activation on startup. In order to overcome this, you have to add (by hand !) a lesser known attribute (‘optional’) in the corresponding tag from the plugin.xml file : \<import plugin=”org.eclipse.ui.ide”/> becomes \<import plugin=”org.eclipse.ui.ide” optional=”true”/>. Now, the plugin is ready to be deployed.

For those brave enough to dare distributing such a plugin via an update site remember to ‘cheat’ by not allowing new plugins such as ‘org.eclipse.ui.ide’ in the feature.xml file (again, delete by hand). The ‘optional’ attribute doesn’t help in this case. Go figure…

I hope that some of you will find useful this recipe for maintaining compatibility between different Eclipse versions with minimum of fuss. However, please note the specific prerequisites for this type of solution :

- there are only simple ‘few-lines’ modifications - the code is not expected to evolve a lot in the ‘affected’ areas - the evaluated code is not in a performance-sensitive area

***Interesting enough, this was one of the reasons I recommended against adopting RCP in one of our apps, a few weeks ago. It’s nice to see that – now – the mechanism linking editors and resources is MUCH more flexible. Anyway, this won’t probably change the decision of not using RCP because the main issue it’s the volume of code we have to change. Development of one of the app modules started almost a year ago and the animal it’s already sold and deployed on different production sites: upgrading would be a real nightmare. Maintaining a fork of the app is not an option either. Well, I guess we’ll just have to cope with ‘plain old’ Jface and SWT.

PS After some days of ‘silence’, I have noticed from the logs that most popular posts on my blog are those concerning Eclipse plugins and Manning books (I seem to have a nice Google ranking on these topics). So, expect more of these (I am reading the MEAP of ‘Tapestry in action’ – a review should be up shortly).

Comments

Tags