Search in sources :

Example 6 with ProvisionException

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");
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 7 with ProvisionException

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

Example 8 with ProvisionException

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());
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) OutOfScopeException(com.google.inject.OutOfScopeException) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 9 with ProvisionException

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) + "]");
    }
}
Also used : Named(com.google.inject.name.Named) ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 10 with ProvisionException

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

Aggregations

ProvisionException (com.google.inject.ProvisionException)57 Injector (com.google.inject.Injector)22 AbstractModule (com.google.inject.AbstractModule)20 Module (com.google.inject.Module)11 Provider (com.google.inject.Provider)9 OutOfScopeException (com.google.inject.OutOfScopeException)6 TypeLiteral (com.google.inject.TypeLiteral)6 Key (com.google.inject.Key)5 Message (com.google.inject.spi.Message)5 ImmutableList (com.google.common.collect.ImmutableList)4 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)4 OrmException (com.google.gwtorm.server.OrmException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Provides (com.google.inject.Provides)3 Dependency (com.google.inject.spi.Dependency)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Path (java.nio.file.Path)3