use of com.google.inject.spi.HasDependencies in project guice by google.
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;
}
use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.
the class OptionalBinderTest method testDependencies_both.
public void testDependencies_both() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
optionalbinder.setDefault().toInstance("A");
optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<String> binding = injector.getBinding(Key.get(String.class));
HasDependencies withDependencies = (HasDependencies) binding;
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("B"), elements);
}
use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.
the class MapBinderTest method testMultibinderDependencies.
/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */
public void testMultibinderDependencies() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class);
mapBinder.addBinding(1).toInstance("A");
mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b")));
bindConstant().annotatedWith(Names.named("b")).to("B");
}
});
Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {
});
HasDependencies withDependencies = (HasDependencies) binding;
Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {
};
assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies());
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("A", "B"), elements);
}
use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.
the class MultibinderTest method testMultibinderDependencies.
/**
* 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 testMultibinderDependencies() {
Injector injector = Guice.createInjector(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;
Set<String> elements = Sets.newHashSet();
for (Dependency<?> dependency : withDependencies.getDependencies()) {
elements.add((String) injector.getInstance(dependency.getKey()));
}
assertEquals(ImmutableSet.of("A", "B"), elements);
}
use of com.google.inject.spi.HasDependencies in project roboguice by roboguice.
the class CheckedProviderTest method testDependencies_Cxtor.
public void testDependencies_Cxtor() {
cxtorInjector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("Foo");
bind(Integer.class).toInstance(5);
bind(Double.class).toInstance(5d);
bind(Long.class).toInstance(5L);
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(DependentMockFoo.class);
}
});
Key<?> key = Key.get(remoteProviderOfFoo);
// RemoteProvider<String> is dependent on Result.
HasDependencies hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
assertEquals(Result.class, key.getTypeLiteral().getRawType());
// Result is dependent on the fake CheckedProvider impl
hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
assertTrue(CheckedProvider.class.isAssignableFrom(key.getTypeLiteral().getRawType()));
// And the CheckedProvider is dependent on DependentMockFoo...
hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
key = Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey();
assertEquals(DependentMockFoo.class, key.getTypeLiteral().getRawType());
// And DependentMockFoo is dependent on the goods.
hasDependencies = (HasDependencies) cxtorInjector.getBinding(key);
Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Aggregations