Search in sources :

Example 31 with Binding

use of com.google.inject.Binding in project guice by google.

the class MapBinderTest method testGetEntriesWithDuplicateKeys.

@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateKeys() {
    // Set up the module
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            mapBinder.addBinding("A").toInstance("a1");
            mapBinder.addBinding("A").toInstance("a2");
            mapBinder.permitDuplicates();
        }
    };
    // Get the MapBinderBinding
    List<com.google.inject.spi.Element> elements = Elements.getElements(module);
    MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
    // Execute the call to getEntries
    List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
    // Assert on the results
    Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
    assertEquals("A", firstEntry.getKey());
    Binding<?> firstBinding = firstEntry.getValue();
    assertEquals("a1", ((InstanceBinding) firstBinding).getInstance());
    Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
    assertEquals("A", secondEntry.getKey());
    Binding<?> secondBinding = secondEntry.getValue();
    assertEquals("a2", ((InstanceBinding) secondBinding).getInstance());
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) AbstractModule(com.google.inject.AbstractModule) ProviderMapEntry(com.google.inject.internal.RealMapBinder.ProviderMapEntry) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Map(java.util.Map) HashMap(java.util.HashMap)

Example 32 with Binding

use of com.google.inject.Binding in project guice by google.

the class MapBinderTest method testTwoMapBindersAreDistinct.

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

        @Override
        protected void configure() {
            MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("A").toInstance("a");
            MapBinder.newMapBinder(binder(), Integer.class, String.class).addBinding(1).toInstance("b");
        }
    });
    Collector collector = new Collector();
    Binding<Map<String, String>> map1 = injector.getBinding(Key.get(mapOfString));
    map1.acceptTargetVisitor(collector);
    assertNotNull(collector.mapbinding);
    MapBinderBinding<?> map1Binding = collector.mapbinding;
    Binding<Map<Integer, String>> map2 = injector.getBinding(Key.get(mapOfIntString));
    map2.acceptTargetVisitor(collector);
    assertNotNull(collector.mapbinding);
    MapBinderBinding<?> map2Binding = collector.mapbinding;
    List<Binding<String>> bindings = injector.findBindingsByType(stringType);
    assertEquals("should have two elements: " + bindings, 2, bindings.size());
    Binding<String> a = bindings.get(0);
    Binding<String> b = bindings.get(1);
    assertEquals("a", ((InstanceBinding<String>) a).getInstance());
    assertEquals("b", ((InstanceBinding<String>) b).getInstance());
    // Make sure the correct elements belong to their own sets.
    assertTrue(map1Binding.containsElement(a));
    assertFalse(map1Binding.containsElement(b));
    assertFalse(map2Binding.containsElement(a));
    assertTrue(map2Binding.containsElement(b));
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Map(java.util.Map) HashMap(java.util.HashMap)

Example 33 with Binding

use of com.google.inject.Binding in project guice by google.

the class MapBinderTest method testGetEntriesWithDuplicateValues.

@SuppressWarnings("rawtypes")
public void testGetEntriesWithDuplicateValues() {
    // Set up the module
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            mapBinder.addBinding("A").toInstance("a");
            mapBinder.addBinding("A").toInstance("a");
        }
    };
    // Get the MapBinderBinding
    List<com.google.inject.spi.Element> elements = Elements.getElements(module);
    MapBinderBinding<?> mapBinderBinding = getMapBinderBinding(elements);
    // Execute the call to getEntries
    List<Map.Entry<?, Binding<?>>> mapEntries = mapBinderBinding.getEntries(elements);
    // Assert on the results
    Map.Entry<?, Binding<?>> firstEntry = mapEntries.get(0);
    assertEquals("A", firstEntry.getKey());
    Binding<?> firstBinding = firstEntry.getValue();
    assertEquals("a", ((InstanceBinding) firstBinding).getInstance());
    Map.Entry<?, Binding<?>> secondEntry = mapEntries.get(1);
    assertEquals("A", secondEntry.getKey());
    Binding<?> secondBinding = secondEntry.getValue();
    assertEquals("a", ((InstanceBinding) secondBinding).getInstance());
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) AbstractModule(com.google.inject.AbstractModule) ProviderMapEntry(com.google.inject.internal.RealMapBinder.ProviderMapEntry) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Map(java.util.Map) HashMap(java.util.HashMap)

Example 34 with Binding

use of com.google.inject.Binding in project guice by google.

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) Key(com.google.inject.Key) SpiUtils.providerKey(com.google.inject.internal.SpiUtils.providerKey) AbstractModule(com.google.inject.AbstractModule)

Example 35 with Binding

use of com.google.inject.Binding in project camel by apache.

the class Main method getCamelContextMap.

protected Map<String, CamelContext> getCamelContextMap() {
    Map<String, CamelContext> answer = Maps.newHashMap();
    if (injector != null) {
        Set<Map.Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
        for (Map.Entry<Key<?>, Binding<?>> entry : entries) {
            Key<?> key = entry.getKey();
            Class<?> keyType = Injectors.getKeyType(key);
            if (keyType != null && CamelContext.class.isAssignableFrom(keyType)) {
                Binding<?> binding = entry.getValue();
                Object value = binding.getProvider().get();
                if (value != null) {
                    CamelContext castValue = CamelContext.class.cast(value);
                    answer.put(key.toString(), castValue);
                }
            }
        }
    }
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext) Binding(com.google.inject.Binding) Map(java.util.Map) Key(com.google.inject.Key)

Aggregations

Binding (com.google.inject.Binding)92 Injector (com.google.inject.Injector)58 Key (com.google.inject.Key)36 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Module (com.google.inject.Module)18 Element (com.google.inject.spi.Element)18 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10