SingleParameterInjector(Dependency<T> dependency, BindingImpl<? extends T> binding) { this.dependency = dependency; this.source = binding.getSource(); this.factory = binding.getInternalFactory(); }
Processor(BindingImpl<T> binding) { source = binding.getSource(); key = binding.getKey(); rawType = key.getTypeLiteral().getRawType(); scoping = binding.getScoping(); }
@Override void doInitialize() { BindingImpl<T> targetBinding = bindingSelection.getBinding(); // we only install this factory if they call setBinding()/setDefault() so we know that // targetBinding will be non-null. this.targetKey = targetBinding.getKey(); this.targetSource = targetBinding.getSource(); this.targetFactory = targetBinding.getInternalFactory(); }
@Override public void inject(InternalContext context, Object o) throws InternalProvisionException { Dependency previous = context.pushDependency(dependency, binding.getSource()); try { Object value = binding.getInternalFactory().get(context, dependency, false); field.set(o, value); } catch (InternalProvisionException e) { throw e.addSource(dependency); } catch (IllegalAccessException e) { throw new AssertionError(e); // a security manager is blocking us, we're hosed } finally { context.popStateAndSetDependency(previous); } } }
@Override public BindingBuilder<T> to(Key<? extends T> linkedKey) { checkNotNull(linkedKey, "linkedKey"); checkNotTargetted(); BindingImpl<T> base = getBinding(); setBinding( new LinkedBindingImpl<T>(base.getSource(), base.getKey(), base.getScoping(), linkedKey)); return this; }
@Override public BindingBuilder<T> toProvider( Key<? extends javax.inject.Provider<? extends T>> providerKey) { checkNotNull(providerKey, "providerKey"); checkNotTargetted(); BindingImpl<T> base = getBinding(); setBinding( new LinkedProviderBindingImpl<T>( base.getSource(), base.getKey(), base.getScoping(), providerKey)); return this; }
<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() { InternalContext currentContext = enterContext(); Dependency previous = currentContext.pushDependency(dependency, source); try { T t = internalFactory.get(currentContext, dependency, false); return t; } catch (InternalProvisionException e) { throw e.addSource(dependency).toProvisionException(); } finally { currentContext.popStateAndSetDependency(previous); currentContext.close(); } } @Override public String toString() { return internalFactory.toString(); } }; }
try { if (!isOkayDuplicate(original, binding, injector.state)) { errors.bindingAlreadySet(key, original.getSource()); return; errors.errorCheckingDuplicateBinding(key, original.getSource(), t); return; injector.state.parent().blacklist(key, injector.state, binding.getSource()); injector.state.putBinding(key, binding);
@Override public void notify(Errors errors) { InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector(); BindingImpl<T> explicitBinding = privateInjector.state.getExplicitBinding(key); // validate that the child injector has its own factory. If the getInternalFactory() returns // this, then that child injector doesn't have a factory (and getExplicitBinding has returned // its parent's binding instead if (explicitBinding.getInternalFactory() == this) { errors.withSource(explicitBinding.getSource()).exposedButNotBound(key); return; } this.delegate = explicitBinding; }
@Override public T get(InternalContext context, Dependency<?> dependency, boolean linked) throws InternalProvisionException { BindingImpl<? extends Provider<T>> localProviderBinding = providerBinding; if (localProviderBinding == null) { throw new IllegalStateException("not initialized"); } Key<? extends Provider<T>> localProviderKey = providerKey; context.pushState(localProviderKey, localProviderBinding.getSource()); try { Provider<? extends T> provider = localProviderBinding.getInternalFactory().get(context, dependency, true); return circularGet(provider, context, dependency, provisionCallback); } catch (InternalProvisionException ipe) { throw ipe.addSource(localProviderKey); } finally { context.popState(); } }
@Override public BindingBuilder<T> toProvider(javax.inject.Provider<? extends T> provider) { checkNotNull(provider, "provider"); checkNotTargetted(); // lookup the injection points, adding any errors to the binder's errors list Set<InjectionPoint> injectionPoints; try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass()); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } BindingImpl<T> base = getBinding(); setBinding( new ProviderInstanceBindingImpl<T>( base.getSource(), base.getKey(), base.getScoping(), injectionPoints, provider)); return this; }
@Override public void toInstance(T instance) { checkNotTargetted(); // lookup the injection points, adding any errors to the binder's errors list Set<InjectionPoint> injectionPoints; if (instance != null) { try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(instance.getClass()); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } } else { binder.addError(BINDING_TO_NULL); injectionPoints = ImmutableSet.of(); } BindingImpl<T> base = getBinding(); setBinding( new InstanceBindingImpl<T>( base.getSource(), base.getKey(), Scoping.EAGER_SINGLETON, injectionPoints, instance)); }
if (isEagerSingleton(injector, binding, stage)) { Dependency<?> dependency = Dependency.get(binding.getKey()); Dependency previous = context.pushDependency(dependency, binding.getSource());
private void toConstant(Class<?> type, Object instance) { // this type will define T, so these assignments are safe @SuppressWarnings("unchecked") Class<T> typeAsClassT = (Class<T>) type; @SuppressWarnings("unchecked") T instanceAsT = (T) instance; if (keyTypeIsSet()) { binder.addError(CONSTANT_VALUE_ALREADY_SET); return; } BindingImpl<T> base = getBinding(); Key<T> key; if (base.getKey().getAnnotation() != null) { key = Key.get(typeAsClassT, base.getKey().getAnnotation()); } else if (base.getKey().getAnnotationType() != null) { key = Key.get(typeAsClassT, base.getKey().getAnnotationType()); } else { key = Key.get(typeAsClassT); } if (instanceAsT == null) { binder.addError(BINDING_TO_NULL); } setBinding( new InstanceBindingImpl<T>( base.getSource(), key, base.getScoping(), ImmutableSet.<InjectionPoint>of(), instanceAsT)); }
@Override public <S extends T> ScopedBindingBuilder toConstructor( Constructor<S> constructor, TypeLiteral<? extends S> type) { checkNotNull(constructor, "constructor"); checkNotNull(type, "type"); checkNotTargetted(); BindingImpl<T> base = getBinding(); Set<InjectionPoint> injectionPoints; try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } try { InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type); setBinding( new ConstructorBindingImpl<T>( base.getKey(), base.getSource(), base.getScoping(), constructorPoint, injectionPoints)); } catch (ConfigurationException e) { copyErrorsToBinder(e); } return this; }
state.parent().blacklist(key, state, binding.getSource()); jitBindings.put(key, binding); return binding;
Object source = stringBinding.getSource();
Processor(BindingImpl<T> binding) { source = binding.getSource(); key = binding.getKey(); rawType = key.getTypeLiteral().getRawType(); scoping = binding.getScoping(); }
@Override void doInitialize() { BindingImpl<T> targetBinding = bindingSelection.getBinding(); // we only install this factory if they call setBinding()/setDefault() so we know that // targetBinding will be non-null. this.targetKey = targetBinding.getKey(); this.targetSource = targetBinding.getSource(); this.targetFactory = targetBinding.getInternalFactory(); }
@Override public BindingBuilder<T> to(Key<? extends T> linkedKey) { checkNotNull(linkedKey, "linkedKey"); checkNotTargetted(); BindingImpl<T> base = getBinding(); setBinding( new LinkedBindingImpl<T>(base.getSource(), base.getKey(), base.getScoping(), linkedKey)); return this; }