Search in sources :

Example 21 with Element

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

the class MultibinderTest method testConcurrentMutation_bindingsSameAtInjectorCreation.

/*
   * Verify through gratuitous mutation that key hashCode snapshots and whatnot happen at the right
   * times, by binding two lists that compare equal at injector creation, but are different when the
   * module is configured *and* when the set is instantiated.
   */
public void testConcurrentMutation_bindingsSameAtInjectorCreation() {
    // We initially bind two distinct lists
    final List<String> list1 = Lists.newArrayList("A");
    final List<String> list2 = Lists.newArrayList("B");
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
            multibinder.addBinding().toInstance(list1);
            multibinder.addBinding().toInstance(list2);
        }
    };
    List<Element> elements = Elements.getElements(module);
    // Now we change the lists so they compare equal, and create the injector.
    list1.add(1, "B");
    list2.add(0, "A");
    Injector injector = Guice.createInjector(Elements.getModule(elements));
    // Now we change the lists again so they are once more different, and create the set.
    list1.remove("A");
    list2.remove("B");
    Set<List<String>> set = injector.getInstance(Key.get(setOfListOfStrings));
    // The set will contain just one of the two lists.
    // (In fact, it will be the first one we bound, but we don't promise that, so we won't test it.)
    assertTrue(ImmutableSet.of(ImmutableList.of("A")).equals(set) || ImmutableSet.of(ImmutableList.of("B")).equals(set));
}
Also used : Injector(com.google.inject.Injector) Element(com.google.inject.spi.Element) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 22 with Element

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

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 23 with Element

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

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 24 with Element

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

the class ServletScopesTest method testIsRequestScopedPositive.

public void testIsRequestScopedPositive() {
    final Key<String> a = Key.get(String.class, named("A"));
    final Key<String> b = Key.get(String.class, named("B"));
    final Key<String> c = Key.get(String.class, named("C"));
    final Key<String> d = Key.get(String.class, named("D"));
    final Key<Object> e = Key.get(Object.class, named("E"));
    final Key<String> f = Key.get(String.class, named("F"));
    final Key<String> g = Key.get(String.class, named("G"));
    Module requestScopedBindings = new AbstractModule() {

        @Override
        protected void configure() {
            bind(a).to(b);
            bind(b).to(c);
            bind(c).toProvider(Providers.of("c")).in(ServletScopes.REQUEST);
            bind(d).toProvider(Providers.of("d")).in(RequestScoped.class);
            bind(e).to(AnnotatedRequestScopedClass.class);
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(f).toProvider(Providers.of("f")).in(RequestScoped.class);
                    expose(f);
                }
            });
        }

        @Provides
        @Named("G")
        @RequestScoped
        String provideG() {
            return "g";
        }
    };
    // we know the module contains only bindings
    @SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
    ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
    // linked bindings are not followed by modules
    assertFalse(ServletScopes.isRequestScoped(map.get(a)));
    assertFalse(ServletScopes.isRequestScoped(map.get(b)));
    assertTrue(ServletScopes.isRequestScoped(map.get(c)));
    assertTrue(ServletScopes.isRequestScoped(map.get(d)));
    // annotated classes are not followed by modules
    assertFalse(ServletScopes.isRequestScoped(map.get(e)));
    assertTrue(ServletScopes.isRequestScoped(map.get(f)));
    assertTrue(ServletScopes.isRequestScoped(map.get(g)));
    Injector injector = Guice.createInjector(requestScopedBindings, new ServletModule());
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(a)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(b)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(c)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(d)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(e)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(f)));
    assertTrue(ServletScopes.isRequestScoped(injector.getBinding(g)));
}
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) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) Key(com.google.inject.Key)

Example 25 with Element

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

the class AbstractProcessor method process.

public void process(InjectorImpl injector, List<Element> elements) {
    Errors errorsAnyElement = this.errors;
    this.injector = injector;
    try {
        for (Iterator<Element> i = elements.iterator(); i.hasNext(); ) {
            Element element = i.next();
            this.errors = errorsAnyElement.withSource(element.getSource());
            Boolean allDone = element.acceptVisitor(this);
            if (allDone) {
                i.remove();
            }
        }
    } finally {
        this.errors = errorsAnyElement;
        this.injector = null;
    }
}
Also used : Element(com.google.inject.spi.Element)

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