Search in sources :

Example 11 with ProvisionException

use of com.google.inject.ProvisionException in project guice by google.

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(final Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure: " + key.toString());
                        }
                    };
                }
            });
        }
    });
    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: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
    }
}
Also used : OutOfScopeException(com.google.inject.OutOfScopeException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) 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) Key(com.google.inject.Key)

Example 12 with ProvisionException

use of com.google.inject.ProvisionException in project guice by google.

the class BoundFieldModuleTest method testNonNullableFieldBound_lazy_rejectNull.

public void testNonNullableFieldBound_lazy_rejectNull() {
    LazyClass asProvider = new LazyClass();
    Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider));
    assertEquals(1, injector.getInstance(Integer.class).intValue());
    asProvider.foo = null;
    try {
        injector.getInstance(Integer.class);
        fail();
    } catch (ProvisionException e) {
        assertContains(e.getMessage(), "Binding to null values is only allowed for fields that are annotated @Nullable.");
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector)

Example 13 with ProvisionException

use of com.google.inject.ProvisionException in project guice by google.

the class ProviderToInternalFactoryAdapter method get.

@Override
public T get() {
    final Errors errors = new Errors();
    try {
        T t = injector.callInContext(new ContextualCallable<T>() {

            @Override
            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 14 with ProvisionException

use of com.google.inject.ProvisionException in project guice by google.

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors) throws ErrorsException {
    Key<T> key = dependency.getKey();
    BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT);
    final InternalFactory<? extends T> internalFactory = binding.getInternalFactory();
    final Object source = binding.getSource();
    return new Provider<T>() {

        @Override
        public T get() {
            final Errors errors = new Errors(dependency);
            try {
                T t = callInContext(new ContextualCallable<T>() {

                    @Override
                    public T call(InternalContext context) throws ErrorsException {
                        Dependency previous = context.pushDependency(dependency, source);
                        try {
                            return internalFactory.get(errors, context, dependency, false);
                        } finally {
                            context.popStateAndSetDependency(previous);
                        }
                    }
                });
                errors.throwIfNewErrors(0);
                return t;
            } catch (ErrorsException e) {
                throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
            }
        }

        @Override
        public String toString() {
            return internalFactory.toString();
        }
    };
}
Also used : Dependency(com.google.inject.spi.Dependency) SourceProvider(com.google.inject.internal.util.SourceProvider) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException)

Example 15 with ProvisionException

use of com.google.inject.ProvisionException in project guice by google.

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.
   */
@Override
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
    // can call the default method implementation (and not our proxied version of it).
    if (methodHandleByMethod.containsKey(method)) {
        return methodHandleByMethod.get(method).invokeWithArguments(args);
    }
    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);
    checkState(data != null, "No data for method: %s", 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