Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Injecting own object to global Eclipse context(The "no actual value was found for the argument" injection exception)
Injecting own object to global Eclipse context [message #1618928] Mon, 16 February 2015 08:11 Go to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

Dear experts,

I am currently injecting my own object to the global Eclipse context, so to make it available through injection anywhere. I am doing this in the plugin activator.

As far as I understand, I can achieve injection of an already created myClass instance (myObj) either by IEclipseContext.set or via CIF:
@Inject
IEclipseContext eclipseContext;
[...]
MyClass myObj = new MyClass(a, b, c, d, e, f, bugsBunny, omega) ;
ContextInjectionFactory.inject(myObj, eclipseContext);  // A
eclipseContext.set(MyClass.class, myObj);               // B


After this, when application model objects get loaded, I want to exploit DI to get my myObj injected but I cannot get this to work. In both cases I get:

@Inject
MyClass myObj;
  ||
  ||
  \/
org.eclipse.e4.core.di.InjectionException: Unable to process "Foo.myObj": no actual value was found for the argument "MyClass".


What am I doing wrong?

thanks a lot for your patience,
-Piero

[Updated on: Mon, 16 February 2015 08:41]

Report message to a moderator

Re: Injecting own object to global Eclipse context [message #1618934 is a reply to message #1618928] Mon, 16 February 2015 08:18 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
How do you get access to the context in the Activator? Generally
speaking you can't get access to the correct root context very easily
inside your Activator.

Tom

On 16.02.15 09:11, Piero Campalani wrote:
> Dear experts,
>
> I am currently injecting my own object to the global Eclipse context, so
> to make it available through injection anywhere. I am doing this in the
> plugin activator.
>
> As far as I understand, I can achieve injection of an already created
> myClass instance (myObj) either by IEclipseContext.set or via CIF:
> @Inject
> IEclipseContext eclipseContext;
> [...]
> MyClass myObj = new myClass(a, b, c, d, e, f, bugsBunny, omega) ;
> ContextInjectionFactory.inject(myObj, eclipseContext); // A
> eclipseContext.set(myClass.class, myObj); // B
>
> After this, when application model objects get loaded, I want to exploit
> DI to get my myObj injected but I cannot get this to work. In both cases
> I get:
>
>
> @Inject
> MyClass myObj;
> ||
> ||
> \/
> org.eclipse.e4.core.di.InjectionException: Unable to process
> "Foo.myClass": no actual value was found for the argument "myClass".
>
> What am I doing wrong?
>
> thanks a lot for your patience,
> -Piero
Re: Injecting own object to global Eclipse context [message #1618960 is a reply to message #1618934] Mon, 16 February 2015 08:37 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

Thanks Tom.

I am using the EclipseContextFactory to get it (or at least to get an instance of that, which at this point I think might get replaced by a new context after Activator's call):

IEclipseContext eclipseContext = EclipseContextFactory
				.getServiceContext(bundleContext);


If I use the method suggested in Eclipse 4 RCP Development How-to by Ram Kulkarni (2013) then (I see now): everything is fine.

IEclipseContext eclipseContext = E4Workbench.getServiceContext();


I opted for what I thought was a less hacky method (ECF) because E4Worlbench is warned to be not API (yet?).

[Updated on: Mon, 16 February 2015 08:38]

Report message to a moderator

Re: Injecting own object to global Eclipse context [message #1619045 is a reply to message #1618960] Mon, 16 February 2015 09:49 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
Ok that's what I expected and this is the wrong way because it creates a
new service context for each requesting bundleContext!

Maybe you should describe why you want to access the IEclipseContext in
your Activator there are better methods than that.

Tom

On 16.02.15 09:37, Piero Campalani wrote:
> Thanks Tom.
>
> I am using the EclipseContextFactory to get it (or at least to get an
> instance of that, which at this point I think might get replaced by a
> new context after Activator's call):
>
> IEclipseContext eclipseContext = EclipseContextFactory
> .getServiceContext(bundleContext);
>
> If I use the method suggested in Eclipse 4 RCP Development How-to by Ram
> Kulkarni (2013) then (I see now): everything is fine.
>
> IEclipseContext eclipseContext = E4Workbench.getServiceContext();
>
> I opted for what I thought was a less hacky method (ECF) because
> E4Worlbench is warned to be not API (yet?).
Re: Injecting own object to global Eclipse context [message #1619064 is a reply to message #1619045] Mon, 16 February 2015 10:04 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

I see..
Thank you Thomas, it's great to have such responsiveness in this forum, I appreciate a lot.

My scenario is: I have a collection of objects which I want to bind with a JFace viewer. I guess this is a common scenario.

As I want my collection to get injected as a field into the part handler, I thought to inject it in the bundle Activator as it is the only way I know I can be sure my collection is available at the time my part is handled.

I cannot use a @PostContextCreate annotation in an lifeCycleURI-like class since I am in a fragment, not an independent product.
@see http://stackoverflow.com/questions/22940803/e4-lifecycleuri-property-for-fragment-plugin
@see https://www.eclipse.org/forums/index.php/m/1614628/#msg_1614628

So finally... Would there be other, more conventional ways to do this?

-Piero
Re: Injecting own object to global Eclipse context [message #1619080 is a reply to message #1619064] Mon, 16 February 2015 10:19 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
* use an IContextFunction
* or use an Addon contributed to the model

Tom

On 16.02.15 11:04, Piero Campalani wrote:
> I see.. Thank you Thomas, it's great to have such responsiveness in this
> forum, I appreciate a lot.
>
> My scenario is: I have a collection of objects which I want to bind with
> a JFace viewer. I guess this is a common scenario.
>
> As I want my collection to get injected as a field into the part
> handler, I thought to inject it in the bundle Activator as it is the
> only way I know I can be sure my collection is available at the time my
> part is handled.
>
> I cannot use a @PostContextCreate annotation in an lifeCycleURI-like
> class since I am in a fragment, not an independent product.
> @see
> http://stackoverflow.com/questions/22940803/e4-lifecycleuri-property-for-fragment-plugin
>
> @see https://www.eclipse.org/forums/index.php/m/1614628/#msg_1614628
>
> So finally... Would there be other, more conventional ways to do this?
>
> -Piero
Re: Injecting own object to global Eclipse context [message #1619148 is a reply to message #1619080] Mon, 16 February 2015 11:17 Go to previous messageGo to next message
Mateusz Malinowski is currently offline Mateusz MalinowskiFriend
Messages: 36
Registered: March 2013
Location: Bristol
Member
Piero,

Personally I can tell you that placing that in addon should make it working properly. I have a singleton class which I create in addon using CIF.make, then I set it in my application's context and later it is available to be injected anywhere I want.
Re: Injecting own object to global Eclipse context [message #1619190 is a reply to message #1619080] Mon, 16 February 2015 11:53 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

Thanks Thomas, it makes more sense indeed now.
So, in both cases I can act before model objects start getting loade, is that right?

(Just out of curiosity: Any comment regarding E4Workbench.getServiceContext() ? )
Re: Injecting own object to global Eclipse context [message #1619228 is a reply to message #1619148] Mon, 16 February 2015 12:29 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

Great, I'll addon-ize too.
Thanks Mateusz.
Re: Injecting own object to global Eclipse context [message #1622327 is a reply to message #1619228] Wed, 18 February 2015 12:17 Go to previous message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 110
Registered: January 2015
Senior Member

Just for the sake of completeness..

I successfully injected my own object in the Eclipse application context by using a context function.
I followed the tutorial by Lars on the matter: link.

As suggested there, if you persist the object in the application context, then the context function will just be called once.


@Lars Vogel: I had to find a separate article (this one) in order to understand a proper location of the component definition (OSGI-INF/) and how to register my component in the plugin MANIFEST (using Service-Component + lazy): you might want to complete the information in your tutorial.

[Updated on: Wed, 18 February 2015 12:42]

Report message to a moderator

Previous Topic:Resource Leak CSSSWTFontHelper, CSSSWTColorHelper
Next Topic:No events received when I listen outside the class who sends
Goto Forum:
  


Current Time: Mon Sep 25 02:48:45 GMT 2017

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

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