Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » E4 Problems with injecting a field that is a subclass of composite
E4 Problems with injecting a field that is a subclass of composite [message #897114] Sat, 21 July 2012 19:14 Go to next message
Markus Oley is currently offline Markus Oley
Messages: 251
Registered: July 2009
Location: Germany
Senior Member
Hi to anyone,

I have still one more problem with DI in E4.

I have a part

public class PreviewEditorPart  {
  @Inject
  private TestToBeInjected test;
....
}


The TestToBeInjected is the following:


public class TestToBeInjected extends Composite {

	@Inject
	public TestToBeInjected(MDirtyable dirtyable, final Composite parent) {
		super (null, 0);
	}


But I get an error message: Unable to process "PreviewEditorPart.test": no actual value was found for the argument "TestToBeInjected".

Can you tell me, what's my problem?

Thanks for your help,

Best regards
Markus

Re: E4 Problems with injecting a field that is a subclass of composite [message #897142 is a reply to message #897114] Sun, 22 July 2012 09:45 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de Alwis
Messages: 242
Registered: July 2009
Senior Member
Sounds like you don't have an instance of TestToBeInjected available within your injection context. You need to do one of the following:

  1. annotate your "test" field as @Optional to indicate that it's ok to be injected with 'null' if a alue can't be located;
  2. provide an instance of your TestToBeInjected in your context either by setting it at some point in the context hierarchy, or through an OSGi service; or
  3. annotate TestToBeInjected with @Createable to indicate to the DI that it can create an instance of TestToBeInjected as needed.
Re: E4 Problems with injecting a field that is a subclass of composite [message #897167 is a reply to message #897142] Sun, 22 July 2012 17:52 Go to previous messageGo to next message
Markus Oley is currently offline Markus Oley
Messages: 251
Registered: July 2009
Location: Germany
Senior Member
Hi Brian,

thank you for your answer,

I have annotated TestToBeInjected with a @Creatable, but nevertheless this errormessage is logged.

What could I do else to solve my problem?

Here's the stack:

org.eclipse.e4.core.di.InjectionException: Unable to process "PreviewEditorPart.test": no actual value was found for the argument "TestToBeInjected".
at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:392)
at org.eclipse.e4.core.internal.di.InjectorImpl.resolveRequestorArgs(InjectorImpl.java:383)
at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:100)
at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:318)
at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:240)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:161)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:102)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:71)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:53)
at org.eclipse.e4.ui.workbench.renderers.swt.ContributedPartRenderer.createWidget(ContributedPartRenderer.java:141)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createWidget(PartRenderingEngine.java:889)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:623)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer.showTab(StackRenderer.java:1042)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java:67)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:41)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:180)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4291)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:187)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java:38)
at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)
at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)
at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)
at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:81)
at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java:57)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)
at org.eclipse.e4.ui.model.application.ui.impl.ElementContainerImpl.setSelectedElement(ElementContainerImpl.java:171)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java:103)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:639)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:59)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:635)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:59)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveRenderer.processContents(PerspectiveRenderer.java:59)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:635)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.showTab(PerspectiveStackRenderer.java:103)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java:67)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:41)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:180)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4291)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:187)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java:38)
at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)
at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)
at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)
at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:81)
at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java:57)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)
at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:135)
at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:1)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java:103)
at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.postProcess(PerspectiveStackRenderer.java:77)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:639)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:59)
at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.processContents(WBWRenderer.java:626)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:635)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:725)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:696)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:690)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:675)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:957)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)


Best regards
Markus

