Questions regarding @Creatable [message #938340] |
Tue, 09 October 2012 15:43  |
Eclipse User |
|
|
|
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 #946600 is a reply to message #938787] |
Tue, 16 October 2012 06:27  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.04545 seconds