Search in sources :

Example 1 with InjectionPoint

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

the class ConstructorInjectorStore method createConstructor.

private <T> ConstructorInjector<T> createConstructor(TypeLiteral<T> type, Errors errors) throws ErrorsException {
    int numErrorsBefore = errors.size();
    InjectionPoint injectionPoint;
    try {
        injectionPoint = InjectionPoint.forConstructorOf(type);
    } catch (ConfigurationException e) {
        errors.merge(e.getErrorMessages());
        throw errors.toException();
    }
    SingleParameterInjector<?>[] constructorParameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
    MembersInjectorImpl<T> membersInjector = injector.membersInjectorStore.get(type, errors);
    ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<>(injectionPoint);
    errors.throwIfNewErrors(numErrorsBefore);
    return new ConstructorInjector<>(membersInjector.getInjectionPoints(), factory.create(), constructorParameterInjectors, membersInjector);
}
Also used : InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint)

Example 2 with InjectionPoint

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

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

Example 4 with InjectionPoint

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

the class MembersInjectorStore method createWithListeners.

/**
     * Creates a new members injector and attaches both injection listeners and method aspects.
     */
private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) throws ErrorsException {
    int numErrorsBefore = errors.size();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        errors.merge(e.getErrorMessages());
        injectionPoints = e.getPartialValue();
    }
    List<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors);
    errors.throwIfNewErrors(numErrorsBefore);
    EncounterImpl<T> encounter = new EncounterImpl<>(errors, injector.lookups);
    for (TypeListenerBinding typeListener : typeListenerBindings) {
        if (typeListener.getTypeMatcher().matches(type)) {
            try {
                typeListener.getListener().hear(type, encounter);
            } catch (RuntimeException e) {
                errors.errorNotifyingTypeListener(typeListener, type, e);
            }
        }
    }
    encounter.invalidate();
    errors.throwIfNewErrors(numErrorsBefore);
    return new MembersInjectorImpl<>(injector, type, encounter, injectors);
}
Also used : TypeListenerBinding(org.elasticsearch.common.inject.spi.TypeListenerBinding) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint)

Aggregations

InjectionPoint (org.elasticsearch.common.inject.spi.InjectionPoint)4 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 ArrayList (java.util.ArrayList)1 Key (org.elasticsearch.common.inject.Key)1 TypeLiteral (org.elasticsearch.common.inject.TypeLiteral)1 Errors (org.elasticsearch.common.inject.internal.Errors)1 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)1 Dependency (org.elasticsearch.common.inject.spi.Dependency)1 TypeListenerBinding (org.elasticsearch.common.inject.spi.TypeListenerBinding)1