public static InternalProvisionException errorInUserCode( Throwable cause, String messageFormat, Object... arguments) { Collection<Message> messages = Errors.getMessagesFromThrowable(cause); if (!messages.isEmpty()) { // TODO(lukes): it seems like we are dropping some valuable context here.. // consider eliminating this special case return new InternalProvisionException(messages); } else { return new InternalProvisionException(Messages.create(cause, messageFormat, arguments)); } }
@Override public T get(InternalContext context, Dependency<?> dependency, boolean linked) throws InternalProvisionException { try { T t = provider.get(); if (t == null && !dependency.isNullable()) { InternalProvisionException.onNullInjectedIntoNonNullableDependency(source, dependency); } return t; } catch (RuntimeException userException) { throw InternalProvisionException.errorInProvider(userException).addSource(source); } }
public Object createProxy(InjectorOptions injectorOptions, Class<?> expectedType) throws InternalProvisionException { if (injectorOptions.disableCircularProxies) { throw InternalProvisionException.circularDependenciesDisabled(expectedType); } if (!expectedType.isInterface()) { throw InternalProvisionException.cannotProxyClass(expectedType); } if (invocationHandlers == null) { invocationHandlers = new ArrayList<>(); } DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<>(); invocationHandlers.add(invocationHandler); // TODO: if I create a proxy which implements all the interfaces of // the implementation type, I'll be able to get away with one proxy // instance (as opposed to one per caller). ClassLoader classLoader = BytecodeGen.getClassLoader(expectedType); return expectedType.cast( Proxy.newProxyInstance( classLoader, new Class[] {expectedType, CircularDependencyProxy.class}, invocationHandler)); }
@Override public void injectMembers(T instance) { TypeLiteral<T> localTypeLiteral = typeLiteral; try { injectAndNotify(instance, null, null, localTypeLiteral, false); } catch (InternalProvisionException ipe) { throw ipe.addSource(localTypeLiteral).toProvisionException(); } }
@Override protected T provision( javax.inject.Provider<? extends T> provider, Dependency<?> dependency, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { return super.provision(provider, dependency, constructionContext); } catch (RuntimeException userException) { throw InternalProvisionException.errorInProvider(userException).addSource(source); } }
@Override protected T provision( javax.inject.Provider<? extends T> provider, Dependency<?> dependency, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { Object o = super.provision(provider, dependency, constructionContext); if (o != null && !rawType.isInstance(o)) { throw InternalProvisionException.subtypeNotProvided(providerType, rawType); } @SuppressWarnings("unchecked") // protected by isInstance() check above T t = (T) o; return t; } catch (RuntimeException e) { throw InternalProvisionException.errorInProvider(e).addSource(source); } } }
public static InternalProvisionException circularDependenciesDisabled(Class<?> expectedType) { return create( "Found a circular dependency involving %s, and circular dependencies are disabled.", expectedType); }
public static InternalProvisionException errorInUserInjector( MembersInjector<?> listener, TypeLiteral<?> type, RuntimeException cause) { return errorInUserCode( cause, "Error injecting %s using %s.%n Reason: %s", type, listener, cause); }
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 public void inject(InternalContext context, Object o) throws InternalProvisionException { Object[] parameters = SingleParameterInjector.getAll(context, parameterInjectors); try { methodInvoker.invoke(o, parameters); } catch (IllegalAccessException e) { throw new AssertionError(e); // a security manager is blocking us, we're hosed } catch (InvocationTargetException userException) { Throwable cause = userException.getCause() != null ? userException.getCause() : userException; throw InternalProvisionException.errorInjectingMethod(cause).addSource(injectionPoint); } } }
/** Provisions a new T. */ private T provision(InternalContext context, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { T t; try { Object[] parameters = SingleParameterInjector.getAll(context, parameterInjectors); t = constructionProxy.newInstance(parameters); constructionContext.setProxyDelegates(t); } finally { constructionContext.finishConstruction(); } // Store reference. If an injector re-enters this factory, they'll get the same reference. constructionContext.setCurrentReference(t); MembersInjectorImpl<T> localMembersInjector = membersInjector; localMembersInjector.injectMembers(t, context, false); localMembersInjector.notifyListeners(t); return t; } catch (InvocationTargetException userException) { Throwable cause = userException.getCause() != null ? userException.getCause() : userException; throw InternalProvisionException.errorInjectingConstructor(cause) .addSource(constructionProxy.getInjectionPoint()); } finally { constructionContext.removeCurrentReference(); } } }
@Override protected T provision( Provider<? extends T> provider, Dependency<?> dependency, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { return super.provision(provider, dependency, constructionContext); } catch (RuntimeException userException) { throw InternalProvisionException.errorInProvider(userException); } }
void injectMembers(T t, InternalContext context, boolean toolableOnly) throws InternalProvisionException { ImmutableList<SingleMemberInjector> localMembersInjectors = memberInjectors; if (localMembersInjectors != null) { // optimization: use manual for/each to save allocating an iterator here for (int i = 0, size = localMembersInjectors.size(); i < size; i++) { SingleMemberInjector injector = localMembersInjectors.get(i); if (!toolableOnly || injector.getInjectionPoint().isToolable()) { injector.inject(context, t); } } } // TODO: There's no way to know if a user's MembersInjector wants toolable injections. if (!toolableOnly) { ImmutableList<MembersInjector<? super T>> localUsersMembersInjectors = userMembersInjectors; if (localUsersMembersInjectors != null) { // optimization: use manual for/each to save allocating an iterator here for (int i = 0; i < localUsersMembersInjectors.size(); i++) { MembersInjector<? super T> userMembersInjector = localUsersMembersInjectors.get(i); try { userMembersInjector.injectMembers(t); } catch (RuntimeException e) { throw InternalProvisionException.errorInUserInjector( userMembersInjector, typeLiteral, e); } } } } }
if (t != null) { if (context.getInjectorOptions().disableCircularProxies) { throw InternalProvisionException.circularDependenciesDisabled( dependency.getKey().getTypeLiteral().getRawType()); } else {
@Override protected T provision( javax.inject.Provider<? extends T> provider, Dependency<?> dependency, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { Object o = super.provision(provider, dependency, constructionContext); if (o != null && !rawType.isInstance(o)) { throw InternalProvisionException.subtypeNotProvided(providerType, rawType); } @SuppressWarnings("unchecked") // protected by isInstance() check above T t = (T) o; return t; } catch (RuntimeException e) { throw InternalProvisionException.errorInProvider(e).addSource(source); } } }
private T provision( Dependency<?> dependency, InternalContext context, ConstructionContext<T> constructionContext) throws InternalProvisionException { try { T t = doProvision(context, dependency); constructionContext.setProxyDelegates(t); return t; } catch (InternalProvisionException ipe) { throw ipe.addSource(getSource()); } catch (Throwable t) { throw InternalProvisionException.errorInProvider(t).addSource(getSource()); } } }
public static InternalProvisionException cannotProxyClass(Class<?> expectedType) { return create( "Tried proxying %s to support a circular dependency, but it is not an interface.", expectedType); }
public static InternalProvisionException errorNotifyingInjectionListener( InjectionListener<?> listener, TypeLiteral<?> type, RuntimeException cause) { return errorInUserCode( cause, "Error notifying InjectionListener %s of %s.%n Reason: %s", listener, type, cause); }
@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(); } }