use of com.google.inject.spi.Message in project google-gin by gwtplus.
the class GuiceBindingVisitor method visit.
public Void visit(InstanceBinding<? extends T> instanceBinding) {
T instance = instanceBinding.getInstance();
if (BindConstantBinding.isConstantKey(targetKey)) {
Context context = Context.forElement(instanceBinding);
bindingsCollection.addBinding(targetKey, bindingFactory.getBindConstantBinding(targetKey, instance, context));
} else {
messages.add(new Message(instanceBinding.getSource(), PrettyPrinter.format("Instance binding not supported; key=%s, inst=%s", targetKey, instance)));
}
return null;
}
use of com.google.inject.spi.Message in project guice by google.
the class InternalProvisionException method getErrors.
ImmutableList<Message> getErrors() {
ImmutableList.Builder<Message> builder = ImmutableList.builder();
// reverse them since sources are added as the exception propagates (so the first source is the
// last one added)
List<Object> newSources = Lists.reverse(sourcesToPrepend);
for (Message error : errors) {
builder.add(Messages.mergeSources(newSources, error));
}
return builder.build();
}
use of com.google.inject.spi.Message in project guice by google.
the class BinderTest method testUserReportedErrorsAreAlsoLogged.
public void testUserReportedErrorsAreAlsoLogged() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
addError(new Message("Whoops!", new IllegalArgumentException()));
}
});
fail();
} catch (CreationException expected) {
}
LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
assertContains(logRecord.getMessage(), "An exception was caught and reported. Message: java.lang.IllegalArgumentException");
}
use of com.google.inject.spi.Message in project guice by google.
the class CheckedProviderMethodsModule method createProviderMethod.
<T> CheckedProviderMethod<T> createProviderMethod(Binder binder, final Method method, CheckedProvides checkedProvides) {
// Class literal uses rawtypes.
@SuppressWarnings("rawtypes") Class<? extends CheckedProvider> throwingProvider = checkedProvides.value();
binder = binder.withSource(method);
Errors errors = new Errors(method);
// prepare the parameter providers
List<Dependency<?>> dependencies = Lists.newArrayList();
List<Provider<?>> parameterProviders = Lists.newArrayList();
List<TypeLiteral<?>> parameterTypes = typeLiteral.getParameterTypes(method);
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterTypes.size(); i++) {
Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]);
if (key.equals(LOGGER_KEY)) {
// If it was a Logger, change the key to be unique & bind it to a
// provider that provides a logger with a proper name.
// This solves issue 482 (returning a new anonymous logger on every call exhausts memory)
Key<Logger> loggerKey = Key.get(Logger.class, UniqueAnnotations.create());
binder.bind(loggerKey).toProvider(new LogProvider(method));
key = loggerKey;
}
dependencies.add(Dependency.get(key));
parameterProviders.add(binder.getProvider(key));
}
// Define T as the method's return type.
@SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
List<TypeLiteral<?>> exceptionTypes = typeLiteral.getExceptionTypes(method);
Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
for (Message message : errors.getMessages()) {
binder.addError(message);
}
return new CheckedProviderMethod<T>(key, method, delegate, ImmutableSet.copyOf(dependencies), parameterProviders, scopeAnnotation, throwingProvider, exceptionTypes, checkedProvides.scopeExceptions());
}
use of com.google.inject.spi.Message in project guice by google.
the class MessagesTest method provisionExceptionWithCustomErrorMessageIsSerializable.
@Test
public void provisionExceptionWithCustomErrorMessageIsSerializable() {
Throwable cause = null;
ProvisionException exception = new ProvisionException(ImmutableList.of(exampleError("Custom error"), new Message("Generic error", cause)));
assertThat(reserialize(exception)).hasMessageThat().isEqualTo("Unable to provision, see the following errors:\n\n" + "1) Custom error\n\n" + "2) Generic error\n\n" + "2 errors");
}
Aggregations