Bug 317595 Add ability to uninject without specific context
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.
bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ReflectionContributionFactory.java
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.ui.internal.workbench;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.InjectionException;
import org.eclipse.e4.core.services.contributions.IContributionFactory;
import org.eclipse.e4.core.services.contributions.IContributionFactorySpi;
import org.eclipse.emf.common.util.URI;
import org.osgi.framework.Bundle;
import org.osgi.service.log.LogService;
/**
 * Create the contribution factory.
 */
public class ReflectionContributionFactory implements IContributionFactory {