Search in sources :

Example 6 with Dependency

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

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 7 with Dependency

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

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)

Example 8 with Dependency

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

the class CheckedProviderMethodsModule method createProviderMethod.

<T> CheckedProviderMethod<T> createProviderMethod(Binder binder, final Method method, CheckedProvides checkedProvides) {
    @SuppressWarnings("rawtypes") Class<? extends CheckedProvider> throwingProvider = checkedProvides.value();
    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, checkedProvides.scopeExceptions());
}
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 9 with Dependency

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

the class Providers method guicify.

/**
   * Returns a Guice-friendly {@code com.google.inject.Provider} for the given JSR-330 {@code
   * javax.inject.Provider}. The converse method is unnecessary, since Guice providers directly
   * implement the JSR-330 interface.
   *
   * @since 3.0
   */
public static <T> Provider<T> guicify(javax.inject.Provider<T> provider) {
    if (provider instanceof Provider) {
        return (Provider<T>) provider;
    }
    final javax.inject.Provider<T> delegate = checkNotNull(provider, "provider");
    // Ensure that we inject all injection points from the delegate provider.
    Set<InjectionPoint> injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass());
    if (injectionPoints.isEmpty()) {
        return new GuicifiedProvider<T>(delegate);
    } else {
        Set<Dependency<?>> mutableDeps = Sets.newHashSet();
        for (InjectionPoint ip : injectionPoints) {
            mutableDeps.addAll(ip.getDependencies());
        }
        final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
        return new GuicifiedProviderWithDependencies<T>(dependencies, delegate);
    }
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Provider(com.google.inject.Provider)

Example 10 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)

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