Search in sources :

Example 16 with Errors

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

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

Example 18 with Errors

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

the class InjectionPoint method forConstructorOf.

/**
 * Returns a new injection point for the injectable constructor of {@code type}.
 *
 * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject},
 *             or a no-arguments constructor that is not private.
 * @throws ConfigurationException if there is no injectable constructor, more than one injectable
 *                                constructor, or if parameters of the injectable constructor are malformed, such as a
 *                                parameter with multiple binding annotations.
 */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type) {
    Class<?> rawType = getRawType(type.getType());
    Errors errors = new Errors(rawType);
    Constructor<?> injectableConstructor = null;
    for (Constructor<?> constructor : rawType.getConstructors()) {
        Inject inject = constructor.getAnnotation(Inject.class);
        if (inject != null) {
            if (inject.optional()) {
                errors.optionalConstructor(constructor);
            }
            if (injectableConstructor != null) {
                errors.tooManyConstructors(rawType);
            }
            injectableConstructor = constructor;
            checkForMisplacedBindingAnnotations(injectableConstructor, errors);
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    if (injectableConstructor != null) {
        return new InjectionPoint(type, injectableConstructor);
    }
    // If no annotated constructor is found, look for a no-arg constructor instead.
    try {
        Constructor<?> noArgConstructor = rawType.getConstructor();
        // Disallow private constructors on non-private classes (unless they have @Inject)
        if (Modifier.isPrivate(noArgConstructor.getModifiers()) && !Modifier.isPrivate(rawType.getModifiers())) {
            errors.missingConstructor(rawType);
            throw new ConfigurationException(errors.getMessages());
        }
        checkForMisplacedBindingAnnotations(noArgConstructor, errors);
        return new InjectionPoint(type, noArgConstructor);
    } catch (NoSuchMethodException e) {
        errors.missingConstructor(rawType);
        throw new ConfigurationException(errors.getMessages());
    }
}
Also used : Inject(org.elasticsearch.common.inject.Inject) Errors(org.elasticsearch.common.inject.internal.Errors) ConfigurationException(org.elasticsearch.common.inject.ConfigurationException)

Example 19 with Errors

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

the class AbstractProcessor method process.

public void process(InjectorImpl injector, List<Element> elements) {
    Errors errorsAnyElement = this.errors;
    this.injector = injector;
    try {
        for (Iterator<Element> i = elements.iterator(); i.hasNext(); ) {
            Element element = i.next();
            this.errors = errorsAnyElement.withSource(element.getSource());
            Boolean allDone = element.acceptVisitor(this);
            if (allDone) {
                i.remove();
            }
        }
    } finally {
        this.errors = errorsAnyElement;
        this.injector = null;
    }
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) Element(org.elasticsearch.common.inject.spi.Element)

Example 20 with Errors

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

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

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