@Override public T get() { InternalContext context = injector.enterContext(); try { // Always pretend that we are a linked binding, to support // scoping implicit bindings. If we are not actually a linked // binding, we'll fail properly elsewhere in the chain. T t = internalFactory.get(context, context.getDependency(), true); return t; } catch (InternalProvisionException e) { throw e.toProvisionException(); } finally { context.close(); } }
/** * Looks up thread local context and {@link InternalContext#enter() enters} it or creates a new * context if necessary. * * <p>All callers of this are responsible for calling {@link InternalContext#close()}. Typical * usage should look like: * * <pre>{@code * InternalContext ctx = injector.enterContext(); * try { * ... use ctx ... * } finally { * ctx.close(); * } * }</pre> */ InternalContext enterContext() { Object[] reference = localContext.get(); if (reference == null) { reference = new Object[1]; localContext.set(reference); } InternalContext ctx = (InternalContext) reference[0]; if (ctx == null) { reference[0] = ctx = new InternalContext(options, reference); } else { ctx.enter(); } return ctx; }
final ConstructionContext<T> constructionContext = context.getConstructionContext(this); context.getInjectorOptions(), dependency.getKey().getTypeLiteral().getRawType()); if (context.getInjectorOptions().disableCircularProxies) { throw InternalProvisionException.circularDependenciesDisabled( dependency.getKey().getTypeLiteral().getRawType());
T inject(InternalContext context) throws InternalProvisionException { Dependency<T> localDependency = dependency; Dependency previous = context.pushDependency(localDependency, source); try { return factory.get(context, localDependency, false); } catch (InternalProvisionException ipe) { throw ipe.addSource(localDependency); } finally { context.popStateAndSetDependency(previous); } }
@Override protected T doProvision(InternalContext context, Dependency<?> dependency) throws InternalProvisionException { // This is what linked bindings do (see FactoryProxy), and we are pretty similar. context.pushState(targetKey, targetSource); try { return targetFactory.get(context, dependency, true); } catch (InternalProvisionException ipe) { throw ipe.addSource(targetKey); } finally { context.popState(); } }
@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(); } }
context.getDependency(), "internalContext.getDependency()"); Class<?> rawType = dependency.getKey().getTypeLiteral().getRawType(); @SuppressWarnings("unchecked") T proxy = (T) constructionContext.createProxy(context.getInjectorOptions(), rawType); return proxy; } catch (InternalProvisionException e) {
public T call(InternalContext context) throws ErrorsException { Dependency dependency = context.getDependency(); // Always pretend that we are a linked binding, to support // scoping implicit bindings. If we are not actually a linked // binding, we'll fail properly elsewhere in the chain. return internalFactory.get(errors, context, dependency, true); } });
/** Looks up thread local context. Creates (and removes) a new context if necessary. */ <T> T callInContext(ContextualCallable<T> callable) throws ErrorsException { Object[] reference = localContext.get(); if (reference[0] == null) { reference[0] = new InternalContext(); try { return callable.call((InternalContext)reference[0]); } finally { // Only clear the context if this call created it. reference[0] = null; } } else { // Someone else will clean up this context. return callable.call((InternalContext)reference[0]); } }
ConstructionContext<T> constructionContext = context.getConstructionContext(this);
/** Sets the new current dependency & adds it to the state. */ Dependency<?> pushDependency(Dependency<?> dependency, Object source) { Dependency<?> previous = this.dependency; this.dependency = dependency; doPushState(dependency, source); return previous; }
/** Pops the current state & sets the new dependency. */ void popStateAndSetDependency(Dependency<?> newDependency) { popState(); this.dependency = newDependency; }
void injectMembers() { InternalContext context = injector.enterContext(); try { boolean isStageTool = injector.options.stage == Stage.TOOL; for (SingleMemberInjector memberInjector : memberInjectors) { // Run injections if we're not in tool stage (ie, PRODUCTION or DEV), // or if we are in tool stage and the injection point is toolable. if (!isStageTool || memberInjector.getInjectionPoint().isToolable()) { try { memberInjector.inject(context, null); } catch (InternalProvisionException e) { errors.merge(e); } } } } finally { context.close(); } } }
@Override public T get(InternalContext context, Dependency<?> dependency, boolean linked) throws InternalProvisionException { Key<? extends T> localTargetKey = targetKey; context.pushState(localTargetKey, source); try { return targetFactory.get(context, dependency, true); } catch (InternalProvisionException ipe) { throw ipe.addSource(localTargetKey); } finally { context.popState(); } }
@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); } } }
if (isEagerSingleton(injector, binding, stage)) { Dependency<?> dependency = Dependency.get(binding.getKey()); Dependency previous = context.pushDependency(dependency, binding.getSource()); errors.withSource(dependency).merge(e); } finally { context.popStateAndSetDependency(previous); context.close();
internalContext.getDependency(), "globalInternalContext.get(currentThread()).getDependency()"); Class<?> rawType = dependency.getKey().getTypeLiteral().getRawType(); @SuppressWarnings("unchecked") T proxy = (T) constructionContext.createProxy( new Errors(), internalContext.getInjectorOptions(), rawType); return proxy; } catch (ErrorsException e) {
public T call(InternalContext context) throws ErrorsException { Dependency dependency = context.getDependency(); // Always pretend that we are a linked binding, to support // scoping implicit bindings. If we are not actually a linked // binding, we'll fail properly elsewhere in the chain. return internalFactory.get(errors, context, dependency, true); } });