use of com.google.inject.spi.InstanceBinding in project guice by google.
the class MultibinderTest method testBindingKeysFixedOnReturnFromGetElements.
/** Ensure bindings do not rehash their keys once returned from {@link Elements#getElements}. */
public void testBindingKeysFixedOnReturnFromGetElements() {
final List<String> list = Lists.newArrayList();
Module ab = new AbstractModule() {
@Override
protected void configure() {
Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
multibinder.addBinding().toInstance(list);
list.add("A");
list.add("B");
}
};
InstanceBinding<?> binding = Iterables.getOnlyElement(Iterables.filter(Elements.getElements(ab), InstanceBinding.class));
Key<?> keyBefore = binding.getKey();
assertEquals(listOfStrings, keyBefore.getTypeLiteral());
list.add("C");
Key<?> keyAfter = binding.getKey();
assertSame(keyBefore, keyAfter);
}
use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.
the class MultibinderTest method testSetAndMapValueAreDistinctInSpi.
// See issue 670
public void testSetAndMapValueAreDistinctInSpi() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder.newSetBinder(binder(), String.class).addBinding().toInstance("A");
MapBinder.newMapBinder(binder(), String.class, String.class).addBinding("B").toInstance("b");
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("C");
}
});
Collector collector = new Collector();
Binding<Map<String, String>> mapbinding = injector.getBinding(Key.get(mapOfStringString));
mapbinding.acceptTargetVisitor(collector);
assertNotNull(collector.mapbinding);
Binding<Set<String>> setbinding = injector.getBinding(Key.get(setOfString));
setbinding.acceptTargetVisitor(collector);
assertNotNull(collector.setbinding);
Binding<Optional<String>> optionalbinding = injector.getBinding(Key.get(optionalOfString));
optionalbinding.acceptTargetVisitor(collector);
assertNotNull(collector.optionalbinding);
// There should only be three instance bindings for string types
// (but because of the OptionalBinder, there's 2 ProviderInstanceBindings also).
// We also know the InstanceBindings will be in the order: A, b, C because that's
// how we bound them, and binding order is preserved.
List<Binding<String>> bindings = FluentIterable.from(injector.findBindingsByType(stringType)).filter(Predicates.instanceOf(InstanceBinding.class)).toList();
assertEquals(bindings.toString(), 3, bindings.size());
Binding<String> a = bindings.get(0);
Binding<String> b = bindings.get(1);
Binding<String> c = bindings.get(2);
assertEquals("A", ((InstanceBinding<String>) a).getInstance());
assertEquals("b", ((InstanceBinding<String>) b).getInstance());
assertEquals("C", ((InstanceBinding<String>) c).getInstance());
// Make sure the correct elements belong to their own sets.
assertFalse(collector.mapbinding.containsElement(a));
assertTrue(collector.mapbinding.containsElement(b));
assertFalse(collector.mapbinding.containsElement(c));
assertTrue(collector.setbinding.containsElement(a));
assertFalse(collector.setbinding.containsElement(b));
assertFalse(collector.setbinding.containsElement(c));
assertFalse(collector.optionalbinding.containsElement(a));
assertFalse(collector.optionalbinding.containsElement(b));
assertTrue(collector.optionalbinding.containsElement(c));
}
use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.
the class MultibinderTest method testMultibinderDependenciesInToolStage.
/**
* We just want to make sure that multibinder's binding depends on each of its values. We don't
* really care about the underlying structure of those bindings, which are implementation details.
*/
public void testMultibinderDependenciesInToolStage() {
Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() {
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
multibinder.addBinding().toInstance("A");
multibinder.addBinding().to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {
});
HasDependencies withDependencies = (HasDependencies) binding;
InstanceBinding<?> instanceBinding = null;
LinkedKeyBinding<?> linkedBinding = null;
// the Key of @Named("b") String=B
for (Dependency<?> dependency : withDependencies.getDependencies()) {
Binding<?> b = injector.getBinding(dependency.getKey());
if (b instanceof InstanceBinding) {
if (instanceBinding != null) {
fail("Already have an instance binding of: " + instanceBinding + ", and now want to add: " + b);
} else {
instanceBinding = (InstanceBinding) b;
}
} else if (b instanceof LinkedKeyBinding) {
if (linkedBinding != null) {
fail("Already have a linked binding of: " + linkedBinding + ", and now want to add: " + b);
} else {
linkedBinding = (LinkedKeyBinding) b;
}
} else {
fail("Unexpected dependency of: " + dependency);
}
}
assertNotNull(instanceBinding);
assertNotNull(linkedBinding);
assertEquals("A", instanceBinding.getInstance());
assertEquals(Key.get(String.class, Names.named("b")), linkedBinding.getLinkedKey());
}
use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.
the class MapBinderTest method recurseForDependencies.
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) {
Set<String> elements = Sets.newHashSet();
for (Dependency<?> dependency : hasDependencies.getDependencies()) {
Binding<?> binding = injector.getBinding(dependency.getKey());
HasDependencies deps = (HasDependencies) binding;
if (binding instanceof InstanceBinding) {
elements.add((String) ((InstanceBinding<?>) binding).getInstance());
} else {
elements.addAll(recurseForDependencies(injector, deps));
}
}
return elements;
}
use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.
the class OptionalBinderTest method recurseForDependencies.
@SuppressWarnings("rawtypes")
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) {
Set<String> elements = Sets.newHashSet();
for (Dependency<?> dependency : hasDependencies.getDependencies()) {
Binding<?> binding = injector.getBinding(dependency.getKey());
HasDependencies deps = (HasDependencies) binding;
if (binding instanceof InstanceBinding) {
elements.add((String) ((InstanceBinding) binding).getInstance());
} else {
elements.addAll(recurseForDependencies(injector, deps));
}
}
return elements;
}
Aggregations