Tuesday, February 16, 2010

Xtext, guice and IExecutableExtensionFactory

Consider that you have written an eclipse plugin using Xtext and added some custom made commands, handlers, menu items ... to your code.

Quite possibly you will want to use google guice sooner or later in your custom code, either to use the advantages of this dependency injection framework yourself, or to access some of the objects which are provided by your xtext plugin, and which you want to inject in your code.

In this case the IExecutableExtensionFactory mechanism provided by eclipse and in a more concrete manner either the subclass of org.eclipse.xtext.ui.core.guice.AbstractGuiceAwareExecutableExtensionFactory which is automatically generated for your ui plugin of the Xtext language. You can also subclass the AbstractGuiceAwareExecutableExtensionFactory yourself, if you do not want to or cannot modify your module definition for guice generated by Xtext. Looking at the generated example should be enough documentation to get you started.

Once you are done, all you need to do is to add the factory in front of your class descriptions in any of the extensions you are using and the class becomes part of the injection container and can "receive" injections:

    <handler class="my.package.MyFactory:my.package.handler.SomeHandler" commandId="commandid">

That's all and you are ready to go.

1 comment: