Search in sources :

Example 6 with HasDependencies

use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.

the class OptionalBinderTest method testDependencies_actual.

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

        @Override
        protected void configure() {
            OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<String> binding = injector.getBinding(Key.get(String.class));
    HasDependencies withDependencies = (HasDependencies) binding;
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("B"), elements);
}
Also used : Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 7 with HasDependencies

use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.

the class OptionalBinderTest method recurseForDependencies.

@SuppressWarnings("rawtypes")
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) {
    Set<String> elements = Sets.newHashSet();
    for (Dependency<?> dependency : hasDependencies.getDependencies()) {
        Binding<?> binding = injector.getBinding(dependency.getKey());
        HasDependencies deps = (HasDependencies) binding;
        if (binding instanceof InstanceBinding) {
            elements.add((String) ((InstanceBinding) binding).getInstance());
        } else {
            elements.addAll(recurseForDependencies(injector, deps));
        }
    }
    return elements;
}
Also used : InstanceBinding(com.google.inject.spi.InstanceBinding) HasDependencies(com.google.inject.spi.HasDependencies)

Example 8 with HasDependencies

use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.

the class CheckedProviderTest method testDependencies_Bind.

public void testDependencies_Bind() {
    bindInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("Foo");
            bind(Integer.class).toInstance(5);
            bind(Double.class).toInstance(5d);
            bind(Long.class).toInstance(5L);
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).to(DependentRemoteProvider.class);
        }
    });
    HasDependencies hasDependencies = (HasDependencies) bindInjector.getBinding(Key.get(remoteProviderOfFoo));
    hasDependencies = (HasDependencies) bindInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    // Make sure that that is dependent on DependentRemoteProvider.
    assertEquals(Dependency.get(Key.get(DependentRemoteProvider.class)), Iterables.getOnlyElement(hasDependencies.getDependencies()));
    // And make sure DependentRemoteProvider has the proper dependencies.
    hasDependencies = (HasDependencies) bindInjector.getBinding(DependentRemoteProvider.class);
    Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 9 with HasDependencies

use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.

the class CheckedProviderTest method testDependencies_Provides.

public void testDependencies_Provides() {
    providesInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("Foo");
            bind(Integer.class).toInstance(5);
            bind(Double.class).toInstance(5d);
            bind(Long.class).toInstance(5L);
            install(ThrowingProviderBinder.forModule(this));
        }

        @SuppressWarnings("unused")
        @CheckedProvides(RemoteProvider.class)
        Foo foo(String s, Integer i, Double d, Long l) {
            return null;
        }
    });
    HasDependencies hasDependencies = (HasDependencies) providesInjector.getBinding(Key.get(remoteProviderOfFoo));
    // RemoteProvider<String> is dependent on the provider method..
    hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    // And the provider method has our real dependencies..
    hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 10 with HasDependencies

use of com.google.inject.spi.HasDependencies in project guice by google.

the class CheckedProviderTest method testDependencies_Cxtor.

public void testDependencies_Cxtor() {
    cxtorInjector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("Foo");
            bind(Integer.class).toInstance(5);
            bind(Double.class).toInstance(5d);
            bind(Long.class).toInstance(5L);
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(DependentMockFoo.class);
        }
    });
    Key<?> key = Key.get(remoteProviderOfFoo);
    // RemoteProvider<String> is dependent on Result.
    HasDependencies hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
    key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
    assertEquals(Result.class, key.getTypeLiteral().getRawType());
    // Result is dependent on the fake CheckedProvider impl
    hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
    key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
    assertTrue(CheckedProvider.class.isAssignableFrom(key.getTypeLiteral().getRawType()));
    // And the CheckedProvider is dependent on DependentMockFoo...
    hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
    key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
    assertEquals(DependentMockFoo.class, key.getTypeLiteral().getRawType());
    // And DependentMockFoo is dependent on the goods.
    hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
    Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Aggregations

HasDependencies (com.google.inject.spi.HasDependencies)34 AbstractModule (com.google.inject.AbstractModule)30 Injector (com.google.inject.Injector)20 Key (com.google.inject.Key)14 Dependency (com.google.inject.spi.Dependency)10 InstanceBinding (com.google.inject.spi.InstanceBinding)6 Function (com.google.common.base.Function)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 BindingAnnotation (com.google.inject.BindingAnnotation)4 Annotation (java.lang.annotation.Annotation)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 Multibinder (com.google.inject.multibindings.Multibinder)3 OptionalBinder (com.google.inject.multibindings.OptionalBinder)3 Provider (com.google.inject.Provider)2 MapBinder (com.google.inject.multibindings.MapBinder)2 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)2 Provider (javax.inject.Provider)2