use of com.google.inject.spi.Message in project roboguice by roboguice.
the class FactoryProvider2 method initialize.
/**
* At injector-creation time, we initialize the invocation handler. At this time we make sure
* all factory methods will be able to build the target types.
*/
@Inject
@Toolable
void initialize(Injector injector) {
if (this.injector != null) {
throw new ConfigurationException(ImmutableList.of(new Message(FactoryProvider2.class, "Factories.create() factories may only be used in one Injector!")));
}
this.injector = injector;
for (Map.Entry<Method, AssistData> entry : assistDataByMethod.entrySet()) {
Method method = entry.getKey();
AssistData data = entry.getValue();
Object[] args;
if (!data.optimized) {
args = new Object[method.getParameterTypes().length];
Arrays.fill(args, "dummy object for validating Factories");
} else {
// won't be used -- instead will bind to data.providers.
args = null;
}
// throws if the binding isn't properly configured
getBindingFromNewInjector(method, args, data);
}
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class TypeListenerTest method testAddErrors.
public void testAddErrors() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
requestInjection(new Object());
bindListener(Matchers.any(), new TypeListener() {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.addError("There was an error on %s", type);
encounter.addError(new IllegalArgumentException("whoops!"));
encounter.addError(new Message("And another problem"));
encounter.addError(new IllegalStateException());
}
});
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) There was an error on java.lang.Object", "2) An exception was caught and reported. Message: whoops!", "3) And another problem", "4) An exception was caught and reported. Message: null", "4 errors");
}
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class Errors method addMessage.
private Errors addMessage(Throwable cause, String messageFormat, Object... arguments) {
String message = format(messageFormat, arguments);
addMessage(new Message(getSources(), message, cause));
return this;
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class CheckedProviderMethodsModule method createProviderMethod.
<T> CheckedProviderMethod<T> createProviderMethod(Binder binder, final Method method, Class<? extends CheckedProvider> throwingProvider) {
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);
}
use of com.google.inject.spi.Message in project roboguice by roboguice.
the class BoundFieldModule method throwBoundFieldException.
private void throwBoundFieldException(Field field, String format, Object... args) {
Preconditions.checkNotNull(binder);
String source = String.format("%s field %s", field.getDeclaringClass().getName(), field.getName());
throw new BoundFieldException(new Message(source, String.format(format, args)));
}
Aggregations