Search in sources :

Example 1 with HasDependencies

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

the class MultibinderTest method testMultibinderDependenciesInToolStage.

/**
   * We just want to make sure that multibinder's binding depends on each of its values. We don't
   * really care about the underlying structure of those bindings, which are implementation details.
   */
public void testMultibinderDependenciesInToolStage() {
    Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {

        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toInstance("A");
            multibinder.addBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {
    });
    HasDependencies withDependencies = (HasDependencies) binding;
    InstanceBinding<?> instanceBinding = null;
    LinkedKeyBinding<?> linkedBinding = null;
    // the Key of @Named("b") String=B
    for (Dependency<?> dependency : withDependencies.getDependencies()) {
        Binding<?> b = injector.getBinding(dependency.getKey());
        if (b instanceof InstanceBinding) {
            if (instanceBinding != null) {
                fail("Already have an instance binding of: " + instanceBinding + ", and now want to add: " + b);
            } else {
                instanceBinding = (InstanceBinding) b;
            }
        } else if (b instanceof LinkedKeyBinding) {
            if (linkedBinding != null) {
                fail("Already have a linked binding of: " + linkedBinding + ", and now want to add: " + b);
            } else {
                linkedBinding = (LinkedKeyBinding) b;
            }
        } else {
            fail("Unexpected dependency of: " + dependency);
        }
    }
    assertNotNull(instanceBinding);
    assertNotNull(linkedBinding);
    assertEquals("A", instanceBinding.getInstance());
    assertEquals(Key.get(String.class, Names.named("b")), linkedBinding.getLinkedKey());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule) InstanceBinding(com.google.inject.spi.InstanceBinding) Injector(com.google.inject.Injector) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding)

Example 2 with HasDependencies

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

the class MultibinderTest method testMultibindingProviderDependencies.

public void testMultibindingProviderDependencies() {
    final Annotation setAnn = Names.named("foo");
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
            multibinder.addBinding().toInstance("a");
            multibinder.addBinding().toInstance("b");
        }
    });
    HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
    });
    HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
    });
    // sanity check the size
    assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
    Set<Dependency<?>> expected = Sets.newHashSet();
    for (Dependency<?> dep : setBinding.getDependencies()) {
        Key key = dep.getKey();
        Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
        expected.add(providerDependency);
    }
    assertEquals(expected, providerBinding.getDependencies());
}
Also used : Injector(com.google.inject.Injector) Dependency(com.google.inject.spi.Dependency) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 3 with HasDependencies

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

the class MapBinderTest method recurseForDependencies.

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 4 with HasDependencies

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

the class MapBinderTest method testMultibinderDependenciesInToolStage.

/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */
public void testMultibinderDependenciesInToolStage() {
    Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class);
            mapBinder.addBinding(1).toInstance("A");
            mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {
    });
    HasDependencies withDependencies = (HasDependencies) binding;
    Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {
    };
    assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies());
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Example 5 with HasDependencies

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

the class OptionalBinderTest method testDependencies_default.

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

        @Override
        protected void configure() {
            OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalbinder.setDefault().toInstance("A");
        }
    });
    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("A"), elements);
}
Also used : Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) 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