Search in sources :

Example 6 with Element

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

the class ServletScopesTest method indexBindings.

private ImmutableMap<Key<?>, Binding<?>> indexBindings(Iterable<Element> elements) {
    ImmutableMap.Builder<Key<?>, Binding<?>> builder = ImmutableMap.builder();
    for (Element element : elements) {
        if (element instanceof Binding) {
            Binding<?> binding = (Binding<?>) element;
            builder.put(binding.getKey(), binding);
        } else if (element instanceof PrivateElements) {
            PrivateElements privateElements = (PrivateElements) element;
            Map<Key<?>, Binding<?>> privateBindings = indexBindings(privateElements.getElements());
            for (Key<?> exposed : privateElements.getExposedKeys()) {
                builder.put(exposed, privateBindings.get(exposed));
            }
        }
    }
    return builder.build();
}
Also used : Binding(com.google.inject.Binding) PrivateElements(com.google.inject.spi.PrivateElements) Element(com.google.inject.spi.Element) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) Key(com.google.inject.Key)

Example 7 with Element

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

the class ExtensionSpiTest method testSpiOnElements.

public final void testSpiOnElements() {
    ServletSpiVisitor visitor = new ServletSpiVisitor(false);
    int count = 0;
    for (Element element : Elements.getElements(new Module())) {
        if (element instanceof Binding) {
            assertEquals(count++, ((Binding) element).acceptTargetVisitor(visitor));
        }
    }
    validateVisitor(visitor);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element)

Example 8 with Element

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

the class ExtensionSpiTest method testSpiOnElements.

public final void testSpiOnElements() throws Exception {
    AssistedInjectSpiVisitor visitor = new AssistedInjectSpiVisitor();
    Integer count = 0;
    for (Element element : Elements.getElements(new Module())) {
        if (element instanceof Binding) {
            assertEquals(count++, ((Binding<?>) element).acceptTargetVisitor(visitor));
        }
    }
    validateVisitor(visitor);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule)

Example 9 with Element

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

the class FactoryModuleBuilderTest method testFactoryBindingDependencies.

public void testFactoryBindingDependencies() {
    // validate dependencies work in all stages & as a raw element,
    // and that dependencies work for methods, fields, constructors,
    // and for @AssistedInject constructors too.
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(42);
            bind(Double.class).toInstance(4.2d);
            bind(Float.class).toInstance(4.2f);
            bind(String.class).annotatedWith(named("dog")).toInstance("dog");
            bind(String.class).annotatedWith(named("cat1")).toInstance("cat1");
            bind(String.class).annotatedWith(named("cat2")).toInstance("cat2");
            bind(String.class).annotatedWith(named("cat3")).toInstance("cat3");
            bind(String.class).annotatedWith(named("arbitrary")).toInstance("fail!");
            install(new FactoryModuleBuilder().implement(Animal.class, Dog.class).build(AnimalHouse.class));
        }
    };
    Set<Key<?>> expectedKeys = ImmutableSet.<Key<?>>of(Key.get(Integer.class), Key.get(Double.class), Key.get(Float.class), Key.get(String.class, named("dog")), Key.get(String.class, named("cat1")), Key.get(String.class, named("cat2")), Key.get(String.class, named("cat3")));
    Injector injector = Guice.createInjector(module);
    validateDependencies(expectedKeys, injector.getBinding(AnimalHouse.class));
    injector = Guice.createInjector(Stage.TOOL, module);
    validateDependencies(expectedKeys, injector.getBinding(AnimalHouse.class));
    List<Element> elements = Elements.getElements(module);
    boolean found = false;
    for (Element element : elements) {
        if (element instanceof Binding) {
            Binding<?> binding = (Binding<?>) element;
            if (binding.getKey().equals(Key.get(AnimalHouse.class))) {
                found = true;
                validateDependencies(expectedKeys, binding);
                break;
            }
        }
    }
    assertTrue(found);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key)

Example 10 with Element

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

the class PrivateElementsImpl method applyTo.

@Override
public void applyTo(Binder binder) {
    PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
    for (Element element : getElements()) {
        element.applyTo(privateBinder);
    }
    // ensure exposedKeysToSources is populated
    getExposedKeys();
    for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
        privateBinder.withSource(entry.getValue()).expose(entry.getKey());
    }
}
Also used : PrivateBinder(com.google.inject.PrivateBinder) Element(com.google.inject.spi.Element) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Key(com.google.inject.Key)

Aggregations

Element (com.google.inject.spi.Element)29 Binding (com.google.inject.Binding)18 Key (com.google.inject.Key)16 AbstractModule (com.google.inject.AbstractModule)13 Injector (com.google.inject.Injector)10 Module (com.google.inject.Module)10 ImmutableMap (com.google.common.collect.ImmutableMap)7 Map (java.util.Map)7 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)6 InstanceBinding (com.google.inject.spi.InstanceBinding)6 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)6 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)6 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)6 ImmutableList (com.google.common.collect.ImmutableList)4 PrivateModule (com.google.inject.PrivateModule)4 PrivateElements (com.google.inject.spi.PrivateElements)4 ProviderLookup (com.google.inject.spi.ProviderLookup)4 List (java.util.List)4 IndexedBinding (com.google.inject.internal.Indexer.IndexedBinding)3 IndexedBinding (com.google.inject.multibindings.Indexer.IndexedBinding)3