Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 317595

Summary: Add ability to uninject without specific context
Product: z_Archived Reporter: Oleg Besedin <ob1.eclipse>
Component: E4Assignee: Project Inbox <e4.runtime-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: emoffatt, ob1.eclipse, pwebster, remy.suen
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 318026    
Attachments:
Description Flags
Patch
none
Patch to IContributionFactory
none
Patch to place the 'uninvoke' behavior into the PRE
none
Patch for the CCE none

Description Oleg Besedin CLA 2010-06-22 10:55:26 EDT
The common pattern for use contexts with UI is:

- get a parent context
- create a local context parented of the parent context
- add some one-time use values in the local context
- #make() an object using the local context

The problem comes at the end of the life cycle: the local context is not stored and therefore we can't use #uninject(object, localContext). 

Possible solutions:

1) Add a variable to the object:
   @Inject IEclipseContext localContext;
and use it to dispose the local context, severing links between the parent context, local context, and the object

Pros: can be done with no changes to the framework 
Cons: a new field has to be added to every affected class

2) Keep a map of all contexts injected into the object and add a method

  static #getInjectedContext()

which would return the local context used in #make() and can be used to uninject

Cons: memory and performance impact - having a table and that lists all contexts and all injected objects and having to search through them on every context disposal scares me.

3) Create a new #make() / #inject() method that would take two contexts: dynamic and static, the parent context being used as dynamic and local context used as static. 
 localContext = EclipseContextFactory.create();
 object = ContextInjectionFactory.make(parentContext, localContext);

and then call #uninject(object, parentContext) as there will be no ties established to the local context.

Cons: it is RC1 and this will be a substantial change.

----

I'll try (3) to see the impact of changes and if that turns out to be too much at this point, I'll do (2) for the initial release.
Comment 1 Oleg Besedin CLA 2010-06-22 16:12:03 EDT
Created attachment 172458 [details]
Patch

+ Added:
 ContextInjectionFactory#make(clazz, IEclipseContext context, IEclipseContext staticContext)

- values from the staticContext override values from the context
- changes to values injected from the staticContext are not tracked
- disposal or GC of the staticContext does not affect injected object
- the context is considered to be injected into the object, uninject / disposal will of the context will affect the object


+ Changed #uninject() to call @PreDestroy

+ For completness, added similar invokation method:

 ContextInjectionFactory#invoke(object, qualifier, context, localContext, defaultValue) 

=====
How to use: 

IEclipseContext localContext = EclipseContextFactory.create();
localContext.set("abc", "myValue");
object = ContextInjectionFactory.make(class, context, localContext);

This allows passing extra non-tracked values that are not present in the "real" context.
Comment 2 Oleg Besedin CLA 2010-06-22 16:13:52 EDT
Patch appled to CVS Head.
Comment 3 Oleg Besedin CLA 2010-06-25 13:29:23 EDT
Created attachment 172794 [details]
Patch to IContributionFactory

One more change: make the new functionality available in the IContributionFactory.
Comment 4 Oleg Besedin CLA 2010-06-25 13:30:29 EDT
Patch "Patch to IContributionFactory" applied to CVS Head. The patch makes new method vissible via IContributionFactory.
Comment 5 Eric Moffatt CLA 2010-06-29 09:28:14 EDT
Created attachment 172996 [details]
Patch to place the 'uninvoke' behavior into the PRE


To test this just start an inner with a new WS, minimize a stack an right-click on the perspective in the switcher and choose 'reset'.
Comment 6 Eric Moffatt CLA 2010-06-29 09:28:33 EDT
Re-opening...
Comment 7 Oleg Besedin CLA 2010-06-29 10:37:11 EDT
Created attachment 173006 [details]
Patch for the CCE
Comment 8 Oleg Besedin CLA 2010-06-29 10:37:55 EDT
Patch applied to CVS Head.