Search in sources :

Example 1 with ErrorsException

use of com.google.inject.internal.ErrorsException in project roboguice by roboguice.

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);
    }
}
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 2 with ErrorsException

use of com.google.inject.internal.ErrorsException 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);
    }
}
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 3 with ErrorsException

use of com.google.inject.internal.ErrorsException in project guice by google.

the class InjectionPoint method forMember.

private ImmutableList<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] paramterAnnotations) {
    Errors errors = new Errors(member);
    List<Dependency<?>> dependencies = Lists.newArrayList();
    int index = 0;
    for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
        try {
            Annotation[] parameterAnnotations = paramterAnnotations[index];
            Key<?> key = Annotations.getKey(parameterType, member, parameterAnnotations, errors);
            dependencies.add(newDependency(key, Nullability.allowsNull(parameterAnnotations), index));
            index++;
        } catch (ConfigurationException e) {
            errors.merge(e.getErrorMessages());
        } catch (ErrorsException e) {
            errors.merge(e.getErrors());
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    return ImmutableList.copyOf(dependencies);
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) ErrorsException(com.google.inject.internal.ErrorsException) Annotation(java.lang.annotation.Annotation)

Example 4 with ErrorsException

use of com.google.inject.internal.ErrorsException in project roboguice by roboguice.

the class InjectionPoint method forMember.

private ImmutableList<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] paramterAnnotations) {
    Errors errors = new Errors(member);
    Iterator<Annotation[]> annotationsIterator = Arrays.asList(paramterAnnotations).iterator();
    List<Dependency<?>> dependencies = Lists.newArrayList();
    int index = 0;
    for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
        try {
            Annotation[] parameterAnnotations = annotationsIterator.next();
            Key<?> key = Annotations.getKey(parameterType, member, parameterAnnotations, errors);
            dependencies.add(newDependency(key, Nullability.allowsNull(parameterAnnotations), index));
            index++;
        } catch (ConfigurationException e) {
            errors.merge(e.getErrorMessages());
        } catch (ErrorsException e) {
            errors.merge(e.getErrors());
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    return ImmutableList.copyOf(dependencies);
}
Also used : Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException) ErrorsException(com.google.inject.internal.ErrorsException) Annotation(java.lang.annotation.Annotation)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)4 Errors (com.google.inject.internal.Errors)4 ErrorsException (com.google.inject.internal.ErrorsException)4 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2