Search in sources :

Example 26 with Dependency

use of com.google.inject.spi.Dependency in project roboguice by roboguice.

the class CheckedProviderMethodsModule method createProviderMethod.

<T> CheckedProviderMethod<T> createProviderMethod(Binder binder, final Method method, Class<? extends CheckedProvider> throwingProvider) {
    binder = binder.withSource(method);
    Errors errors = new Errors(method);
    // prepare the parameter providers
    List<Dependency<?>> dependencies = Lists.newArrayList();
    List<Provider<?>> parameterProviders = Lists.newArrayList();
    List<TypeLiteral<?>> parameterTypes = typeLiteral.getParameterTypes(method);
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < parameterTypes.size(); i++) {
        Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]);
        if (key.equals(LOGGER_KEY)) {
            // If it was a Logger, change the key to be unique & bind it to a
            // provider that provides a logger with a proper name.
            // This solves issue 482 (returning a new anonymous logger on every call exhausts memory)
            Key<Logger> loggerKey = Key.get(Logger.class, UniqueAnnotations.create());
            binder.bind(loggerKey).toProvider(new LogProvider(method));
            key = loggerKey;
        }
        dependencies.add(Dependency.get(key));
        parameterProviders.add(binder.getProvider(key));
    }
    // Define T as the method's return type.
    @SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
    List<TypeLiteral<?>> exceptionTypes = typeLiteral.getExceptionTypes(method);
    Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
    Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
    for (Message message : errors.getMessages()) {
        binder.addError(message);
    }
    return new CheckedProviderMethod<T>(key, method, delegate, ImmutableSet.copyOf(dependencies), parameterProviders, scopeAnnotation, throwingProvider, exceptionTypes);
}
Also used : Message(com.google.inject.spi.Message) Dependency(com.google.inject.spi.Dependency) Logger(java.util.logging.Logger) Provider(com.google.inject.Provider) Errors(com.google.inject.internal.Errors) TypeLiteral(com.google.inject.TypeLiteral)

Example 27 with Dependency

use of com.google.inject.spi.Dependency in project roboguice by roboguice.

the class ThrowingProviderTest method testDependencies_Bind.

public void testDependencies_Bind() {
    bindInjector = Guice.createInjector(new AbstractModule() {

        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, String.class).to(DependentRemoteProvider.class);
        }
    });
    HasDependencies hasDependencies = (HasDependencies) bindInjector.getBinding(Key.get(remoteProviderOfString));
    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(), new Function<Dependency<?>, Key<?>>() {

        public Key<?> apply(Dependency<?> from) {
            return from.getKey();
        }
    }));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : Function(com.google.common.base.Function) Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 28 with Dependency

use of com.google.inject.spi.Dependency in project roboguice by roboguice.

the class ThrowingProviderTest method testDependencies_Provides.

public void testDependencies_Provides() {
    providesInjector = Guice.createInjector(new AbstractModule() {

        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)
        String foo(String s, Integer i, Double d, Long l) {
            return null;
        }
    });
    HasDependencies hasDependencies = (HasDependencies) providesInjector.getBinding(Key.get(remoteProviderOfString));
    // 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(), new Function<Dependency<?>, Key<?>>() {

        public Key<?> apply(Dependency<?> from) {
            return from.getKey();
        }
    }));
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
Also used : Function(com.google.common.base.Function) Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

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