@Override public <T> Provider<T> getProvider(Class<T> clazz, String name) { crashIfClosed(); return new ThreadSafeProviderImpl<>(this, clazz, name, false); }
/** * Install bindings for scope. */ private void installBindingForScope() { //it's always possible to get access to the scope that contains an injected object. installBoundProvider(Scope.class, null, new InternalProviderImpl<>(this), false); }
@Override public void doRun() { Toothpick.openScope(scopeName).getInstance(clazz); setIsSuccessful(true); } }
@Override public <T> T getInstance(Class<T> clazz, String name) { crashIfClosed(); ConfigurationHolder.configuration.checkCyclesStart(clazz, name); T t; try { t = lookupProvider(clazz, name).get(this); } finally { ConfigurationHolder.configuration.checkCyclesEnd(clazz, name); } return t; }
public void setScopeName(Object scopeName) { if (scope != null) { throw new IllegalStateException("scope is already initialized, use a constructor without a scope name for the rule."); } scope = Toothpick.openScope(scopeName); scope.installTestModules(testModule); }
/** * Opens a scope without any parent. * If a scope by this {@code name} already exists, it is returned. * Otherwise a new scope is created. */ public static Scope openScope(Object name) { return openScope(name, true); }
/** * Resets the state of the scope. * Useful for automation testing when we want to reset the scope used to install test modules. */ @Override protected void reset() { super.reset(); mapClassesToNamedBoundProviders.clear(); mapClassesToUnNamedBoundProviders.clear(); hasTestModules = false; installBindingForScope(); }
@Override public void installModules(Module... modules) { installModules(false, modules); }
/** * Injects all dependencies (transitively) in {@code obj}, dependencies will be obtained in the * scope {@code scope}. * * @param obj the object to be injected. * @param scope the scope in which all dependencies are obtained. */ public static void inject(Object obj, Scope scope) { injector.inject(obj, scope); }
/** * Resets the state of a single scope. Useful for automation testing when we want to reset the scope used to install * test modules. * @param scope the scope we want to reset. */ public static void reset(Scope scope) { ScopeNode scopeNode = (ScopeNode) scope; scopeNode.reset(); }
/** * Obtains the provider of the class {@code clazz} and name {@code bindingName}, if any. The returned provider * will belong to the pool of unbound providers. It can be {@code null} if there is no such provider. * * @param clazz the class for which to obtain the unbound provider. * @param bindingName the name, possibly {@code null}, for which to obtain the unbound provider. * @param <T> the type of {@code clazz}. * @return the unbound provider for class {@code clazz} and {@code bindingName}. Returns {@code null} is there * is no such unbound provider. */ private <T> InternalProviderImpl<? extends T> getUnBoundProvider(Class<T> clazz, String bindingName) { return getInternalProvider(clazz, bindingName, false); }
@Override public <T> Provider<T> getProvider(Class<T> clazz) { return getProvider(clazz, null); }
public T get(Scope scope) { return super.get(this.scope); } }
public ScopeImpl(Object name) { super(name); installBindingForScope(); }
public static int getScopeNamesSize() { return Toothpick.getScopeNamesSize(); } }
@Override public <T> Lazy<T> getLazy(Class<T> clazz, String name) { crashIfClosed(); return new ThreadSafeProviderImpl<>(this, clazz, name, true); }