Search in sources :

Example 21 with Dependency

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

the class ThrowingProviderTest 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)
        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<?>>() {

        @Override
        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 22 with Dependency

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

the class ThrowingProviderTest 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, 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<?>>() {

        @Override
        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 23 with Dependency

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

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Key<T> key, Errors errors) throws ErrorsException {
    final BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
    final Dependency<T> dependency = Dependency.get(key);
    return new Provider<T>() {

        public T get() {
            final Errors errors = new Errors(dependency);
            try {
                T t = callInContext(new ContextualCallable<T>() {

                    public T call(InternalContext context) throws ErrorsException {
                        Dependency previous = context.pushDependency(dependency, binding.getSource());
                        try {
                            return binding.getInternalFactory().get(errors, context, dependency, false);
                        } finally {
                            context.popStateAndSetDependency(previous);
                        }
                    }
                });
                errors.throwIfNewErrors(0);
                return t;
            } catch (ErrorsException e) {
                throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
            }
        }

        @Override
        public String toString() {
            return binding.getInternalFactory().toString();
        }
    };
}
Also used : ProvisionException(com.google.inject.ProvisionException) Dependency(com.google.inject.spi.Dependency) SourceProvider(com.google.inject.internal.util.SourceProvider) Provider(com.google.inject.Provider)

Example 24 with Dependency

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

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 {
        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)

Example 25 with Dependency

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

the class Jsr330Test method testGuicifyWithDependencies.

public void testGuicifyWithDependencies() {
    Provider<String> jsr330Provider = new Provider<String>() {

        @Inject
        double d;

        int i;

        @Inject
        void injectMe(int i) {
            this.i = i;
        }

        public String get() {
            return d + "-" + i;
        }
    };
    final com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider);
    assertTrue(guicified instanceof HasDependencies);
    Set<Dependency<?>> actual = ((HasDependencies) guicified).getDependencies();
    validateDependencies(actual, jsr330Provider.getClass());
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toProvider(guicified);
            bind(int.class).toInstance(1);
            bind(double.class).toInstance(2.0d);
        }
    });
    Binding<String> binding = injector.getBinding(String.class);
    assertEquals("2.0-1", binding.getProvider().get());
    validateDependencies(actual, jsr330Provider.getClass());
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) Provider(javax.inject.Provider) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector)

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