Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Injection of own objects in handler(Added object to IEclipseContext but framework can't find it in Handler class)
Injection of own objects in handler [message #1100066] Mon, 02 September 2013 16:09 Go to next message
Marina Knieling is currently offline Marina Knieling
Messages: 78
Registered: February 2013
Member
Hi there,

I recently ran into a problem with DI. I have a handler (in a fragment) that dynamically generates a new window including a few parts. The fragment includes some commands and their handlers that are used in the generated window.

In the mentioned handler I have the following code:

// Convert String to Path
Path path = FileSystems.getDefault().getPath(s_path, new String[]{});
//Create child context for all import file stuff
IEclipseContext importFileContext = ctx.createChild("importFileContext");
// add a named value so that the sequencer file processor finds the path
importFileContext.set("importPath", path);
SequencerFileProcessor fileProcessor = ContextInjectionFactory.make(SequencerFileProcessor.class, importFileContext);
ctx.set(SequencerFileProcessor.class, fileProcessor);


I need this SequencerFileProcessor in another handler in the same package now (in the CanExecute method to be precise), so I thought I simply can inject it in that handler. But that doesn't work and as a result a NullPointerException is thrown.
I also tried injecting it again via ContextInjectionFactory, but I don't have access to the child context which includes needed values (the path).

[EDIT] It works in the parts I create dynamically afterwards in the handler, so the object must be available in general. Why not in the handler?

Is there anything I get wrong about Dependency Injection or why isn't this working?

Thanks in advance.

Marina

[Updated on: Mon, 02 September 2013 16:35]

Report message to a moderator

Re: Injection of own objects in handler [message #1100456 is a reply to message #1100066] Tue, 03 September 2013 06:20 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1289
Registered: July 2012
Senior Member
How do you inject your value into your handler? Via field or method injection?

Handlers are created in another context (there are several posts in this forum about this). At the time the handler is instantiated, the value is not created yet. At least this is my understanding.

So if I'm right you are using field injection in your handler. Which you definitely shouldn't do. If you change this to method injection and inject your value as method parameter to your @CanExecute or @Execute, it should work.
Re: Injection of own objects in handler [message #1100541 is a reply to message #1100456] Tue, 03 September 2013 08:26 Go to previous messageGo to next message
Marina Knieling is currently offline Marina Knieling
Messages: 78
Registered: February 2013
Member
Thanks for the quick reply.

Yes, I am using field injection. I didn't know that method injection works for the @CanExecute annotation as well. I know it works for @Execute. I will give it try later and then give a quick report here whether it worked for me.
Re: Injection of own objects in handler [message #1100667 is a reply to message #1100541] Tue, 03 September 2013 12:24 Go to previous messageGo to next message
Marina Knieling is currently offline Marina Knieling
Messages: 78
Registered: February 2013
Member
That worked. Thanks a lot. That was easy Wink
Is there any information on that topic in a more "prominent" place like tutorials or wiki? As I said I've read in some tutorial that @Execute includes @Inject, but not for @CanExecute. I think there wouldn't be such questions if this was written anywhere. Maybe I didn't see that information.
Re: Injection of own objects in handler [message #1100670 is a reply to message #1100667] Tue, 03 September 2013 12:29 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
All declarative method calls (@Execute, @CanExecute, @PersistState,
@Presist, ...) in the end anything invoked through CIF#invoke() can have
any number of arguments.

Tom

On 03.09.13 14:24, Marina Knieling wrote:
> That worked. Thanks a lot. That was easy ;)
> Is there any information on that topic in a more "prominent" place like
> tutorials or wiki? As I said I've read in some tutorial that @Execute
> includes @Inject, but not for @CanExecute. I think there wouldn't be
> such questions if this was written anywhere. Maybe I didn't see that
> information.
Previous Topic:How to register an Event Broker at startup
Next Topic:Saving the perspective layout
Goto Forum:
  


Current Time: Tue Sep 23 08:29:04 GMT 2014

Powered by FUDForum. Page generated in 0.02763 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software