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;
}
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();
}
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());
}
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);
}
}
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);
}
}
Aggregations