Search in sources :

Example 1 with Dependency

use of org.elasticsearch.common.inject.spi.Dependency 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 2 with Dependency

use of org.elasticsearch.common.inject.spi.Dependency in project elasticsearch by elastic.

the class ProviderMethodsModule method createProviderMethod.

<T> ProviderMethod<T> createProviderMethod(Binder binder, final Method method) {
    binder = binder.withSource(method);
    Errors errors = new Errors(method);
    // prepare the parameter providers
    Set<Dependency<?>> dependencies = new HashSet<>();
    List<Provider<?>> parameterProviders = new ArrayList<>();
    List<TypeLiteral<?>> parameterTypes = typeLiteral.getParameterTypes(method);
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < parameterTypes.size(); i++) {
        Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]);
        dependencies.add(Dependency.get(key));
        parameterProviders.add(binder.getProvider(key));
    }
    // Define T as the method's return type.
    @SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
    Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
    Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
    for (Message message : errors.getMessages()) {
        binder.addError(message);
    }
    return new ProviderMethod<>(key, method, delegate, unmodifiableSet(dependencies), parameterProviders, scopeAnnotation);
}
Also used : Message(org.elasticsearch.common.inject.spi.Message) ArrayList(java.util.ArrayList) Dependency(org.elasticsearch.common.inject.spi.Dependency) Provider(org.elasticsearch.common.inject.Provider) TypeLiteral(org.elasticsearch.common.inject.TypeLiteral) HashSet(java.util.HashSet)

Example 3 with Dependency

use of org.elasticsearch.common.inject.spi.Dependency in project elasticsearch by elastic.

the class Errors method formatSource.

public static void formatSource(Formatter formatter, Object source) {
    if (source instanceof Dependency) {
        Dependency<?> dependency = (Dependency<?>) source;
        InjectionPoint injectionPoint = dependency.getInjectionPoint();
        if (injectionPoint != null) {
            formatInjectionPoint(formatter, dependency, injectionPoint);
        } else {
            formatSource(formatter, dependency.getKey());
        }
    } else if (source instanceof InjectionPoint) {
        formatInjectionPoint(formatter, null, (InjectionPoint) source);
    } else if (source instanceof Class) {
        formatter.format("  at %s%n", StackTraceElements.forType((Class<?>) source));
    } else if (source instanceof Member) {
        formatter.format("  at %s%n", StackTraceElements.forMember((Member) source));
    } else if (source instanceof TypeLiteral) {
        formatter.format("  while locating %s%n", source);
    } else if (source instanceof Key) {
        Key<?> key = (Key<?>) source;
        formatter.format("  while locating %s%n", convert(key));
    } else {
        formatter.format("  at %s%n", source);
    }
}
Also used : TypeLiteral(org.elasticsearch.common.inject.TypeLiteral) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) Dependency(org.elasticsearch.common.inject.spi.Dependency) Member(java.lang.reflect.Member) Key(org.elasticsearch.common.inject.Key)

Aggregations

Dependency (org.elasticsearch.common.inject.spi.Dependency)3 TypeLiteral (org.elasticsearch.common.inject.TypeLiteral)2 Member (java.lang.reflect.Member)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Key (org.elasticsearch.common.inject.Key)1 Provider (org.elasticsearch.common.inject.Provider)1 Errors (org.elasticsearch.common.inject.internal.Errors)1 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)1 InternalContext (org.elasticsearch.common.inject.internal.InternalContext)1 InjectionPoint (org.elasticsearch.common.inject.spi.InjectionPoint)1 Message (org.elasticsearch.common.inject.spi.Message)1