Search in sources :

Example 11 with Key

use of com.google.inject.Key in project guice by google.

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

Example 12 with Key

use of com.google.inject.Key in project guice by google.

the class CheckedProviderTest method testDependencies_Provides.

public void testDependencies_Provides() {
    providesInjector = 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);
            install(ThrowingProviderBinder.forModule(this));
        }

        @SuppressWarnings("unused")
        @CheckedProvides(RemoteProvider.class)
        Foo foo(String s, Integer i, Double d, Long l) {
            return null;
        }
    });
    HasDependencies hasDependencies = (HasDependencies) providesInjector.getBinding(Key.get(remoteProviderOfFoo));
    // RemoteProvider<String> is dependent on the provider method..
    hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    // And the provider method has our real dependencies..
    hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    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);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 13 with Key

use of com.google.inject.Key in project guice by google.

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure: " + key.toString());
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
    }
}
Also used : OutOfScopeException(com.google.inject.OutOfScopeException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.google.inject.Key)

Example 14 with Key

use of com.google.inject.Key in project guice by google.

the class CheckedProviderTest method testDependencies_Bind.

public void testDependencies_Bind() {
    bindInjector = 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).to(DependentRemoteProvider.class);
        }
    });
    HasDependencies hasDependencies = (HasDependencies) bindInjector.getBinding(Key.get(remoteProviderOfFoo));
    hasDependencies = (HasDependencies) bindInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
    // Make sure that that is dependent on DependentRemoteProvider.
    assertEquals(Dependency.get(Key.get(DependentRemoteProvider.class)), Iterables.getOnlyElement(hasDependencies.getDependencies()));
    // And make sure DependentRemoteProvider has the proper dependencies.
    hasDependencies = (HasDependencies) bindInjector.getBinding(DependentRemoteProvider.class);
    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);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 15 with Key

use of com.google.inject.Key in project guice by google.

the class Errors method formatSource.

public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
    String modules = moduleSourceString(elementSource);
    if (source instanceof Dependency) {
        Dependency<?> dependency = (Dependency<?>) source;
        InjectionPoint injectionPoint = dependency.getInjectionPoint();
        if (injectionPoint != null) {
            formatInjectionPoint(formatter, dependency, injectionPoint, elementSource);
        } else {
            formatSource(formatter, dependency.getKey(), elementSource);
        }
    } else if (source instanceof InjectionPoint) {
        formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource);
    } else if (source instanceof Class) {
        formatter.format("  at %s%s%n", StackTraceElements.forType((Class<?>) source), modules);
    } else if (source instanceof Member) {
        formatter.format("  at %s%s%n", StackTraceElements.forMember((Member) source), modules);
    } else if (source instanceof TypeLiteral) {
        formatter.format("  while locating %s%s%n", source, modules);
    } else if (source instanceof Key) {
        Key<?> key = (Key<?>) source;
        formatter.format("  while locating %s%n", convert(key, elementSource));
    } else if (source instanceof Thread) {
        formatter.format("  in thread %s%n", source);
    } else {
        formatter.format("  at %s%s%n", source, modules);
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Aggregations

Key (com.google.inject.Key)106 AbstractModule (com.google.inject.AbstractModule)55 Injector (com.google.inject.Injector)52 Binding (com.google.inject.Binding)36 Module (com.google.inject.Module)20 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)16 Map (java.util.Map)16 HasDependencies (com.google.inject.spi.HasDependencies)14 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 TypeLiteral (com.google.inject.TypeLiteral)12 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)9 PrivateModule (com.google.inject.PrivateModule)8 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)8 Dependency (com.google.inject.spi.Dependency)8 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)8 ImmutableList (com.google.common.collect.ImmutableList)7 InjectionPoint (com.google.inject.spi.InjectionPoint)7