Search in sources :

Example 76 with Provider

use of com.google.inject.Provider in project che by eclipse.

the class ExtensionInitializer method startExtensions.

public void startExtensions() {
    Map<String, Provider> providers = extensionManager.getExtensions();
    for (Entry<String, Provider> entry : providers.entrySet()) {
        final String extensionFqn = entry.getKey();
        final Provider extensionProvider = entry.getValue();
        try {
            // Order of startup is managed by GIN dependency injection framework
            extensionProvider.get();
        } catch (Throwable e) {
            Log.error(ExtensionInitializer.class, "Can't initialize extension: " + extensionFqn, e);
        }
    }
    eventBus.fireEvent(new ExtensionsInitializedEvent());
}
Also used : ExtensionsInitializedEvent(org.eclipse.che.ide.api.extension.ExtensionsInitializedEvent) Provider(com.google.inject.Provider)

Example 77 with Provider

use of com.google.inject.Provider in project hudson-2.x by hudson.

the class SezPozExtensionModule method bindProvider.

private void bindProvider(final Binder binder, final SpaceIndexItem item, final Key key) {
    binder.bind(key).toProvider(new Provider() {

        public Object get() {
            try {
                return item.instance();
            } catch (final InstantiationException e) {
                throw new ProvisionException(e.toString(), e);
            }
        }
    }).in(Scopes.SINGLETON);
    bindHierarchy(binder, key);
}
Also used : ProvisionException(com.google.inject.ProvisionException) Provider(com.google.inject.Provider)

Example 78 with Provider

use of com.google.inject.Provider in project guice by google.

the class MultibinderTest method testMultibinderCanInjectCollectionOfProvidersWithAnnotation.

public void testMultibinderCanInjectCollectionOfProvidersWithAnnotation() {
    final Annotation ann = Names.named("foo");
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, ann);
            multibinder.addBinding().toProvider(Providers.of("A"));
            multibinder.addBinding().toProvider(Providers.of("B"));
            multibinder.addBinding().toInstance("C");
        }
    };
    Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
    Injector injector = Guice.createInjector(module);
    Collection<Provider<String>> providers = injector.getInstance(Key.get(collectionOfProvidersOfStrings, ann));
    Collection<String> values = collectValues(providers);
    assertEquals(expectedValues, values);
    Collection<javax.inject.Provider<String>> javaxProviders = injector.getInstance(Key.get(collectionOfJavaxProvidersOf(stringType), ann));
    assertEquals(expectedValues, collectValues(javaxProviders));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 79 with Provider

use of com.google.inject.Provider in project guice by google.

the class MultibinderTest method testEmptyMultibinder.

public void testEmptyMultibinder() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder.newSetBinder(binder(), String.class);
        }
    });
    assertEquals(ImmutableSet.of(), injector.getInstance(new Key<Set<String>>() {
    }));
    assertEquals(ImmutableList.of(), injector.getInstance(new Key<Collection<Provider<String>>>() {
    }));
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 80 with Provider

use of com.google.inject.Provider in project guice by google.

the class OptionalBinderTest method testSetBindingOverridesDefault.

public void testSetBindingOverridesDefault() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder<String> optionalBinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalBinder.setDefault().toInstance("a");
            optionalBinder.setBinding().toInstance("b");
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals("b", injector.getInstance(String.class));
    Optional<String> optional = injector.getInstance(Key.get(optionalOfString));
    assertTrue(optional.isPresent());
    assertEquals("b", optional.get());
    Optional<Provider<String>> optionalP = injector.getInstance(Key.get(optionalOfProviderString));
    assertTrue(optionalP.isPresent());
    assertEquals("b", optionalP.get().get());
    Optional<javax.inject.Provider<String>> optionalJxP = injector.getInstance(Key.get(optionalOfJavaxProviderString));
    assertTrue(optionalJxP.isPresent());
    assertEquals("b", optionalJxP.get().get());
    assertOptionalVisitor(stringKey, setOf(module), VisitType.BOTH, 0, instance("a"), instance("b"), null);
    if (HAS_JAVA_OPTIONAL) {
        optional = toOptional(injector.getInstance(Key.get(javaOptionalOfString)));
        assertTrue(optional.isPresent());
        assertEquals("b", optional.get());
        optionalP = toOptional(injector.getInstance(Key.get(javaOptionalOfProviderString)));
        assertTrue(optionalP.isPresent());
        assertEquals("b", optionalP.get().get());
        optionalJxP = toOptional(injector.getInstance(Key.get(javaOptionalOfJavaxProviderString)));
        assertTrue(optionalJxP.isPresent());
        assertEquals("b", optionalJxP.get().get());
    }
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Aggregations

Provider (com.google.inject.Provider)133 Injector (com.google.inject.Injector)72 AbstractModule (com.google.inject.AbstractModule)63 Module (com.google.inject.Module)44 Key (com.google.inject.Key)17 TypeLiteral (com.google.inject.TypeLiteral)10 ArrayList (java.util.ArrayList)10 ProvisionException (com.google.inject.ProvisionException)9 Dependency (com.google.inject.spi.Dependency)9 Map (java.util.Map)8 Before (org.junit.Before)7 CurrentUser (com.google.gerrit.server.CurrentUser)6 RequestContext (com.google.gerrit.server.util.RequestContext)6 ThreadLocalRequestContext (com.google.gerrit.server.util.ThreadLocalRequestContext)6 InMemoryModule (com.google.gerrit.testutil.InMemoryModule)6 PrivateModule (com.google.inject.PrivateModule)6 Scope (com.google.inject.Scope)6 List (java.util.List)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Annotation (java.lang.annotation.Annotation)5