Search in sources :

Example 6 with InjectionException

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);
}
Also used : PrimaryObjectSupplier(org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)

Example 7 with InjectionException

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;
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) Type(java.lang.reflect.Type) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 8 with InjectionException

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);
}
Also used : IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 9 with InjectionException

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);
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 10 with InjectionException

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);
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

InjectionException (org.eclipse.e4.core.di.InjectionException)10 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)4 Test (org.junit.Test)4 PrimaryObjectSupplier (org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 BundleContext (org.osgi.framework.BundleContext)2 Constructor (java.lang.reflect.Constructor)1 Type (java.lang.reflect.Type)1 Hashtable (java.util.Hashtable)1 IBinding (org.eclipse.e4.core.di.IBinding)1 Creatable (org.eclipse.e4.core.di.annotations.Creatable)1 ExtendedObjectSupplier (org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)1 IObjectDescriptor (org.eclipse.e4.core.di.suppliers.IObjectDescriptor)1 IRequestor (org.eclipse.e4.core.di.suppliers.IRequestor)1 Bundle (org.osgi.framework.Bundle)1 EventHandler (org.osgi.service.event.EventHandler)1