Search in sources :

Example 21 with ConfigurationException

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

the class FactoryProvider method newFactory.

public static <F> Provider<F> newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
    Map<Method, AssistedConstructor<?>> factoryMethodToConstructor = createMethodMapping(factoryType, implementationType);
    if (!factoryMethodToConstructor.isEmpty()) {
        return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
    } else {
        BindingCollector collector = new BindingCollector();
        // Preserving backwards-compatibility:  Map all return types in a factory
        // interface to the passed implementation type.
        Errors errors = new Errors();
        Key<?> implementationKey = Key.get(implementationType);
        try {
            for (Method method : factoryType.getRawType().getMethods()) {
                Key<?> returnType = getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
                if (!implementationKey.equals(returnType)) {
                    collector.addBinding(returnType, implementationType);
                }
            }
        } catch (ErrorsException e) {
            throw new ConfigurationException(e.getErrors().getMessages());
        }
        return new FactoryProvider2<F>(Key.get(factoryType), collector, /* userLookups= */
        null);
    }
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) ErrorsException(com.google.inject.internal.ErrorsException) Method(java.lang.reflect.Method)

Example 22 with ConfigurationException

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

the class InjectorImpl method getProvider.

@Override
public <T> Provider<T> getProvider(final Key<T> key) {
    checkNotNull(key, "key");
    Errors errors = new Errors(key);
    try {
        Provider<T> result = getProviderOrThrow(Dependency.get(key), errors);
        errors.throwIfNewErrors(0);
        return result;
    } catch (ErrorsException e) {
        ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
        throw exception;
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException)

Example 23 with ConfigurationException

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

the class InjectorImpl method getBinding.

/**
 * Returns the binding for {@code key}
 */
@Override
public <T> BindingImpl<T> getBinding(Key<T> key) {
    Errors errors = new Errors(checkNotNull(key, "key"));
    try {
        BindingImpl<T> result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT);
        errors.throwConfigurationExceptionIfErrorsExist();
        return result;
    } catch (ErrorsException e) {
        ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
        throw exception;
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException)

Example 24 with ConfigurationException

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

the class InjectorImpl method getMembersInjector.

@Override
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
    checkNotNull(typeLiteral, "typeLiteral");
    userRequestedMembersInjectorTypes.add(typeLiteral);
    Errors errors = new Errors(typeLiteral);
    try {
        return membersInjectorStore.get(typeLiteral, errors);
    } catch (ErrorsException e) {
        ConfigurationException exception = new ConfigurationException(errors.merge(e.getErrors()).getMessages());
        throw exception;
    }
}
Also used : ConfigurationException(com.google.inject.ConfigurationException)

Example 25 with ConfigurationException

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

the class ConstructorBindingImpl method create.

/**
 * @param constructorInjector the constructor to use, or {@code null} to use the default.
 * @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should only
 *     succeed if retrieved from a linked binding
 */
static <T> ConstructorBindingImpl<T> create(InjectorImpl injector, Key<T> key, InjectionPoint constructorInjector, Object source, Scoping scoping, Errors errors, boolean failIfNotLinked, boolean atInjectRequired) throws ErrorsException {
    int numErrors = errors.size();
    Class<?> rawType = constructorInjector == null ? key.getTypeLiteral().getRawType() : constructorInjector.getDeclaringType().getRawType();
    // We can't inject abstract classes.
    if (Modifier.isAbstract(rawType.getModifiers())) {
        errors.missingImplementationWithHint(key, injector);
    }
    // Error: Inner class.
    if (Classes.isInnerClass(rawType)) {
        errors.cannotInjectInnerClass(rawType);
    }
    if (KotlinSupport.getInstance().isLocalClass(rawType)) {
        errors.cannotInjectLocalClass(rawType);
    }
    errors.throwIfNewErrors(numErrors);
    // Find a constructor annotated @Inject
    if (constructorInjector == null) {
        try {
            constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral(), atInjectRequired);
        } catch (ConfigurationException e) {
            throw errors.merge(e.getErrorMessages()).toException();
        }
    }
    // if no scope is specified, look for a scoping annotation on the concrete class
    if (!scoping.isExplicitlyScoped()) {
        Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
        Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
        if (scopeAnnotation != null) {
            scoping = Scoping.makeInjectable(Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType));
        }
    }
    errors.throwIfNewErrors(numErrors);
    Factory<T> factoryFactory = new Factory<>(failIfNotLinked, key);
    InternalFactory<? extends T> scopedFactory = Scoping.scope(key, injector, factoryFactory, source, scoping);
    return new ConstructorBindingImpl<T>(injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) InjectionPoint(com.google.inject.spi.InjectionPoint)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)62 Injector (com.google.inject.Injector)16 Errors (com.google.inject.internal.Errors)13 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Method (java.lang.reflect.Method)7 AbstractModule (com.google.inject.AbstractModule)5 Inject (com.google.inject.Inject)5 ErrorsException (com.google.inject.internal.ErrorsException)5 Annotation (java.lang.annotation.Annotation)5 TypeLiteral (com.google.inject.TypeLiteral)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)3 Module (com.google.inject.Module)3 Message (com.google.inject.spi.Message)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Binding (com.google.inject.Binding)2