Search in sources :

Example 6 with Message

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

Example 7 with Message

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

Example 8 with Message

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

Example 9 with Message

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());
}
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 10 with Message

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

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