@Override public boolean equals(Object obj) { if (obj instanceof ProviderBindingImpl) { ProviderBindingImpl<?> o = (ProviderBindingImpl<?>) obj; return getKey().equals(o.getKey()) && getScoping().equals(o.getScoping()) && Objects.equal(providedBinding, o.providedBinding); } else { return false; } }
if (!scoping.isExplicitlyScoped()) { Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass(); Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType); if (scopeAnnotation != null) { scoping = Scoping.makeInjectable( Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType)); Scoping.scope(key, injector, factoryFactory, source, scoping);
/** * Replaces annotation scopes with instance scopes using the Injector's annotation-to-instance * map. If the scope annotation has no corresponding instance, an error will be added and unscoped * will be retuned. */ static Scoping makeInjectable(Scoping scoping, InjectorImpl injector, Errors errors) { Class<? extends Annotation> scopeAnnotation = scoping.getScopeAnnotation(); if (scopeAnnotation == null) { return scoping; } ScopeBinding scope = injector.state.getScopeBinding(scopeAnnotation); if (scope != null) { return forInstance(scope.getScope()); } errors.scopeNotFound(scopeAnnotation); return UNSCOPED; } }
@Override public boolean equals(Object obj) { if (obj instanceof Scoping) { Scoping o = (Scoping) obj; return Objects.equal(getScopeAnnotation(), o.getScopeAnnotation()) && Objects.equal(getScopeInstance(), o.getScopeInstance()); } else { return false; } }
/** Scopes an internal factory. */ static <T> InternalFactory<? extends T> scope( Key<T> key, InjectorImpl injector, InternalFactory<? extends T> creator, Object source, Scoping scoping) { if (scoping.isNoScope()) { return creator; } Scope scope = scoping.getScopeInstance(); // NOTE: SingletonScope relies on the fact that we are passing a // ProviderToInternalFactoryAdapter here. If you change the type make sure to update // SingletonScope as well. Provider<T> scoped = scope.scope(key, new ProviderToInternalFactoryAdapter<T>(injector, creator)); return new InternalFactoryToProviderAdapter<T>(scoped, source); }
@Override public void applyTo(Binder binder) { getScoping() .applyTo(binder.withSource(getSource()).bind(getKey()).toProvider(getProviderKey())); }
public void in(final Scope scope) { checkNotNull(scope, "scope"); checkNotScoped(); setBinding(getBinding().withScoping(Scoping.forInstance(scope))); }
public void in(final Class<? extends Annotation> scopeAnnotation) { checkNotNull(scopeAnnotation, "scopeAnnotation"); checkNotScoped(); setBinding(getBinding().withScoping(Scoping.forAnnotation(scopeAnnotation))); }
public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) { if (scopingAnnotation == Singleton.class || scopingAnnotation == javax.inject.Singleton.class) { return SINGLETON_ANNOTATION; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(scopingAnnotation); } @Override public Class<? extends Annotation> getScopeAnnotation() { return scopingAnnotation; } @Override public String toString() { return scopingAnnotation.getName(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scopingAnnotation); } }; }
private boolean isEagerSingleton(InjectorImpl injector, BindingImpl<?> binding, Stage stage) { if (binding.getScoping().isEagerSingleton(stage)) { return true; } // handle a corner case where a child injector links to a binding in a parent injector, and // that binding is singleton. We won't catch this otherwise because we only iterate the child's // bindings. if (binding instanceof LinkedBindingImpl) { Key<?> linkedBinding = ((LinkedBindingImpl<?>) binding).getLinkedKey(); return isEagerSingleton(injector, injector.getBinding(linkedBinding), stage); } return false; }
@Override public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) { return scoping.acceptVisitor(visitor); }
/** * Returns true if this is the default scope. In this case a new instance will be provided for * each injection. */ public boolean isNoScope() { return getScopeInstance() == Scopes.NO_SCOPE; }
public void releaseContext(GuiceBean bean, Object context) { Scoping scoping = ((BindingImpl)bean.binding).getScoping(); if (scoping.isNoScope()) { invokePreDestroy(context); } }
@Override public int hashCode() { return Objects.hashCode(getScopeAnnotation(), getScopeInstance()); }
/** * Scopes an internal factory. */ static <T> InternalFactory<? extends T> scope(Key<T> key, InjectorImpl injector, InternalFactory<? extends T> creator, Object source, Scoping scoping) { if (scoping.isNoScope()) { return creator; } Scope scope = scoping.getScopeInstance(); Provider<T> scoped = scope.scope(key, new ProviderToInternalFactoryAdapter<T>(injector, creator)); return new InternalFactoryToProviderAdapter<T>(scoped, source); }
@Override public void applyTo(Binder binder) { getScoping() .applyTo( binder.withSource(getSource()).bind(getKey()).toProvider(getUserSuppliedProvider())); }
@Override public Scoping visitScope(Scope scope) { return Scoping.forInstance(scope); }
@Override public Scoping visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { return Scoping.forAnnotation(scopeAnnotation); }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } else if (scope == Scopes.NO_SCOPE) { return EXPLICITLY_UNSCOPED; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
private boolean isEagerSingleton(InjectorImpl injector, BindingImpl<?> binding, Stage stage) { if (binding.getScoping().isEagerSingleton(stage)) { return true; } // handle a corner case where a child injector links to a binding in a parent injector, and // that binding is singleton. We won't catch this otherwise because we only iterate the child's // bindings. if (binding instanceof LinkedBindingImpl) { Key<?> linkedBinding = ((LinkedBindingImpl<?>) binding).getLinkedKey(); return isEagerSingleton(injector, injector.getBinding(linkedBinding), stage); } return false; }