use of com.google.inject.spi.Dependency in project guice by google.
the class MapBinderTest method testMultibinderDependenciesInToolStage.
/** Check that the dependencies are correct in the Tool Stage. */
public void testMultibinderDependenciesInToolStage() {
Injector injector = Guice.createInjector(Stage.TOOL, 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;
Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();
// We expect two dependencies, because the dependencies are annotated with
// Element, which has a uniqueId, it's difficult to directly compare them.
// Instead we will manually compare all the fields except the uniqueId
assertEquals(2, actualDependencies.size());
for (Dependency<?> dependency : actualDependencies) {
Key<?> key = dependency.getKey();
assertEquals(new TypeLiteral<String>() {
}, key.getTypeLiteral());
Annotation annotation = dependency.getKey().getAnnotation();
assertTrue(annotation instanceof Element);
Element element = (Element) annotation;
assertEquals("", element.setName());
assertEquals(Element.Type.MAPBINDER, element.type());
assertEquals("java.lang.Integer", element.keyType());
}
}
use of com.google.inject.spi.Dependency in project guice by google.
the class MultibinderTest method testMultibindingProviderDependencies.
public void testMultibindingProviderDependencies() {
final Annotation setAnn = Names.named("foo");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
multibinder.addBinding().toInstance("a");
multibinder.addBinding().toInstance("b");
}
});
HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
});
HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
});
// sanity check the size
assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
Set<Dependency<?>> expected = Sets.newHashSet();
for (Dependency<?> dep : setBinding.getDependencies()) {
Key key = dep.getKey();
Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
expected.add(providerDependency);
}
assertEquals(expected, providerBinding.getDependencies());
}
use of com.google.inject.spi.Dependency in project guice by google.
the class InjectorImpl method cleanup.
/**
* Iterates through the binding's dependencies to clean up any stray bindings that were leftover
* from a failed JIT binding. This is required because the bindings are eagerly & optimistically
* added to allow circular dependency support, so dependencies may pass where they should have
* failed.
*/
private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) {
boolean bindingFailed = false;
Set<Dependency<?>> deps = getInternalDependencies(binding);
for (Dependency dep : deps) {
Key<?> depKey = dep.getKey();
InjectionPoint ip = dep.getInjectionPoint();
if (encountered.add(depKey)) {
// only check if we haven't looked at this key yet
BindingImpl depBinding = jitBindings.get(depKey);
if (depBinding != null) {
// if the binding still exists, validate
// if children fail, we fail
boolean failed = cleanup(depBinding, encountered);
if (depBinding instanceof ConstructorBindingImpl) {
ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding;
ip = ctorBinding.getInternalConstructor();
if (!ctorBinding.isInitialized()) {
failed = true;
}
}
if (failed) {
removeFailedJitBinding(depBinding, ip);
bindingFailed = true;
}
} else if (state.getExplicitBinding(depKey) == null) {
// ignore keys if they were explicitly bound, but if neither JIT
// nor explicit, it's also invalid & should let parent know.
bindingFailed = true;
}
}
}
return bindingFailed;
}
use of com.google.inject.spi.Dependency in project guice by google.
the class MapBinderTest method testMultibinderDependencies.
/** Check that the dependencies are correct. */
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;
Set<Dependency<?>> actualDependencies = withDependencies.getDependencies();
// We expect two dependencies, because the dependencies are annotated with
// Element, which has a uniqueId, it's difficult to directly compare them.
// Instead we will manually compare all the fields except the uniqueId
assertEquals(2, actualDependencies.size());
for (Dependency<?> dependency : actualDependencies) {
Key<?> key = dependency.getKey();
assertEquals(new TypeLiteral<String>() {
}, key.getTypeLiteral());
Annotation annotation = dependency.getKey().getAnnotation();
assertTrue(annotation instanceof Element);
Element element = (Element) annotation;
assertEquals("", element.setName());
assertEquals(Element.Type.MAPBINDER, element.type());
assertEquals("java.lang.Integer", element.keyType());
}
Set<String> elements = Sets.newHashSet();
elements.addAll(recurseForDependencies(injector, withDependencies));
assertEquals(ImmutableSet.of("A", "B"), elements);
}
use of com.google.inject.spi.Dependency in project guice by google.
the class FactoryProvider2 method getDependencies.
/** Calculates all dependencies required by the implementation and constructor. */
private Set<Dependency<?>> getDependencies(InjectionPoint ctorPoint, TypeLiteral<?> implementation) {
ImmutableSet.Builder<Dependency<?>> builder = ImmutableSet.builder();
builder.addAll(ctorPoint.getDependencies());
if (!implementation.getRawType().isInterface()) {
for (InjectionPoint ip : InjectionPoint.forInstanceMethodsAndFields(implementation)) {
builder.addAll(ip.getDependencies());
}
}
return builder.build();
}
Aggregations