Search in sources :

Example 11 with InjectionException

use of org.eclipse.e4.core.di.InjectionException 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 12 with InjectionException

use of org.eclipse.e4.core.di.InjectionException 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 13 with InjectionException

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

the class EventObjectSupplier method subscribe.

private void subscribe(String topic, IRequestor requestor) {
    Subscriber subscriber = new Subscriber(requestor, topic);
    synchronized (registrations) {
        if (registrations.containsKey(subscriber))
            return;
    }
    BundleContext bundleContext = FrameworkUtil.getBundle(EventObjectSupplier.class).getBundleContext();
    if (bundleContext == null)
        throw new InjectionException(// $NON-NLS-1$
        "Unable to subscribe to events: org.eclipse.e4.core.di.extensions bundle is not activated");
    String[] topics = new String[] { topic };
    Dictionary<String, Object> d = new Hashtable<>();
    d.put(EventConstants.EVENT_TOPIC, topics);
    EventHandler wrappedHandler = makeHandler(topic, requestor);
    ServiceRegistration<EventHandler> registration = bundleContext.registerService(EventHandler.class, wrappedHandler, d);
    // be OK
    synchronized (registrations) {
        registrations.put(subscriber, registration);
    }
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) Hashtable(java.util.Hashtable) EventHandler(org.osgi.service.event.EventHandler) BundleContext(org.osgi.framework.BundleContext)

Example 14 with InjectionException

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

the class MethodRequestor method execute.

@Override
public Object execute() throws InjectionException {
    if (actualArgs == null) {
        if (location.getParameterTypes().length > 0)
            // optional method call
            return null;
    }
    Object userObject = getRequestingObject();
    if (userObject == null)
        return null;
    Object result = null;
    if (!location.isAccessible()) {
        location.setAccessible(true);
    }
    boolean pausedRecording = false;
    if ((primarySupplier != null)) {
        primarySupplier.pauseRecording();
        pausedRecording = true;
    }
    try {
        result = location.invoke(userObject, actualArgs);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new InjectionException(e);
    } catch (InvocationTargetException e) {
        Throwable originalException = e.getCause();
        // http://bugs.eclipse.org/bugs/show_bug.cgi?id=457687
        if (originalException instanceof Error) {
            throw (Error) originalException;
        }
        throw new InjectionException((originalException != null) ? originalException : e);
    } finally {
        if (pausedRecording)
            primarySupplier.resumeRecording();
        clearResolvedArgs();
    }
    return result;
}
Also used : InjectionException(org.eclipse.e4.core.di.InjectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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