options = optionsProcessor.getOptions(stage, options); InjectorImpl injector = new InjectorImpl(parent, state, options); if (privateElements != null) { privateElements.initInjector(injector);
Set<Dependency<?>> deps = getInternalDependencies(binding); for (Dependency dep : deps) { Key<?> depKey = dep.getKey(); BindingImpl depBinding = jitBindings.get(depKey); if (depBinding != null) { // if the binding still exists, validate boolean failed = cleanup(depBinding, encountered); // if children fail, we fail if (depBinding instanceof ConstructorBindingImpl) { ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding; removeFailedJitBinding(depBinding, ip); bindingFailed = true;
if (isProvider(key)) { BindingImpl<T> binding = (BindingImpl<T>) createProviderBinding((Key) key, errors); return binding; if (isMembersInjector(key)) { BindingImpl<T> binding = (BindingImpl<T>) createMembersInjectorBinding((Key) key, errors); return binding; BindingImpl<T> convertedBinding = convertConstantStringBinding(key, errors); if (convertedBinding != null) { return convertedBinding; if (!isTypeLiteral(key) && jitDisabled && jitType != JitLimitation.NEW_OR_EXISTING_JIT) { throw errors.jitDisabled(key).toException(); try { Errors ignored = new Errors(); return getBindingOrThrow(key.withoutAttributes(), ignored, JitLimitation.NO_JIT); } catch (ErrorsException ignored) { createUninitializedBinding(key, Scoping.UNSCOPED, source, errors, true); errors.throwIfNewErrors(numErrorsBefore); initializeJitBinding(binding, errors); return binding;
<T> void initializeJitBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException { // Put the partially constructed binding in the map a little early. This enables us to handle // circular dependencies. Example: FooImpl -> BarImpl -> FooImpl. // Note: We don't need to synchronize on state.lock() during injector creation. if (binding instanceof DelayedInitialize) { Key<T> key = binding.getKey(); jitBindings.put(key, binding); boolean successful = false; DelayedInitialize delayed = (DelayedInitialize) binding; try { delayed.initialize(this, errors); successful = true; } finally { if (!successful) { // We do not pass cb.getInternalConstructor as the second parameter // so that cached exceptions while constructing it get stored. // See TypeListenerTest#testTypeListenerThrows removeFailedJitBinding(binding, null); cleanup(binding, new HashSet<Key>()); } } } }
throws ErrorsException { boolean jitOverride = isProvider(key) || isTypeLiteral(key) || isMembersInjector(key); synchronized (state.lock()) { throw errors.toException(); return createJustInTimeBindingRecursive(key, errors, options.jitDisabled, jitType);
@SuppressWarnings("unchecked") // we have to fudge the inner type as Object BindingImpl<T> binding = (BindingImpl<T>) createTypeLiteralBinding((Key<TypeLiteral<Object>>) key, errors); return binding; return createImplementedByBinding(key, scoping, implementedBy, errors); if (providedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createProvidedByBinding(key, scoping, providedBy, errors);
return parent.createJustInTimeBindingRecursive( key, new Errors(), BindingImpl<T> binding = createJustInTimeBinding(key, errors, jitDisabled, jitType); state.parent().blacklist(key, state, binding.getSource()); jitBindings.put(key, binding);
@Override public Injector createChildInjector(Module... modules) { return createChildInjector(ImmutableList.copyOf(modules)); }
/** Returns parameter injectors, or {@code null} if there are no parameters. */ SingleParameterInjector<?>[] getParametersInjectors(List<Dependency<?>> parameters, Errors errors) throws ErrorsException { if (parameters.isEmpty()) { return null; } int numErrorsBefore = errors.size(); SingleParameterInjector<?>[] result = new SingleParameterInjector<?>[parameters.size()]; int i = 0; for (Dependency<?> parameter : parameters) { try { result[i++] = createParameterInjector(parameter, errors.withSource(parameter)); } catch (ErrorsException rethrownBelow) { // rethrown below } } errors.throwIfNewErrors(numErrorsBefore); return result; }
/** * Returns a just-in-time binding for {@code key}, creating it if necessary. * * @throws ErrorsException if the binding could not be created. */ private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, JitLimitation jitType) throws ErrorsException { boolean jitOverride = isProvider(key) || isTypeLiteral(key) || isMembersInjector(key); synchronized (state.lock()) { // first try to find a JIT binding that we've already created for (InjectorImpl injector = this; injector != null; injector = injector.parent) { @SuppressWarnings("unchecked") // we only store bindings that match their key BindingImpl<T> binding = (BindingImpl<T>) injector.jitBindings.get(key); if (binding != null) { // If we found a JIT binding and we don't allow them, // fail. (But allow bindings created through TypeConverters.) if (options.jitDisabled && jitType == JitLimitation.NO_JIT && !jitOverride && !(binding instanceof ConvertedConstantBindingImpl)) { throw errors.jitDisabled(key).toException(); } else { return binding; } } } return createJustInTimeBindingRecursive(key, errors, options.jitDisabled, jitType); } }
BindingImpl<T> binding = (BindingImpl<T>) createTypeLiteralBinding( (Key<TypeLiteral<Object>>) key, errors); return binding; if (implementedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createImplementedByBinding(key, scoping, implementedBy, errors); if (providedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createProvidedByBinding(key, scoping, providedBy, errors);
<T> void initializeJitBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException { // Put the partially constructed binding in the map a little early. This enables us to handle // circular dependencies. Example: FooImpl -> BarImpl -> FooImpl. // Note: We don't need to synchronize on state.lock() during injector creation. if (binding instanceof DelayedInitialize) { Key<T> key = binding.getKey(); jitBindings.put(key, binding); boolean successful = false; DelayedInitialize delayed = (DelayedInitialize) binding; try { delayed.initialize(this, errors); successful = true; } finally { if (!successful) { // We do not pass cb.getInternalConstructor as the second parameter // so that cached exceptions while constructing it get stored. // See TypeListenerTest#testTypeListenerThrows removeFailedJitBinding(binding, null); cleanup(binding, new HashSet<Key>()); } } } }
/** * Attempts to create a just-in-time binding for {@code key} in the root injector, falling back to * other ancestor injectors until this injector is tried. */ private <T> BindingImpl<T> createJustInTimeBindingRecursive(Key<T> key, Errors errors, boolean jitDisabled, JitLimitation jitType) throws ErrorsException { // ask the parent to create the JIT binding if (parent != null) { try { return parent.createJustInTimeBindingRecursive(key, new Errors(), jitDisabled, parent.options.jitDisabled ? JitLimitation.NO_JIT : jitType); } catch (ErrorsException ignored) { } } if (state.isBlacklisted(key)) { Set<Object> sources = state.getSourcesForBlacklistedKey(key); throw errors.childBindingAlreadySet(key, sources).toException(); } BindingImpl<T> binding = createJustInTimeBinding(key, errors, jitDisabled, jitType); state.parent().blacklist(key, binding.getSource()); jitBindings.put(key, binding); return binding; }
@Override public Injector createChildInjector(Module... modules) { return createChildInjector(ImmutableList.copyOf(modules)); }
/** Returns parameter injectors, or {@code null} if there are no parameters. */ SingleParameterInjector<?>[] getParametersInjectors(List<Dependency<?>> parameters, Errors errors) throws ErrorsException { if (parameters.isEmpty()) { return null; } int numErrorsBefore = errors.size(); SingleParameterInjector<?>[] result = new SingleParameterInjector<?>[parameters.size()]; int i = 0; for (Dependency<?> parameter : parameters) { try { result[i++] = createParameterInjector(parameter, errors.withSource(parameter)); } catch (ErrorsException rethrownBelow) { // rethrown below } } errors.throwIfNewErrors(numErrorsBefore); return result; }
if (isProvider(key)) { BindingImpl<T> binding = (BindingImpl<T>) createProviderBinding((Key) key, errors); return binding; if (isMembersInjector(key)) { BindingImpl<T> binding = (BindingImpl<T>) createMembersInjectorBinding((Key) key, errors); return binding; BindingImpl<T> convertedBinding = convertConstantStringBinding(key, errors); if (convertedBinding != null) { return convertedBinding; if (!isTypeLiteral(key) && jitDisabled && jitType != JitLimitation.NEW_OR_EXISTING_JIT) { throw errors.jitDisabled(key).toException(); try { Errors ignored = new Errors(); return getBindingOrThrow(key.withoutAttributes(), ignored, JitLimitation.NO_JIT); } catch (ErrorsException ignored) { createUninitializedBinding(key, Scoping.UNSCOPED, source, errors, true); errors.throwIfNewErrors(numErrorsBefore); initializeJitBinding(binding, errors); return binding;
/** * Returns a just-in-time binding for {@code key}, creating it if necessary. * * @throws ErrorsException if the binding could not be created. */ private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, JitLimitation jitType) throws ErrorsException { boolean jitOverride = isProvider(key) || isTypeLiteral(key) || isMembersInjector(key); synchronized (state.lock()) { // first try to find a JIT binding that we've already created for (InjectorImpl injector = this; injector != null; injector = injector.parent) { @SuppressWarnings("unchecked") // we only store bindings that match their key BindingImpl<T> binding = (BindingImpl<T>) injector.jitBindings.get(key); if (binding != null) { // If we found a JIT binding and we don't allow them, // fail. (But allow bindings created through TypeConverters.) if (options.jitDisabled && jitType == JitLimitation.NO_JIT && !jitOverride && !(binding instanceof ConvertedConstantBindingImpl)) { throw errors.jitDisabled(key).toException(); } else { return binding; } } } return createJustInTimeBindingRecursive(key, errors, options.jitDisabled, jitType); } }
Set<Dependency<?>> deps = getInternalDependencies(binding); for (Dependency dep : deps) { Key<?> depKey = dep.getKey(); BindingImpl depBinding = jitBindings.get(depKey); if (depBinding != null) { // if the binding still exists, validate boolean failed = cleanup(depBinding, encountered); // if children fail, we fail if (depBinding instanceof ConstructorBindingImpl) { ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding; removeFailedJitBinding(depBinding, ip); bindingFailed = true;
BindingImpl<T> binding = (BindingImpl<T>) createTypeLiteralBinding( (Key<TypeLiteral<Object>>) key, errors); return binding; if (implementedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createImplementedByBinding(key, scoping, implementedBy, errors); if (providedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); return createProvidedByBinding(key, scoping, providedBy, errors);
<T> void initializeJitBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException { // Put the partially constructed binding in the map a little early. This enables us to handle // circular dependencies. Example: FooImpl -> BarImpl -> FooImpl. // Note: We don't need to synchronize on state.lock() during injector creation. if (binding instanceof ConstructorBindingImpl<?>) { Key<T> key = binding.getKey(); jitBindings.put(key, binding); boolean successful = false; ConstructorBindingImpl cb = (ConstructorBindingImpl)binding; try { cb.initialize(this, errors); successful = true; } finally { if (!successful) { // We do not pass cb.getInternalConstructor as the second parameter // so that cached exceptions while constructing it get stored. // See TypeListenerTest#testTypeListenerThrows removeFailedJitBinding(key, null); cleanup(binding, new HashSet<Key>()); } } } }