Search in sources :

Example 1 with Errors

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

the class InjectorImpl method getProviderOrThrow.

<T> Provider<T> getProviderOrThrow(final Key<T> key, Errors errors) throws ErrorsException {
    final InternalFactory<? extends T> factory = getInternalFactory(key, errors);
    // ES: optimize for a common case of read only instance getting from the parent...
    if (factory instanceof InternalFactory.Instance) {
        return new Provider<T>() {

            @Override
            public T get() {
                try {
                    return (T) ((InternalFactory.Instance) factory).get(null, null, null);
                } catch (ErrorsException e) {
                // ignore
                }
                // should never happen...
                assert false;
                return null;
            }
        };
    }
    final Dependency<T> dependency = Dependency.get(key);
    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 {
                        context.setDependency(dependency);
                        try {
                            return factory.get(errors, context, dependency);
                        } finally {
                            context.setDependency(null);
                        }
                    }
                });
                errors.throwIfNewErrors(0);
                return t;
            } catch (ErrorsException e) {
                throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
            }
        }

        @Override
        public String toString() {
            return factory.toString();
        }
    };
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) InternalContext(org.elasticsearch.common.inject.internal.InternalContext) InternalFactory(org.elasticsearch.common.inject.internal.InternalFactory) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) SourceProvider(org.elasticsearch.common.inject.internal.SourceProvider)

Example 2 with Errors

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

the class MembersInjectorImpl method injectMembers.

@Override
public void injectMembers(T instance) {
    Errors errors = new Errors(typeLiteral);
    try {
        injectAndNotify(instance, errors);
    } catch (ErrorsException e) {
        errors.merge(e.getErrors());
    }
    errors.throwProvisionExceptionIfErrorsExist();
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException)

Example 3 with Errors

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

the class MembersInjectorStore method getInjectors.

/**
     * Returns the injectors for the specified injection points.
     */
List<SingleMemberInjector> getInjectors(Set<InjectionPoint> injectionPoints, Errors errors) {
    List<SingleMemberInjector> injectors = new ArrayList<>();
    for (InjectionPoint injectionPoint : injectionPoints) {
        try {
            Errors errorsForMember = injectionPoint.isOptional() ? new Errors(injectionPoint) : errors.withSource(injectionPoint);
            SingleMemberInjector injector = injectionPoint.getMember() instanceof Field ? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember) : new SingleMethodInjector(this.injector, injectionPoint, errorsForMember);
            injectors.add(injector);
        } catch (ErrorsException ignoredForNow) {
        // ignored for now
        }
    }
    return Collections.unmodifiableList(injectors);
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) Field(java.lang.reflect.Field) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) ArrayList(java.util.ArrayList) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException)

Example 4 with Errors

use of org.elasticsearch.common.inject.internal.Errors 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 5 with Errors

use of org.elasticsearch.common.inject.internal.Errors 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)

Aggregations

Errors (org.elasticsearch.common.inject.internal.Errors)22 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)14 ConfigurationException (org.elasticsearch.common.inject.ConfigurationException)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 InternalContext (org.elasticsearch.common.inject.internal.InternalContext)4 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 Inject (org.elasticsearch.common.inject.Inject)2 BindingImpl (org.elasticsearch.common.inject.internal.BindingImpl)2 InstanceBindingImpl (org.elasticsearch.common.inject.internal.InstanceBindingImpl)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 SourceProvider (org.elasticsearch.common.inject.internal.SourceProvider)2 Dependency (org.elasticsearch.common.inject.spi.Dependency)2 Element (org.elasticsearch.common.inject.spi.Element)2 InjectionPoint (org.elasticsearch.common.inject.spi.InjectionPoint)2