Search in sources :

Example 6 with PrimaryObjectSupplier

use of org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier in project eclipse.platform.runtime by eclipse.

the class ContextInjectionFactory method setDefault.

/**
 * Specifies context used by the injector to create its internal objects.
 * Providing this context allows injector to become aware of higher-level
 * constructs, such as application logging and synchronization.
 * @param context the context to be used as a data source by the injector
 * @since 1.2
 */
public static void setDefault(IEclipseContext context) {
    PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector);
    injector.setDefaultSupplier(supplier);
}
Also used : PrimaryObjectSupplier(org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)

Example 7 with PrimaryObjectSupplier

use of org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier 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 8 with PrimaryObjectSupplier

use of org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier in project eclipse.platform.runtime by eclipse.

the class InjectorImpl method internalMake.

private Object internalMake(Class<?> clazz, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier tempSupplier) {
    if (shouldDebug && classesBeingCreated.contains(clazz))
        // $NON-NLS-1$ //$NON-NLS-2$
        LogHelper.logWarning("Possible recursive reference trying to create class \"" + clazz.getName() + "\".", null);
    try {
        if (shouldDebug)
            classesBeingCreated.add(clazz);
        boolean isSingleton = isAnnotationPresent(clazz, Singleton.class);
        if (isSingleton) {
            synchronized (singletonCache) {
                if (singletonCache.containsKey(clazz))
                    return singletonCache.get(clazz);
            }
        }
        Constructor<?>[] constructors = getDeclaredConstructors(clazz);
        // Sort the constructors by descending number of constructor arguments
        ArrayList<Constructor<?>> sortedConstructors = new ArrayList<>(constructors.length);
        for (Constructor<?> constructor : constructors) sortedConstructors.add(constructor);
        Collections.sort(sortedConstructors, new Comparator<Constructor<?>>() {

            @Override
            public int compare(Constructor<?> c1, Constructor<?> c2) {
                int l1 = c1.getParameterTypes().length;
                int l2 = c2.getParameterTypes().length;
                return l2 - l1;
            }
        });
        for (Constructor<?> constructor : sortedConstructors) {
            // skip private and protected constructors; allow public and package visibility
            int modifiers = constructor.getModifiers();
            if (((modifiers & Modifier.PRIVATE) != 0) || ((modifiers & Modifier.PROTECTED) != 0))
                continue;
            // unless this is the default constructor, it has to be tagged
            if (!isAnnotationPresent(constructor, Inject.class) && constructor.getParameterTypes().length != 0)
                continue;
            ConstructorRequestor requestor = new ConstructorRequestor(constructor, this, objectSupplier, tempSupplier);
            Object[] actualArgs = resolveArgs(requestor, objectSupplier, tempSupplier, false, true, false);
            if (unresolved(actualArgs) != -1)
                continue;
            requestor.setResolvedArgs(actualArgs);
            Object newInstance = requestor.execute();
            if (newInstance != null) {
                internalInject(newInstance, objectSupplier, tempSupplier);
                if (isSingleton) {
                    synchronized (singletonCache) {
                        // TBD this is not quite right, synch the method
                        singletonCache.put(clazz, newInstance);
                    }
                }
                return newInstance;
            }
        }
        // $NON-NLS-1$
        throw new InjectionException("Could not find satisfiable constructor in " + clazz.getName());
    } catch (NoClassDefFoundError | NoSuchMethodError e) {
        throw new InjectionException(e);
    } finally {
        if (shouldDebug)
            classesBeingCreated.remove(clazz);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) InjectionException(org.eclipse.e4.core.di.InjectionException)

Example 9 with PrimaryObjectSupplier

use of org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier in project eclipse.platform.runtime by eclipse.

the class InjectorImpl method uninject.

@Override
public void uninject(Object object, PrimaryObjectSupplier objectSupplier) {
    try {
        if (!forgetInjectedObject(object, objectSupplier))
            // not injected at this time
            return;
        processAnnotated(PreDestroy.class, object, object.getClass(), objectSupplier, null, new ArrayList<Class<?>>(5));
        ArrayList<Requestor<?>> requestors = new ArrayList<>();
        processClassHierarchy(object, objectSupplier, null, true, /* track */
        false, /* inverse order */
        requestors);
        for (Requestor<?> requestor : requestors) {
            // Ask suppliers to fill actual values {requestor, descriptor[], actualvalues[] }
            Object[] actualArgs = resolveArgs(requestor, null, null, true, false, false);
            int unresolved = unresolved(actualArgs);
            if (unresolved == -1) {
                requestor.setResolvedArgs(actualArgs);
                requestor.execute();
            } else {
                if (requestor.isOptional())
                    requestor.setResolvedArgs(null);
                else if (shouldDebug) {
                    StringBuilder tmp = new StringBuilder();
                    // $NON-NLS-1$
                    tmp.append("Uninjecting object \"");
                    tmp.append(object.toString());
                    // $NON-NLS-1$
                    tmp.append("\": dependency on \"");
                    tmp.append(requestor.getDependentObjects()[unresolved].toString());
                    // $NON-NLS-1$
                    tmp.append("\" is not optional.");
                    LogHelper.logError(tmp.toString(), null);
                }
            }
        }
    } catch (NoClassDefFoundError e) {
        throw new InjectionException(e);
    } catch (NoSuchMethodError e) {
        throw new InjectionException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) InjectionException(org.eclipse.e4.core.di.InjectionException) IRequestor(org.eclipse.e4.core.di.suppliers.IRequestor)

Example 10 with PrimaryObjectSupplier

use of org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier in project eclipse.platform.runtime by eclipse.

the class InjectorImpl method findExtendedSupplier.

private ExtendedObjectSupplier findExtendedSupplier(IObjectDescriptor descriptor, PrimaryObjectSupplier objectSupplier) {
    Annotation[] qualifiers = descriptor.getQualifiers();
    if (qualifiers == null)
        return null;
    for (Annotation qualifier : qualifiers) {
        Class<?> type = qualifier.annotationType();
        String key = ((Class<?>) type).getName();
        ExtendedObjectSupplier supplier;
        try {
            // use qualified name to refer to a class that might be missing
            supplier = org.eclipse.e4.core.internal.di.osgi.ProviderHelper.findProvider(key, defaultSupplier);
        } catch (NoClassDefFoundError e) {
            // OSGi framework not present
            return null;
        }
        if (supplier != null)
            return supplier;
    }
    return null;
}
Also used : Annotation(java.lang.annotation.Annotation) ExtendedObjectSupplier(org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)

Aggregations

PrimaryObjectSupplier (org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier)4 InjectionException (org.eclipse.e4.core.di.InjectionException)3 ExtendedObjectSupplier (org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier)3 ArrayList (java.util.ArrayList)2 IBinding (org.eclipse.e4.core.di.IBinding)2 IObjectDescriptor (org.eclipse.e4.core.di.suppliers.IObjectDescriptor)2 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 IInjector (org.eclipse.e4.core.di.IInjector)1 Creatable (org.eclipse.e4.core.di.annotations.Creatable)1 IRequestor (org.eclipse.e4.core.di.suppliers.IRequestor)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1