Search in sources :

Example 1 with Dependency

use of com.google.inject.spi.Dependency 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)

Example 2 with Dependency

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

the class ProviderToInternalFactoryAdapter method get.

@Override
public T get() {
    final Errors errors = new Errors();
    try {
        T t = injector.callInContext(new ContextualCallable<T>() {

            @Override
            public T call(InternalContext context) throws ErrorsException {
                Dependency dependency = context.getDependency();
                // binding, we'll fail properly elsewhere in the chain.
                return internalFactory.get(errors, context, dependency, true);
            }
        });
        errors.throwIfNewErrors(0);
        return t;
    } catch (ErrorsException e) {
        throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Dependency(com.google.inject.spi.Dependency)

Example 3 with Dependency

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

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors) throws ErrorsException {
    Key<T> key = dependency.getKey();
    BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
    final InternalFactory<? extends T> internalFactory = binding.getInternalFactory();
    final Object source = binding.getSource();
    return new Provider<T>() {

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

                    @Override
                    public T call(InternalContext context) throws ErrorsException {
                        Dependency previous = context.pushDependency(dependency, source);
                        try {
                            return internalFactory.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 internalFactory.toString();
        }
    };
}
Also used : Dependency(com.google.inject.spi.Dependency) SourceProvider(com.google.inject.internal.util.SourceProvider) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException)

Example 4 with Dependency

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

the class SingleFieldInjector method inject.

@Override
public void inject(Errors errors, InternalContext context, Object o) {
    errors = errors.withSource(dependency);
    Dependency previous = context.pushDependency(dependency, binding.getSource());
    try {
        Object value = binding.getInternalFactory().get(errors, context, dependency, false);
        field.set(o, value);
    } catch (ErrorsException e) {
        errors.withSource(injectionPoint).merge(e.getErrors());
    } catch (IllegalAccessException e) {
        // a security manager is blocking us, we're hosed
        throw new AssertionError(e);
    } finally {
        context.popStateAndSetDependency(previous);
    }
}
Also used : Dependency(com.google.inject.spi.Dependency)

Example 5 with Dependency

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

the class SingleParameterInjector method inject.

T inject(Errors errors, InternalContext context) throws ErrorsException {
    Dependency<T> localDependency = dependency;
    Dependency previous = context.pushDependency(localDependency, source);
    try {
        return factory.get(errors.withSource(localDependency), context, localDependency, false);
    } finally {
        context.popStateAndSetDependency(previous);
    }
}
Also used : Dependency(com.google.inject.spi.Dependency)

Aggregations

Dependency (com.google.inject.spi.Dependency)27 Provider (com.google.inject.Provider)9 InjectionPoint (com.google.inject.spi.InjectionPoint)9 AbstractModule (com.google.inject.AbstractModule)8 HasDependencies (com.google.inject.spi.HasDependencies)8 Injector (com.google.inject.Injector)6 Key (com.google.inject.Key)6 TypeLiteral (com.google.inject.TypeLiteral)5 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 Function (com.google.common.base.Function)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 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