use of com.google.inject.ProvisionException in project guice by google.
the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.
public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
bindScope(BadScope.class, new Scope() {
@Override
public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure: " + key.toString());
}
};
}
});
}
});
try {
injector.getInstance(Key.get(remoteProviderOfFoo)).get();
fail();
} catch (ProvisionException pe) {
assertEquals(2, pe.getErrorMessages().size());
List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
}
}
use of com.google.inject.ProvisionException in project guice by google.
the class BoundFieldModuleTest method testNonNullableFieldBound_lazy_rejectNull.
public void testNonNullableFieldBound_lazy_rejectNull() {
LazyClass asProvider = new LazyClass();
Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider));
assertEquals(1, injector.getInstance(Integer.class).intValue());
asProvider.foo = null;
try {
injector.getInstance(Integer.class);
fail();
} catch (ProvisionException e) {
assertContains(e.getMessage(), "Binding to null values is only allowed for fields that are annotated @Nullable.");
}
}
use of com.google.inject.ProvisionException in project guice by google.
the class ProviderToInternalFactoryAdapter method get.
@Override
public T get() {
final Errors errors = new Errors();
try {
T t = injector.callInContext(new ContextualCallable<T>() {
@Override
public T call(InternalContext context) throws ErrorsException {
Dependency dependency = context.getDependency();
// binding, we'll fail properly elsewhere in the chain.
return internalFactory.get(errors, context, dependency, true);
}
});
errors.throwIfNewErrors(0);
return t;
} catch (ErrorsException e) {
throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
}
}
use of com.google.inject.ProvisionException in project guice by google.
the class InjectorImpl method getProviderOrThrow.
<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors) throws ErrorsException {
Key<T> key = dependency.getKey();
BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
final InternalFactory<? extends T> internalFactory = binding.getInternalFactory();
final Object source = binding.getSource();
return new Provider<T>() {
@Override
public T get() {
final Errors errors = new Errors(dependency);
try {
T t = callInContext(new ContextualCallable<T>() {
@Override
public T call(InternalContext context) throws ErrorsException {
Dependency previous = context.pushDependency(dependency, source);
try {
return internalFactory.get(errors, context, dependency, false);
} finally {
context.popStateAndSetDependency(previous);
}
}
});
errors.throwIfNewErrors(0);
return t;
} catch (ErrorsException e) {
throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
}
}
@Override
public String toString() {
return internalFactory.toString();
}
};
}
use of com.google.inject.ProvisionException in project guice by google.
the class FactoryProvider2 method invoke.
/**
* 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 {
// can call the default method implementation (and not our proxied version of it).
if (methodHandleByMethod.containsKey(method)) {
return methodHandleByMethod.get(method).invokeWithArguments(args);
}
if (method.getDeclaringClass().equals(Object.class)) {
if ("equals".equals(method.getName())) {
return proxy == args[0];
} else if ("hashCode".equals(method.getName())) {
return System.identityHashCode(proxy);
} else {
return method.invoke(this, args);
}
}
AssistData data = assistDataByMethod.get(method);
checkState(data != null, "No data for method: %s", method);
Provider<?> provider;
if (data.cachedBinding != null) {
// Try to get optimized form...
provider = data.cachedBinding.getProvider();
} else {
provider = getBindingFromNewInjector(method, args, data).getProvider();
}
try {
int p = 0;
for (ThreadLocalProvider tlp : data.providers) {
tlp.set(args[p++]);
}
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 = getOnlyElement(e.getErrorMessages());
Throwable cause = onlyError.getCause();
if (cause != null && canRethrow(method, cause)) {
throw cause;
}
}
throw e;
} finally {
for (ThreadLocalProvider tlp : data.providers) {
tlp.remove();
}
}
}
Aggregations