use of com.google.inject.ProvisionException in project roboguice by roboguice.
the class MultibinderTest method testMultibinderSetForbidsNullElements.
public void testMultibinderSetForbidsNullElements() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
Multibinder.newSetBinder(binder(), String.class).addBinding().toProvider(Providers.<String>of(null));
}
});
try {
injector.getInstance(Key.get(setOfString));
fail();
} catch (ProvisionException expected) {
assertContains(expected.getMessage(), "1) Set injection failed due to null element");
}
}
use of com.google.inject.ProvisionException in project roboguice by roboguice.
the class ProviderToInternalFactoryAdapter method get.
public T get() {
final Errors errors = new Errors();
try {
T t = injector.callInContext(new ContextualCallable<T>() {
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 roboguice by roboguice.
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(Key<T> key, Provider<T> unscoped) {
return new Provider<T>() {
@Override
public T get() {
throw new OutOfScopeException("failure");
}
};
}
});
}
});
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", messages.get(0).getMessage());
assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
}
}
use of com.google.inject.ProvisionException in project roboguice by roboguice.
the class ServletTest method testScopeExceptions.
public void testScopeExceptions() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new ServletModule());
}
@Provides
@RequestScoped
String provideString() {
return "foo";
}
@Provides
@SessionScoped
Integer provideInteger() {
return 1;
}
@Provides
@RequestScoped
@Named("foo")
String provideNamedString() {
return "foo";
}
});
try {
injector.getInstance(String.class);
fail();
} catch (ProvisionException oose) {
assertContains(oose.getMessage(), "Cannot access scoped [java.lang.String].");
}
try {
injector.getInstance(Integer.class);
fail();
} catch (ProvisionException oose) {
assertContains(oose.getMessage(), "Cannot access scoped [java.lang.Integer].");
}
Key<?> key = Key.get(String.class, Names.named("foo"));
try {
injector.getInstance(key);
fail();
} catch (ProvisionException oose) {
assertContains(oose.getMessage(), "Cannot access scoped [" + Errors.convert(key) + "]");
}
}
use of com.google.inject.ProvisionException in project roboguice by roboguice.
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.
*/
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
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);
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