I know of three ways of how Eclipse 4 can inject objects in your classes:
- During start-up the Eclipse runtime looks for relevant annotations in the classes it instantiates.
- Objects injected in 1. are tracked and will be re-injected if changed.
- Manually triggering injection using the ContextInjectionFactory and IEclipseContext.
What you want may be possible with the third option. Here is a code example:
ManipulateModelhandler man = new ManipulateModelhandler();
//inject the context into an object
//IEclipseContext iEclipseContext was injected into this class
ContextInjectionFactory.inject(man,iEclipseContext);
man.execute();
The problem is, however; that the IEclipseContext already needs to be injected into a class that can access the object that needs injection. Depending on the number of necessary injections, it might be more useful to use delegation instead (testability would be one argument).
@Inject
public void setFoo(Foo foo) {
//Bar is not attached to the e4 Application Model
bar.setFoo(foo);
}
Therefore, a better solution is probably using the @Creatable annotation.
Simply annotate your class, and give it a no-argument constructor.
@Creatable
public class Foo {
public Foo () {}
}
Using @Inject on that type as in the method above, will let Eclipse instantiate and inject it.
The disadvantage is that you cannot control the object creation anymore, as you would with ContextInjectionFactory.inject(..).