use of org.eclipse.e4.core.di.InjectionException in project eclipse.platform.runtime by eclipse.
the class ContextInjectionFactory method inject.
/**
* Injects a context into a domain object. See the class comment for details on the injection
* algorithm that is used.
*
* @param object The object to perform injection on
* @param context The context to obtain injected values from
* @throws InjectionException if an exception occurred while performing this operation
*/
public static void inject(Object object, IEclipseContext context) throws InjectionException {
PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector);
injector.inject(object, supplier);
}
use of org.eclipse.e4.core.di.InjectionException in project eclipse.platform.runtime by eclipse.
the class OSGiObjectSupplier method get.
@Override
public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
final Class<?> requestingObjectClass = requestor.getRequestingObjectClass();
final Type desiredType = descriptor.getDesiredType();
if (BundleContext.class.equals(desiredType)) {
final Bundle bundle = FrameworkUtil.getBundle(requestingObjectClass);
// Iff track is request and there is no listener yet, lets track the bundle
if (track) {
if (!requestor2listener.containsKey(requestor)) {
track(bundle, requestor);
}
// Handlers only executed once and thus don't track the BC/Bundle.
// Still guard to now de-register a non-existing listener.
} else if (requestor2listener.containsKey(requestor)) {
untrack(requestor);
}
final BundleContext bundleContext = bundle.getBundleContext();
if (bundleContext != null) {
return bundleContext;
} else if (descriptor.getQualifier(Optional.class) != null) {
// Do not have a bundle context but requestor has marked the parameter/field optional
return null;
}
// $NON-NLS-1$ //$NON-NLS-2$
throw new InjectionException("Unable to inject BundleContext: " + bundle.getSymbolicName() + " bundle is not active or starting/stopping");
} else if (Bundle.class.equals(desiredType)) {
// state. However, the requestor will go away along with its bundle anyway.
return FrameworkUtil.getBundle(requestingObjectClass);
}
// Annotation used with unsupported type
return null;
}
use of org.eclipse.e4.core.di.InjectionException in project eclipse.platform.runtime by eclipse.
the class InjectionErrorReportingTest method testRecursionError.
/**
* Manual test to check error message for recursive object creation Although
* bug 377343 disabled throwing InjectionExceptions on recursive creation,
* the fix for bug 457687 now exposes java.lang.Errors (such as
* StackOverflowError) rather than wrapping them in an InjectionException.
*/
@Test(expected = StackOverflowError.class)
public void testRecursionError() {
IEclipseContext context = EclipseContextFactory.create();
ContextInjectionFactory.make(InjectedRecursive.class, context);
context.set(InjectedRecursive.class, new InjectedRecursive());
ContextInjectionFactory.make(InjectedRecursive.class, context);
}
use of org.eclipse.e4.core.di.InjectionException in project eclipse.platform.runtime by eclipse.
the class RecursiveObjectCreationTest method testSelfInject.
/**
* Checks a simple case of constructor needing the same class
*/
@Test
public void testSelfInject() {
IEclipseContext context = EclipseContextFactory.create();
boolean exceptionReceived = false;
try {
CheckSelfInject testInstance = ContextInjectionFactory.make(CheckSelfInject.class, context);
// unreachable
assertNotNull(testInstance);
} catch (InjectionException e) {
exceptionReceived = true;
}
assertTrue(exceptionReceived);
}
use of org.eclipse.e4.core.di.InjectionException in project eclipse.platform.runtime by eclipse.
the class RecursiveObjectCreationTest method testNested.
/**
* Checks inner class using outer class which is still being created
*/
@Test
public void testNested() {
IEclipseContext context = EclipseContextFactory.create();
boolean exceptionReceived = false;
try {
TestOuterClass outer = ContextInjectionFactory.make(TestOuterClass.class, context);
// unreachable
assertNotNull(outer);
} catch (InjectionException e) {
exceptionReceived = true;
}
assertTrue(exceptionReceived);
}
Aggregations