Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Inject Part - What did I miss?
Inject Part - What did I miss? [message #1005611] Sun, 27 January 2013 14:04 Go to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Injection can be really neat, but if something doesn't work, you have no idea Sad

Injection works for almost all stuff I tried, but not for (visible) parts. Create a new e4 Project and modify the AboutHandler:


public class AboutHandler {
	
	@Execute
	public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Active AppPart a) {
		MessageDialog.openInformation(shell, "About", "e4 Application example.");
	}
	
	@CanExecute
	public boolean canExecute(MApplication app,  EModelService service, @Optional AppPart appPartInjected) {
	    PartImpl element = (PartImpl) service.find("myapp.part.0", app);
	    AppPart appPart = (AppPart) element.getObject();
            return true;
	}
}


"appPartInjected" is always null. I also tried @Named("myapp.part.0") or @Active, but null. If I don't use @Optional, the method is not called, of course.

I worked through several tutorials (maybe a little bit too fast?), but could not find any hint, that injection of parts does not work that easily. Instead I found the code I am using within "canExecute". This code also works and I get my part, so the part is available! And should be active, right? It is the only part I am using, and it is also visible at startup? However, why is injection not working in that case?

BTW: AppPart is just a regular part. I haven't specified anything special, e.g. @Singleton... but I tried of course. I also tried @Creatable, but this is not the correct effect Wink

Thanks in advance!
Re: Inject Part - What did I miss? [message #1005626 is a reply to message #1005611] Sun, 27 January 2013 17:20 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member

No never use DI-Fields inside handlers! Never use @Inject inside
handlers this is doomed to fail miserably because the handler is created
in a completely different context then it is executed!


See here: http://www.eclipse.org/forums/index.php/m/987832/
Re: Inject Part - What did I miss? [message #1005628 is a reply to message #1005626] Sun, 27 January 2013 18:24 Go to previous messageGo to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Hmh, as far as I understand the thread you are pointing to, you can not/should not inject fields. I am aware that the creation of handlers (objects) is done in another context. Maybe...

I don't know if my issue is related to that! According to tutorials DI perfectly works for handlers if method parameters are injected. For example, "MApplication app" and "EModelService Service" are perfectly injected. Also other data classes I have (POJOs, Singleton) are perfectly injected (not in my example). Why is this not working for parts? If anyone is telling me "this does not work for parts DOT", then ok. However, this not straightforward IMHO.

Re: Inject Part - What did I miss? [message #1005631 is a reply to message #1005628] Sun, 27 January 2013 19:01 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
The problem is, that not all objects, which are available in YOUR model will be available in Handlöer creation model.

The standard e4 Service perhaps will be, but not your parts IMHO.
Re: Inject Part - What did I miss? [message #1005632 is a reply to message #1005631] Sun, 27 January 2013 19:09 Go to previous messageGo to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
I don't think that this has anything to do with handler creation. It is about calling the method. Method arguments should be injected during the call.

Even the context must be correct. Inside my method I am able to retrieve the part using "service.find". Therefore, I cannot understand why "@Named("myapp.part.0") AppPart appPartInjected" cannot inject the argument. Of course, I would, if I study the implementation. However, DI should make our life easier, so I started a topic here.

No easy answer to this?
Re: Inject Part - What did I miss? [message #1005664 is a reply to message #1005611] Mon, 28 January 2013 07:22 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
AppPart is never put to a context! You can only get MPart the model
instance, from there you can the real instance using MPart#getObject()!

You don't need the @Active because a handler is always run with the
currently active context.

Tom

Am 27.01.13 15:04, schrieb Kirsten M. Z.:
> Injection can be really neat, but if something doesn't work, you have no
> idea :(
>
> Injection works for almost all stuff I tried, but not for (visible)
> parts. Create a new e4 Project and modify the AboutHandler:
>
>
> public class AboutHandler {
>
> @Execute
> public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell
> shell, @Active AppPart a) {
> MessageDialog.openInformation(shell, "About", "e4 Application
> example.");
> }
>
> @CanExecute
> public boolean canExecute(MApplication app, EModelService service,
> @Optional AppPart appPartInjected) {
> PartImpl element = (PartImpl) service.find("myapp.part.0", app);
> AppPart appPart = (AppPart) element.getObject();
> return true;
> }
> }
>
> "appPartInjected" is always null. I also tried @Named("myapp.part.0") or
> @Active, but null. If I don't use @Optional, the method is not called,
> of course.
>
> I worked through several tutorials (maybe a little bit too fast?), but
> could not find any hint, that injection of parts does not work that
> easily. Instead I found the code I am using within "canExecute". This
> code also works and I get my part, so the part is available! And should
> be active, right? It is the only part I am using, and it is also visible
> at startup? However, why is injection not working in that case?
>
> BTW: AppPart is just a regular part. I haven't specified anything
> special, e.g. @Singleton... but I tried of course. I also tried
> @Creatable, but this is not the correct effect ;)
>
> Thanks in advance!
Previous Topic:What's up with the Package Explorer selection in Juno?
Next Topic:How to connect to DB / Legacy system in parallel to eclipse start?
Goto Forum:
  


Current Time: Fri Mar 29 05:34:01 GMT 2024

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

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

Back to the top