Search code examples
javaeclipsercpe4

Get e4 service without injection


I'm trying to adapt an Eclipse RCP 3.x application to use some facilities from e4. For this reason, there is no e4xmi file.

In particular, I need to get access to some services:

public class RunModeService {

@Inject
private static ECommandService commandService;
@Inject 
private static EHandlerService handlerService;

...
}

It would appear that if I instantiate the class myself (as I am doing) then none of the injection takes place.

Is it possible to get hold of these services another way? If so, I can begin to hook into e4 and DI, by creating a command whose handler is instantiated by the framework and in which injection occurs.


Solution

  • If you have the IEclipseContext you can get objects using:

    ECommandService commandService = eclipseContext.get(ECommandService.class);
    

    IEclipseContext can be injected.

    You can create your own objects using ContextInjectionFactory which will do DI on the object for your:

    MyClasss myClass = ContextInjectionFactory.make(MyClass.class, eclipseContext);
    

    or you can do injection on an existing class instance with:

    ContextInjectionFactory.inject(myClass, eclipseContext);
    

    In a view or editor you can get the Eclipse Context from the view / editor site using:

    eclipseContext = ((PartSite)getSite()).getContext();
    

    But PartSite is an internal class so it really should not be used.