@Override public Key<? extends T> getProvidedKey() { return providedBinding.getKey(); }
protected boolean keyTypeIsSet() { return !Void.class.equals(binding.getKey().getTypeLiteral().getType()); }
protected void checkNotAnnotated() { if (binding.getKey().getAnnotationType() != null) { binder.addError(ANNOTATION_ALREADY_SPECIFIED); } }
@Override public String toString() { return "BindingBuilder<" + getBinding().getKey().getTypeLiteral() + ">"; }
Processor(BindingImpl<T> binding) { source = binding.getSource(); key = binding.getKey(); rawType = key.getTypeLiteral().getRawType(); scoping = binding.getScoping(); }
/** Sets the binding to a copy with the specified annotation on the bound key */ protected BindingImpl<T> annotatedWithInternal(Annotation annotation) { checkNotNull(annotation, "annotation"); checkNotAnnotated(); return setBinding(binding.withKey(Key.get(this.binding.getKey().getTypeLiteral(), annotation))); }
/** Sets the binding to a copy with the specified annotation on the bound key */ protected BindingImpl<T> annotatedWithInternal(Class<? extends Annotation> annotationType) { checkNotNull(annotationType, "annotationType"); checkNotAnnotated(); return setBinding( binding.withKey(Key.get(this.binding.getKey().getTypeLiteral(), annotationType))); }
@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(); }
<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>()); } } } }
/** * We tolerate duplicate bindings if one exposes the other or if the two bindings are considered * duplicates (see {@link Bindings#areDuplicates(BindingImpl, BindingImpl)}. * * @param original the binding in the parent injector (candidate for an exposing binding) * @param binding the binding to check (candidate for the exposed binding) */ private boolean isOkayDuplicate(BindingImpl<?> original, BindingImpl<?> binding, State state) { if (original instanceof ExposedBindingImpl) { ExposedBindingImpl exposed = (ExposedBindingImpl) original; InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector(); return (exposedFrom == binding.getInjector()); } else { original = (BindingImpl<?>) state.getExplicitBindingsThisLevel().get(binding.getKey()); // If no original at this level, the original was on a parent, and we don't // allow deduplication between parents & children. if (original == null) { return false; } else { return original.equals(binding); } } }
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 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; }
@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(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)); }
for (BindingImpl<?> binding : candidateBindings) { if (isEagerSingleton(injector, binding, stage)) { Dependency<?> dependency = Dependency.get(binding.getKey()); Dependency previous = context.pushDependency(dependency, binding.getSource());
@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; }
protected void putBinding(BindingImpl<?> binding) { Key<?> key = binding.getKey();
@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; }