/** * When a factory method is invoked, we create a child injector that binds all parameters, then * use that to get an instance of the return type. */ @Override public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable { if (method.getDeclaringClass() == Object.class) { return method.invoke(this, args); } Provider<?> provider = getBindingFromNewInjector(method, args).getProvider(); try { return provider.get(); } catch (ProvisionException e) { // if this is an exception declared by the factory method, throw it as-is if (e.getErrorMessages().size() == 1) { Message onlyError = Iterables.getOnlyElement(e.getErrorMessages()); Throwable cause = onlyError.getCause(); if (cause != null && canRethrow(method, cause)) { throw cause; } } throw e; } }