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);
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations