Search in sources :

Example 36 with Message

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);
    }
}
Also used : Message(com.google.inject.spi.Message) ConfigurationException(com.google.inject.ConfigurationException) Method(java.lang.reflect.Method) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Inject(com.google.inject.Inject) Toolable(com.google.inject.spi.Toolable)

Example 37 with Message

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");
    }
}
Also used : Message(com.google.inject.spi.Message) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 38 with Message

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;
}
Also used : Message(com.google.inject.spi.Message)

Example 39 with Message

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);
}
Also used : Message(com.google.inject.spi.Message) Dependency(com.google.inject.spi.Dependency) Logger(java.util.logging.Logger) Provider(com.google.inject.Provider) Errors(com.google.inject.internal.Errors) TypeLiteral(com.google.inject.TypeLiteral)

Example 40 with Message

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)));
}
Also used : Message(com.google.inject.spi.Message)

Aggregations

Message (com.google.inject.spi.Message)49 ProvisionException (com.google.inject.ProvisionException)9 CreationException (com.google.inject.CreationException)7 ArrayList (java.util.ArrayList)7 AbstractModule (com.google.inject.AbstractModule)6 Provider (com.google.inject.Provider)5 TypeLiteral (com.google.inject.TypeLiteral)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 Errors (com.google.inject.internal.Errors)4 InjectionPoint (com.google.inject.spi.InjectionPoint)4 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)3 GerritServerConfigModule (com.google.gerrit.server.config.GerritServerConfigModule)3 SitePath (com.google.gerrit.server.config.SitePath)3 Test (org.junit.Test)3 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)2 Function (com.google.common.base.Function)2 ImmutableList (com.google.common.collect.ImmutableList)2