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));
}
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);
}
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);
}
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)));
}
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;
}
}
Aggregations