Search in sources :

Example 21 with HasDependencies

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

the class MapBinderTest method testMultibinderDependencies.

/** Check that the dependencies are correct. */
public void testMultibinderDependencies() {
    Injector injector = Guice.createInjector(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;
    Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();
    // We expect two dependencies, because the dependencies are annotated with
    // Element, which has a uniqueId, it's difficult to directly compare them.
    // Instead we will manually compare all the fields except the uniqueId
    assertEquals(2, actualDependencies.size());
    for (Dependency<?> dependency : actualDependencies) {
        Key<?> key = dependency.getKey();
        assertEquals(new TypeLiteral<String>() {
        }, key.getTypeLiteral());
        Annotation annotation = dependency.getKey().getAnnotation();
        assertTrue(annotation instanceof Element);
        Element element = (Element) annotation;
        assertEquals("", element.setName());
        assertEquals(Element.Type.MAPBINDER, element.type());
        assertEquals("java.lang.Integer", element.keyType());
    }
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("A", "B"), elements);
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with HasDependencies

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

the class MultibinderTest method testMultibinderDependencies.

/**
   * 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 testMultibinderDependencies() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        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;
    Set<String> elements = Sets.newHashSet();
    for (Dependency<?> dependency : withDependencies.getDependencies()) {
        elements.add((String) injector.getInstance(dependency.getKey()));
    }
    assertEquals(ImmutableSet.of("A", "B"), elements);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 23 with HasDependencies

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

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 : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) InstanceBinding(com.google.inject.spi.InstanceBinding) HasDependencies(com.google.inject.spi.HasDependencies)

Example 24 with HasDependencies

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

the class FactoryProviderTest method testFactoryBindingDependencies.

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

        @Override
        protected void configure() {
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    Binding<?> binding = injector.getBinding(ColoredCarFactory.class);
    HasDependencies hasDependencies = (HasDependencies) binding;
    assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(double.class))), hasDependencies.getDependencies());
}
Also used : Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 25 with HasDependencies

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

the class ThrowingProviderTest 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)
        String foo(String s, Integer i, Double d, Long l) {
            return null;
        }
    });
    HasDependencies hasDependencies = (HasDependencies) providesInjector.getBinding(Key.get(remoteProviderOfString));
    // 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(), new Function<Dependency<?>, Key<?>>() {

        @Override
        public Key<?> apply(Dependency<?> from) {
            return from.getKey();
        }
    }));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : Function(com.google.common.base.Function) Dependency(com.google.inject.spi.Dependency) 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