Search in sources :

Example 46 with Provider

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

the class SpiBindingsTest method testProviderBinding.

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

        protected void configure() {
            bind(String.class).toInstance("A");
        }
    });
    Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
    };
    Binding<Provider<String>> binding = injector.getBinding(providerOfStringKey);
    assertEquals(providerOfStringKey, binding.getKey());
    checkBindingSource(binding);
    assertTrue(binding instanceof ProviderBinding);
    binding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {

        @Override
        public Void visit(ProviderBinding<? extends Provider<String>> binding) {
            assertEquals(Key.get(String.class), binding.getProvidedKey());
            return null;
        }
    });
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 47 with Provider

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

the class OverrideModuleTest method testOverrideScopeAnnotation.

public void testOverrideScopeAnnotation() {
    final Scope scope = new Scope() {

        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            throw new AssertionError("Should not be called");
        }
    };
    final SingleUseScope replacementScope = new SingleUseScope();
    Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(TestScopeAnnotation.class);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, replacementScope);
        }
    };
    Injector injector = createInjector(Modules.override(original).with(replacements));
    injector.getInstance(Date.class);
    assertTrue(replacementScope.used);
}
Also used : Scope(com.google.inject.Scope) Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 48 with Provider

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

the class InjectorSpiTest method testExistingBinding.

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

        @Override
        protected void configure() {
            bind(Foo.class);
            bind(Baz.class);
        }
    });
    // Sanity check -- ensure we return the proper binding for all existing bindings.
    for (Map.Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        assertSame(entry.getValue(), injector.getExistingBinding(entry.getKey()));
    }
    // Now run through specifics...
    Binding<?> binding;
    // 1) non-Provider Foo.class
    binding = injector.getExistingBinding(Key.get(Foo.class));
    assertNotNull(binding);
    assertEquals(Foo.class, binding.getKey().getTypeLiteral().getRawType());
    // 2) Provider<Foo> class (should already exist, because Baz @Injects it).
    // the assertTrue is a bit stricter than necessary, but makes sure this works for pre-existing Provider bindings
    assertTrue(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Foo>>() {
    })));
    binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Foo>>() {
    }));
    assertNotNull(binding);
    assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
    assertEquals(Foo.class, ((Provider) binding.getProvider().get()).get().getClass());
    // 3) non-Provider Baz.class
    binding = injector.getExistingBinding(Key.get(Baz.class));
    assertNotNull(binding);
    assertEquals(Baz.class, binding.getKey().getTypeLiteral().getRawType());
    // 4) Provider<Baz> class (should not already exist, because nothing used it yet).
    // the assertFalse is a bit stricter than necessary, but makes sure this works for non-pre-existing Provider bindings
    assertFalse(injector.getAllBindings().containsKey(Key.get(new TypeLiteral<Provider<Baz>>() {
    })));
    binding = injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Baz>>() {
    }));
    assertNotNull(binding);
    assertEquals(Provider.class, binding.getKey().getTypeLiteral().getRawType());
    assertEquals(Baz.class, ((Provider) binding.getProvider().get()).get().getClass());
    // 5) non-Provider Bar, doesn't exist.
    assertNull(injector.getExistingBinding(Key.get(Bar.class)));
    // 6) Provider Bar, doesn't exist.
    assertNull(injector.getExistingBinding(Key.get(new TypeLiteral<Provider<Bar>>() {
    })));
}
Also used : Binding(com.google.inject.Binding) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) Injector(com.google.inject.Injector) Map(java.util.Map) Key(com.google.inject.Key)

Example 49 with Provider

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

the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.

public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
    final Scope scope = new Scope() {

        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            return unscoped;
        }

        @Override
        public String toString() {
            return "ORIGINAL SCOPE";
        }
    };
    final Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(scope);
            bind(String.class).in(scope);
        }
    };
    Module originalWrapper = new AbstractModule() {

        @Override
        protected void configure() {
            install(original);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, new SingleUseScope());
        }
    };
    try {
        createInjector(Modules.override(originalWrapper).with(replacements));
        fail("Exception expected");
    } catch (CreationException e) {
        assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
    }
}
Also used : Scope(com.google.inject.Scope) Modules(com.google.inject.util.Modules) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 50 with Provider

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

the class ContextScope method scope.

public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
    return new Provider<T>() {

        public T get() {
            synchronized (ContextScope.class) {
                final Stack<WeakReference<Context>> stack = getContextStack();
                // The context should never be finalized as long as the provider is still in memory
                final Context context = stack.peek().get();
                final Map<Key<?>, Object> objectsForScope = getScopedObjectMap(context);
                if (objectsForScope == null)
                    // May want to consider throwing an exception here (if provider is used after onDestroy())
                    return null;
                @SuppressWarnings({ "unchecked" }) T current = (T) objectsForScope.get(key);
                if (current == null && !objectsForScope.containsKey(key)) {
                    current = unscoped.get();
                    objectsForScope.put(key, current);
                }
                return current;
            }
        }
    };
}
Also used : Context(android.content.Context) RoboContext(roboguice.util.RoboContext) WeakReference(java.lang.ref.WeakReference) Key(com.google.inject.Key) 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