Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Questions regarding @Creatable
Questions regarding @Creatable [message #938340] Tue, 09 October 2012 19:43 Go to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1339
Registered: July 2012
Senior Member
Hi,

I'm working through the Eclipse 4 book by Lars Vogel. I've found the information on adding own objects to the context with the @Creatable annotation useful and tried it like described also online here. http://www.vogella.com/articles/EclipseRCP/article.html#ownobjects

So I created a class that holds some configurations and annotated it with @Creatable

In my Lifecycle Hook I injected the object with @Inject and filled the instance with some configuration parameters, so it can be used later on in my application.

Then I created a handler that gets this configuration object injected with @Inject, and within the execute method, code is executed dependent on values within the configuration object.

This didn't work as there was a new instance injected into my handler instead of the instance created first within the lifecycle hook.

I changed this to create the object with new and put it to the context manually. In this case the object injected into my handler is the one created within the lifecycle hook.

So it seems on using @Creatable the context hierarchy isn't searched for an existing instance, but a new one is created if there is none in the current context.

Long text short question, how does @Creatable work? Should it search die context hierarchy for an existing instance or simply create a new instance for the current context?

Greez,
Dirk
Re: Questions regarding @Creatable [message #938453 is a reply to message #938340] Tue, 09 October 2012 22:27 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Try annotating the creatable class with @Singleton also. You would get something like:
@Creatable
@Singleton
public class MyCreatableClass

and this should prevent it from being recreated by the injector.
Re: Questions regarding @Creatable [message #938787 is a reply to message #938453] Wed, 10 October 2012 06:39 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1339
Registered: July 2012
Senior Member
Hi Sopot,

thanks for the answer. Yes this solves the issue with creating a new object.

So if @Singleton is used together with @Creatable, the context hierarchy is searched if there is an instance already. Without @Singleton the injector will create a new instance for every context. Is this correct? Regarding to what I know about dependency injection right now, this would make sense.

Greez,
Dirk
Re: Questions regarding @Creatable [message #946600 is a reply to message #938787] Tue, 16 October 2012 10:27 Go to previous message
François POYER is currently offline François POYER
Messages: 6
Registered: December 2009
Junior Member
As far as i can tell, if @Singleton is not used, the instance created and put in the context for your Lifecycle hook is garbaged as soon as the hook returns (try overriding #finalize and put a breakpoint or trace in there) because the context only holds soft (or maybe weak) references on objects its creates (but strong references on objects that are manually put in it). And so when it comes to your handler, there is (again) no instance in the context so a new one is created and put in the context (and it too will be garbaged as soon as possible).

Using @Singleton creates a reference to self, and thus prevents the garbaging, meaning the first instance will stay in the context and be found (and used) for you handler.
Previous Topic:Dynamically creating / deleting menu items in MPopupMenu doesn't work
Next Topic:MHandledToolItem visibleWhen not working?
Goto Forum:
  


Current Time: Thu Oct 23 11:57:47 GMT 2014

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

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