Re: E4 Problems with injecting a field that is a subclass of composite [message #897268 is a reply to message #897167] Mon, 23 July 2012 09:18 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

See if it works with a no-arg constructor. Just as it was failing before when it couldn't find the TestToBeInjected, it is likely not finding either the MDirtyable or Shell (my guess is the shell).

To get the active shell use @Named(IServiceConstants.ACTIVE_SHELL), and to be safe it would likely be a good idea to use @Optional with that just in case. But someone else might be able to give a little clarity on that.

Then you might also want to reconsider your use of MDirtyable. Again, someone else might be able to provide a little more clarity, but I have a suspicion that using MPart instead (as MPart implements MDirtyable) is better/safer. I do not believe there is a reference to MDirtyable in the context.

Take care,

JD
Re: E4 Problems with injecting a field that is a subclass of composite [message #897346 is a reply to message #897268] Mon, 23 July 2012 12:44 Go to previous messageGo to next message
Markus Oley is currently offline Markus Oley
Messages: 251
Registered: July 2009
Location: Germany
Senior Member
Hi again,

This:
@Creatable
public class TestToBeInjected  {
	public TestToBeInjected(@Optional  @Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell) {
		//super(activeShell, SWT.NONE);
	}
	
	@PostConstruct
	public void hello () {
		System.out.println ("");
	}
}


doesn't work,

This:

@Creatable
public class TestToBeInjected {
	public TestToBeInjected(@Active Shell activeShell) {
		//super(activeShell, SWT.NONE);
	}


doesn't work too.

As soon as I remove the injection of the active shell it works.

Is it possible, that I don't have all required plugins as dependency?

Strange - isn#t it?

Any help welcome

Regards
Markus


Re: E4 Problems with injecting a field that is a subclass of composite [message #897352 is a reply to message #897346] Mon, 23 July 2012 13:01 Go to previous messageGo to next message
Markus Oley is currently offline Markus Oley
Messages: 251
Registered: July 2009
Location: Germany
Senior Member
Ok, I guess I am not yet that familiar with DI syntax in E4...


@Inject @Named(IServiceConstants.ACTIVE_SHELL)
	private Shell activeShell;


works
Smile
Re: E4 Problems with injecting a field that is a subclass of composite [message #897375 is a reply to message #897352] Mon, 23 July 2012 16:18 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 596
Registered: December 2010
Senior Member

Think of DI as a fancy way to get things from a Java map.
This thing has a key (class object i.e. Sopot.class or String object "Sopot") and when you do @Inject ISopot sopot; a "lookup" for the map with key ISopot.class is made. When you do
@Inject @Name("fancyName") a lookup for key "fancyName" is made (Actually the IServiceConstants.ACTIVE_SHELL is a string).

This map in the analogy is mostly an IEclipseContext which is a map on steroids (supports context functions etc.) Much like maps, before getting things you have to put them there or will get your original 'no actual value was found for the argument "TestToBeInjected"' exception. You can suppress this exception with @Optional which tells the engine "Shut up if you don't find it". Some things like active shell, parent composite, model objects (MPart etc) are put in this map by the framework. At some point you can put things in the IEclipseContext yourself and get it injected later. Also the @Createable tells to the engine that if you don't find this thing in the "map" (IEC) create it yourself and put it there.

More details here: http://www.eclipse.org/forums/index.php/t/333467/
Re: E4 Problems with injecting a field that is a subclass of composite [message #897393 is a reply to message #897346] Mon, 23 July 2012 20:57 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

IIRC, try using the @Inject annotation on the constructor. While the DI engine will look for @Inject delegates (not real delegates) like @PostConstruct, @Focus, etc. The engine does not assume it should work with every constructor, and of the "injectable" constructors it will pick the one it can resolve the greatest number of arguments.

I could be wrong, but I am fairly certain that the constructor must be annotated with @Inject (even though it is clearly asking for an injectable argument). Because logically it does not make any sense for the the field to be injected (which occurs after the constructor returns) but not the constructor argument. So try that, if I have time tonight I'll look it up.

Take care,

JD
Re: E4 Problems with injecting a field that is a subclass of composite [message #897394 is a reply to message #897393] Mon, 23 July 2012 21:06 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

Yep that does the trick!
Re: E4 Problems with injecting a field that is a subclass of composite [message #897534 is a reply to message #897394] Tue, 24 July 2012 08:40 Go to previous message
Brian de Alwis is currently offline Brian de Alwis
Messages: 242
Registered: July 2009
Senior Member
JD is exactly right: the DI will auto-create @Creatable objects if they have a zero-argument constructor, otherwise the constructor must be marked as @Inject-able.

Brian.
Previous Topic:Dependency injection of fields
Next Topic:(Announce) "Jeeeyul's Eclipse Themes" is Updated
Goto Forum:
  


Current Time: Tue Jul 22 13:41:43 EDT 2014

Powered by FUDForum. Page generated in 0.02364 seconds