Search in sources :

Example 16 with Dependency

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());
    }
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 17 with Dependency

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());
}
Also used : Multibinder(com.google.inject.multibindings.Multibinder) Injector(com.google.inject.Injector) Dependency(com.google.inject.spi.Dependency) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 18 with Dependency

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;
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency)

Example 19 with Dependency

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);
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with Dependency

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency)

Aggregations

Dependency (com.google.inject.spi.Dependency)28 AbstractModule (com.google.inject.AbstractModule)10 HasDependencies (com.google.inject.spi.HasDependencies)10 Provider (com.google.inject.Provider)9 Key (com.google.inject.Key)8 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Injector (com.google.inject.Injector)6 TypeLiteral (com.google.inject.TypeLiteral)5 Function (com.google.common.base.Function)4 BindingAnnotation (com.google.inject.BindingAnnotation)4 ProvisionException (com.google.inject.ProvisionException)4 Annotation (java.lang.annotation.Annotation)4 Message (com.google.inject.spi.Message)3 Logger (java.util.logging.Logger)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Errors (com.google.inject.internal.Errors)2 SourceProvider (com.google.inject.internal.util.SourceProvider)2 MapBinder (com.google.inject.multibindings.MapBinder)2 Member (java.lang.reflect.Member)2 HashMap (java.util.HashMap)2