Search in sources :

Example 41 with Key

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

the class SpiUtils method setModuleTest.

@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, Key<Collection<Provider<T>>> collectionOfProvidersKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
    List<BindResult> bindResults = Lists.newArrayList(results);
    List<Element> elements = Elements.getElements(modules);
    Visitor<T> visitor = new Visitor<T>();
    MultibinderBinding<Set<T>> multibinder = null;
    for (Element element : elements) {
        if (element instanceof Binding && ((Binding) element).getKey().equals(setKey)) {
            multibinder = (MultibinderBinding<Set<T>>) ((Binding) element).acceptTargetVisitor(visitor);
            break;
        }
    }
    assertNotNull(multibinder);
    assertEquals(elementType, multibinder.getElementTypeLiteral());
    List<Object> otherMultibinders = Lists.newArrayList();
    Set<Element> otherContains = new HashSet<Element>();
    List<Element> otherElements = Lists.newArrayList();
    int duplicates = 0;
    Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
    Indexer indexer = new Indexer(null);
    boolean collectionOfProvidersMatch = false;
    for (Element element : elements) {
        boolean contains = multibinder.containsElement(element);
        if (!contains) {
            otherElements.add(element);
        }
        boolean matched = false;
        Key key = null;
        if (element instanceof Binding) {
            Binding binding = (Binding) element;
            if (indexer.isIndexable(binding) && !setOfIndexed.add((IndexedBinding) binding.acceptTargetVisitor(indexer))) {
                duplicates++;
            }
            key = binding.getKey();
            Object visited = binding.acceptTargetVisitor(visitor);
            if (visited != null) {
                matched = true;
                if (visited.equals(multibinder)) {
                    assertTrue(contains);
                } else {
                    otherMultibinders.add(visited);
                }
            }
        }
        if (collectionOfProvidersKey.equals(key)) {
            assertTrue(contains);
            assertFalse(matched);
            collectionOfProvidersMatch = true;
        } else if (!matched && contains) {
            otherContains.add(element);
        }
    }
    if (allowDuplicates) {
        assertEquals("wrong contained elements: " + otherContains, bindResults.size() + 1 + duplicates, otherContains.size());
    } else {
        assertEquals("wrong contained elements: " + otherContains, bindResults.size() + duplicates, otherContains.size());
    }
    assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
    assertTrue(collectionOfProvidersMatch);
    // Validate that we can construct an injector out of the remaining bindings.
    Guice.createInjector(Elements.getModule(otherElements));
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) IndexedBinding(com.google.inject.multibindings.Indexer.IndexedBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) ProviderKeyBinding(com.google.inject.spi.ProviderKeyBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) IndexedBinding(com.google.inject.multibindings.Indexer.IndexedBinding) Element(com.google.inject.spi.Element) Key(com.google.inject.Key) HashSet(java.util.HashSet)

Example 42 with Key

use of com.google.inject.Key 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 43 with Key

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

the class MultibinderTest method testKeyHashCodesFixedAtInjectionTime.

/**
   * Ensure key hash codes are fixed at injection time, not binding time.
   */
public void testKeyHashCodesFixedAtInjectionTime() {
    Module ab = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
            List<String> list = Lists.newArrayList();
            multibinder.addBinding().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    Injector injector = Guice.createInjector(ab);
    for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        Key<?> bindingKey = entry.getKey();
        Key<?> clonedKey;
        if (bindingKey.getAnnotation() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
        } else if (bindingKey.getAnnotationType() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
        } else {
            clonedKey = Key.get(bindingKey.getTypeLiteral());
        }
        assertEquals(bindingKey, clonedKey);
        assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
    }
}
Also used : Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) Injector(com.google.inject.Injector) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 44 with Key

use of com.google.inject.Key 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 45 with Key

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

the class OptionalBinderTest method testKeyHashCodesFixedAtInjectionTime.

/**
   * Ensure key hash codes are fixed at injection time, not binding time.
   */
public void testKeyHashCodesFixedAtInjectionTime() {
    Module m = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
            List<String> list = Lists.newArrayList();
            b.setDefault().toInstance(list);
            b.setBinding().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    Injector injector = Guice.createInjector(m);
    for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        Key<?> bindingKey = entry.getKey();
        Key<?> clonedKey;
        if (bindingKey.getAnnotation() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
        } else if (bindingKey.getAnnotationType() != null) {
            clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
        } else {
            clonedKey = Key.get(bindingKey.getTypeLiteral());
        }
        assertEquals(bindingKey, clonedKey);
        assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
    }
}
Also used : Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) Injector(com.google.inject.Injector) List(java.util.List) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) SpiUtils.providerKey(com.google.inject.multibindings.SpiUtils.providerKey) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Key (com.google.inject.Key)106 AbstractModule (com.google.inject.AbstractModule)55 Injector (com.google.inject.Injector)52 Binding (com.google.inject.Binding)36 Module (com.google.inject.Module)20 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)16 Map (java.util.Map)16 HasDependencies (com.google.inject.spi.HasDependencies)14 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 TypeLiteral (com.google.inject.TypeLiteral)12 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)9 PrivateModule (com.google.inject.PrivateModule)8 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)8 Dependency (com.google.inject.spi.Dependency)8 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)8 ImmutableList (com.google.common.collect.ImmutableList)7 InjectionPoint (com.google.inject.spi.InjectionPoint)7