use of org.eclipse.e4.core.di.IInjector in project eclipse.platform.runtime by eclipse.
the class ProviderHelper method findProvider.
public static ExtendedObjectSupplier findProvider(String qualifier, PrimaryObjectSupplier objectSupplier) {
synchronized (extendedSuppliers) {
if (extendedSuppliers.containsKey(qualifier))
return extendedSuppliers.get(qualifier);
Bundle bundle = FrameworkUtil.getBundle(ProviderHelper.class);
if (bundle == null) {
// In case we are not in an OSGi context, see bug 513883
return null;
}
BundleContext bundleContext = bundle.getBundleContext();
try {
String filter = '(' + ExtendedObjectSupplier.SERVICE_CONTEXT_KEY + '=' + qualifier + ')';
ServiceReference<?>[] refs = bundleContext.getServiceReferences(ExtendedObjectSupplier.SERVICE_NAME, filter);
if (refs != null && refs.length > 0) {
ExtendedObjectSupplier supplier;
// Explicitly sort by ranking if more than one supplier is found
if (refs.length > 1) {
Arrays.sort(refs, Collections.reverseOrder());
}
supplier = (ExtendedObjectSupplier) bundleContext.getService(refs[0]);
if (objectSupplier != null) {
IInjector injector = InjectorFactory.getDefault();
injector.inject(supplier, objectSupplier);
}
extendedSuppliers.put(qualifier, supplier);
return supplier;
}
} catch (InvalidSyntaxException e) {
// should not happen - we tested the line above
}
extendedSuppliers.put(qualifier, null);
return null;
}
}
use of org.eclipse.e4.core.di.IInjector in project eclipse.platform.runtime by eclipse.
the class AtInjectTest method suite.
public static Test suite() {
IInjector injector = InjectorFactory.getDefault();
injector.addBinding(SpareTire.class);
injector.addBinding(Seat.class);
injector.addBinding(DriversSeat.class);
injector.addBinding(Cupholder.class);
injector.addBinding(Tire.class);
injector.addBinding(FuelTank.class);
injector.addBinding(Car.class).implementedBy(Convertible.class);
injector.addBinding(Seat.class).named(Drivers.class.getName()).implementedBy(DriversSeat.class);
injector.addBinding(Engine.class).implementedBy(V8Engine.class);
injector.addBinding(Tire.class).named("spare").implementedBy(SpareTire.class);
Car car = injector.make(Car.class, null);
return Tck.testsFor(car, true, true);
}
use of org.eclipse.e4.core.di.IInjector in project eclipse.platform.runtime by eclipse.
the class ProviderInjectionTest method testInvokeWithProvider.
@Test
public synchronized void testInvokeWithProvider() {
// create context
IEclipseContext context = EclipseContextFactory.create();
context.set(String.class.getName(), "abc");
IInjector injector = InjectorFactory.getDefault();
injector.addBinding(TestData.class);
TestInvokeClass userObject = new TestInvokeClass();
assertEquals(1, ContextInjectionFactory.invoke(userObject, Execute.class, context, null));
assertNotNull(userObject.provider.get());
assertEquals("abc", userObject.provider.get().data);
}
use of org.eclipse.e4.core.di.IInjector in project eclipse.platform.runtime by eclipse.
the class InjectionEventTest method testEventInjection.
@Test
public void testEventInjection() throws InvocationTargetException, InstantiationException {
IInjector injector = InjectorFactory.getDefault();
injector.addBinding(MyBinding.class);
IEclipseContext context = EclipseContextFactory.create();
InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context);
// initial state
assertEquals(0, target.counter1);
assertNull(target.string1);
assertEquals(1, target.counter3);
assertNull(target.string3);
assertNotNull(target.myBinding);
// send event1
helper.sendEvent("e4/test/event1", "event1data");
assertEquals(1, target.counter1);
assertEquals("event1data", target.string1);
assertEquals(1, target.counter3);
assertNull(target.string3);
assertNotNull(target.myBinding);
// send event2
helper.sendEvent("e4/test/event2", "event2data");
assertEquals(1, target.counter1);
assertEquals("event1data", target.string1);
assertEquals(1, target.counter3);
assertNull(target.string3);
assertNotNull(target.myBinding);
// send event3
helper.sendEvent("e4/test/event3", "event3data");
assertEquals(1, target.counter1);
assertEquals("event1data", target.string1);
assertEquals(2, target.counter3);
assertEquals("event3data", target.string3);
assertNotNull(target.myBinding);
// send event1 again
helper.sendEvent("e4/test/event1", "abc");
assertEquals(2, target.counter1);
assertEquals("abc", target.string1);
assertEquals(2, target.counter3);
assertEquals("event3data", target.string3);
assertNotNull(target.myBinding);
}
use of org.eclipse.e4.core.di.IInjector in project eclipse.platform.runtime by eclipse.
the class ProviderInjectionTest method testConstructorWithProvider.
@Test
public synchronized void testConstructorWithProvider() {
// create context
IEclipseContext context = EclipseContextFactory.create();
context.set(String.class.getName(), "abc");
IInjector injector = InjectorFactory.getDefault();
injector.addBinding(TestData.class);
TestConstructorClass userObject = ContextInjectionFactory.make(TestConstructorClass.class, context);
assertNotNull(userObject);
assertNotNull(userObject.provider);
assertNotNull(userObject.provider.get());
assertEquals("abc", userObject.provider.get().data);
}
Aggregations