Search in sources :

Example 6 with ErrorsException

use of org.elasticsearch.common.inject.internal.ErrorsException in project elasticsearch by elastic.

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();
                return internalFactory.get(errors, context, dependency);
            }
        });
        errors.throwIfNewErrors(0);
        return t;
    } catch (ErrorsException e) {
        throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) Dependency(org.elasticsearch.common.inject.spi.Dependency)

Example 7 with ErrorsException

use of org.elasticsearch.common.inject.internal.ErrorsException in project elasticsearch by elastic.

the class InjectionPoint method forMember.

private List<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] parameterAnnotations) {
    Errors errors = new Errors(member);
    Iterator<Annotation[]> annotationsIterator = Arrays.asList(parameterAnnotations).iterator();
    List<Dependency<?>> dependencies = new ArrayList<>();
    int index = 0;
    for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
        try {
            Annotation[] paramAnnotations = annotationsIterator.next();
            Key<?> key = Annotations.getKey(parameterType, member, paramAnnotations, errors);
            dependencies.add(newDependency(key, Nullability.allowsNull(paramAnnotations), index));
            index++;
        } catch (ErrorsException e) {
            errors.merge(e.getErrors());
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    return Collections.unmodifiableList(dependencies);
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) ArrayList(java.util.ArrayList) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) Annotation(java.lang.annotation.Annotation)

Example 8 with ErrorsException

use of org.elasticsearch.common.inject.internal.ErrorsException in project elasticsearch by elastic.

the class InjectorImpl method getProvider.

@Override
public <T> Provider<T> getProvider(final Key<T> key) {
    Errors errors = new Errors(key);
    try {
        Provider<T> result = getProviderOrThrow(key, errors);
        errors.throwIfNewErrors(0);
        return result;
    } catch (ErrorsException e) {
        throw new ConfigurationException(errors.merge(e.getErrors()).getMessages());
    }
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException)

Example 9 with ErrorsException

use of org.elasticsearch.common.inject.internal.ErrorsException in project elasticsearch by elastic.

the class InjectorImpl method createJustInTimeBinding.

/**
     * Returns a new just-in-time binding created by resolving {@code key}. The strategies used to
     * create just-in-time bindings are:
     * <ol>
     * <li>Internalizing Providers. If the requested binding is for {@code Provider<T>}, we delegate
     * to the binding for {@code T}.
     * <li>Converting constants.
     * <li>ImplementedBy and ProvidedBy annotations. Only for unannotated keys.
     * <li>The constructor of the raw type. Only for unannotated keys.
     * </ol>
     *
     * @throws org.elasticsearch.common.inject.internal.ErrorsException
     *          if the binding cannot be created.
     */
<T> BindingImpl<T> createJustInTimeBinding(Key<T> key, Errors errors) throws ErrorsException {
    if (state.isBlacklisted(key)) {
        throw errors.childBindingAlreadySet(key).toException();
    }
    // Handle cases where T is a Provider<?>.
    if (isProvider(key)) {
        // These casts are safe. We know T extends Provider<X> and that given Key<Provider<X>>,
        // createProviderBinding() will return BindingImpl<Provider<X>>.
        @SuppressWarnings("unchecked") BindingImpl binding = createProviderBinding((Key) key, errors);
        return binding;
    }
    // Handle cases where T is a MembersInjector<?>
    if (isMembersInjector(key)) {
        // These casts are safe. T extends MembersInjector<X> and that given Key<MembersInjector<X>>,
        // createMembersInjectorBinding() will return BindingImpl<MembersInjector<X>>.
        @SuppressWarnings("unchecked") BindingImpl binding = createMembersInjectorBinding((Key) key, errors);
        return binding;
    }
    // Try to convert a constant string binding to the requested type.
    BindingImpl<T> convertedBinding = convertConstantStringBinding(key, errors);
    if (convertedBinding != null) {
        return convertedBinding;
    }
    // If the key has an annotation...
    if (key.hasAnnotationType()) {
        // Look for a binding without annotation attributes or return null.
        if (key.hasAttributes()) {
            try {
                Errors ignored = new Errors();
                return getBindingOrThrow(key.withoutAttributes(), ignored);
            } catch (ErrorsException ignored) {
            // throw with a more appropriate message below
            }
        }
        throw errors.missingImplementation(key).toException();
    }
    Object source = key.getTypeLiteral().getRawType();
    BindingImpl<T> binding = createUnitializedBinding(key, Scoping.UNSCOPED, source, errors);
    initializeBinding(binding, errors);
    return binding;
}
Also used : LinkedProviderBindingImpl(org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) LinkedBindingImpl(org.elasticsearch.common.inject.internal.LinkedBindingImpl) BindingImpl(org.elasticsearch.common.inject.internal.BindingImpl) Errors(org.elasticsearch.common.inject.internal.Errors) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException)

Aggregations

ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)9 Errors (org.elasticsearch.common.inject.internal.Errors)7 ArrayList (java.util.ArrayList)2 BindingImpl (org.elasticsearch.common.inject.internal.BindingImpl)2 InstanceBindingImpl (org.elasticsearch.common.inject.internal.InstanceBindingImpl)2 InternalContext (org.elasticsearch.common.inject.internal.InternalContext)2 InternalFactory (org.elasticsearch.common.inject.internal.InternalFactory)2 LinkedBindingImpl (org.elasticsearch.common.inject.internal.LinkedBindingImpl)2 LinkedProviderBindingImpl (org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Collections.unmodifiableSet (java.util.Collections.unmodifiableSet)1 Set (java.util.Set)1 ExposedBindingImpl (org.elasticsearch.common.inject.internal.ExposedBindingImpl)1 MatcherAndConverter (org.elasticsearch.common.inject.internal.MatcherAndConverter)1 ProviderInstanceBindingImpl (org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl)1 ProviderMethod (org.elasticsearch.common.inject.internal.ProviderMethod)1 Scoping (org.elasticsearch.common.inject.internal.Scoping)1 SourceProvider (org.elasticsearch.common.inject.internal.SourceProvider)1 UntargettedBindingImpl (org.elasticsearch.common.inject.internal.UntargettedBindingImpl)1