Search in sources :

Example 1 with ErrorsException

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

the class BindingProcessor method visit.

@Override
public <T> Boolean visit(Binding<T> command) {
    final Object source = command.getSource();
    if (Void.class.equals(command.getKey().getRawType())) {
        if (command instanceof ProviderInstanceBinding && ((ProviderInstanceBinding) command).getProviderInstance() instanceof ProviderMethod) {
            errors.voidProviderMethod();
        } else {
            errors.missingConstantValues();
        }
        return true;
    }
    final Key<T> key = command.getKey();
    Class<? super T> rawType = key.getTypeLiteral().getRawType();
    if (rawType == Provider.class) {
        errors.bindingToProvider();
        return true;
    }
    validateKey(command.getSource(), command.getKey());
    final Scoping scoping = Scopes.makeInjectable(((BindingImpl<?>) command).getScoping(), injector, errors);
    command.acceptTargetVisitor(new BindingTargetVisitor<T, Void>() {

        @Override
        public Void visit(InstanceBinding<? extends T> binding) {
            Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
            T instance = binding.getInstance();
            Initializable<T> ref = initializer.requestInjection(injector, instance, source, injectionPoints);
            ConstantFactory<? extends T> factory = new ConstantFactory<>(ref);
            InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
            putBinding(new InstanceBindingImpl<>(injector, key, source, scopedFactory, injectionPoints, instance));
            return null;
        }

        @Override
        public Void visit(ProviderInstanceBinding<? extends T> binding) {
            Provider<? extends T> provider = binding.getProviderInstance();
            Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
            Initializable<Provider<? extends T>> initializable = initializer.<Provider<? extends T>>requestInjection(injector, provider, source, injectionPoints);
            InternalFactory<T> factory = new InternalFactoryToProviderAdapter<>(initializable, source);
            InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
            putBinding(new ProviderInstanceBindingImpl<>(injector, key, source, scopedFactory, scoping, provider, injectionPoints));
            return null;
        }

        @Override
        public Void visit(ProviderKeyBinding<? extends T> binding) {
            Key<? extends Provider<? extends T>> providerKey = binding.getProviderKey();
            BoundProviderFactory<T> boundProviderFactory = new BoundProviderFactory<>(injector, providerKey, source);
            creationListeners.add(boundProviderFactory);
            InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, (InternalFactory<? extends T>) boundProviderFactory, scoping);
            putBinding(new LinkedProviderBindingImpl<>(injector, key, source, scopedFactory, scoping, providerKey));
            return null;
        }

        @Override
        public Void visit(LinkedKeyBinding<? extends T> binding) {
            Key<? extends T> linkedKey = binding.getLinkedKey();
            if (key.equals(linkedKey)) {
                errors.recursiveBinding();
            }
            FactoryProxy<T> factory = new FactoryProxy<>(injector, key, linkedKey, source);
            creationListeners.add(factory);
            InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
            putBinding(new LinkedBindingImpl<>(injector, key, source, scopedFactory, scoping, linkedKey));
            return null;
        }

        @Override
        public Void visit(UntargettedBinding<? extends T> untargetted) {
            // @ImplementedBy annotation or something.
            if (key.hasAnnotationType()) {
                errors.missingImplementation(key);
                putBinding(invalidBinding(injector, key, source));
                return null;
            }
            // This cast is safe after the preceding check.
            final BindingImpl<T> binding;
            try {
                binding = injector.createUnitializedBinding(key, scoping, source, errors);
                putBinding(binding);
            } catch (ErrorsException e) {
                errors.merge(e.getErrors());
                putBinding(invalidBinding(injector, key, source));
                return null;
            }
            uninitializedBindings.add(new Runnable() {

                @Override
                public void run() {
                    try {
                        ((InjectorImpl) binding.getInjector()).initializeBinding(binding, errors.withSource(source));
                    } catch (ErrorsException e) {
                        errors.merge(e.getErrors());
                    }
                }
            });
            return null;
        }

        @Override
        public Void visit(ExposedBinding<? extends T> binding) {
            throw new IllegalArgumentException("Cannot apply a non-module element");
        }

        @Override
        public Void visit(ConvertedConstantBinding<? extends T> binding) {
            throw new IllegalArgumentException("Cannot apply a non-module element");
        }

        @Override
        public Void visit(ConstructorBinding<? extends T> binding) {
            throw new IllegalArgumentException("Cannot apply a non-module element");
        }

        @Override
        public Void visit(ProviderBinding<? extends T> binding) {
            throw new IllegalArgumentException("Cannot apply a non-module element");
        }
    });
    return true;
}
Also used : ExposedBindingImpl(org.elasticsearch.common.inject.internal.ExposedBindingImpl) LinkedProviderBindingImpl(org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) ProviderInstanceBindingImpl(org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl) LinkedBindingImpl(org.elasticsearch.common.inject.internal.LinkedBindingImpl) UntargettedBindingImpl(org.elasticsearch.common.inject.internal.UntargettedBindingImpl) BindingImpl(org.elasticsearch.common.inject.internal.BindingImpl) Sets.newHashSet(org.elasticsearch.common.util.set.Sets.newHashSet) Set(java.util.Set) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) ProviderInstanceBindingImpl(org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl) Scoping(org.elasticsearch.common.inject.internal.Scoping) ProviderInstanceBindingImpl(org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) InternalFactory(org.elasticsearch.common.inject.internal.InternalFactory) ProviderMethod(org.elasticsearch.common.inject.internal.ProviderMethod) LinkedProviderBindingImpl(org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl) LinkedBindingImpl(org.elasticsearch.common.inject.internal.LinkedBindingImpl)

Example 2 with ErrorsException

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

the class InjectorImpl method convertConstantStringBinding.

/**
     * Converts a constant string binding to the required type.
     *
     * @return the binding if it could be resolved, or null if the binding doesn't exist
     * @throws org.elasticsearch.common.inject.internal.ErrorsException
     *          if there was an error resolving the binding
     */
private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) throws ErrorsException {
    // Find a constant string binding.
    Key<String> stringKey = key.ofType(String.class);
    BindingImpl<String> stringBinding = state.getExplicitBinding(stringKey);
    if (stringBinding == null || !stringBinding.isConstant()) {
        return null;
    }
    String stringValue = stringBinding.getProvider().get();
    Object source = stringBinding.getSource();
    // Find a matching type converter.
    TypeLiteral<T> type = key.getTypeLiteral();
    MatcherAndConverter matchingConverter = state.getConverter(stringValue, type, errors, source);
    if (matchingConverter == null) {
        // No converter can handle the given type.
        return null;
    }
    // Try to convert the string. A failed conversion results in an error.
    try {
        // This cast is safe because we double check below.
        @SuppressWarnings("unchecked") T converted = (T) matchingConverter.getTypeConverter().convert(stringValue, type);
        if (converted == null) {
            throw errors.converterReturnedNull(stringValue, source, type, matchingConverter).toException();
        }
        if (!type.getRawType().isInstance(converted)) {
            throw errors.conversionTypeError(stringValue, source, type, matchingConverter, converted).toException();
        }
        return new ConvertedConstantBindingImpl<>(this, key, converted, stringBinding);
    } catch (ErrorsException e) {
        throw e;
    } catch (RuntimeException e) {
        throw errors.conversionError(stringValue, source, type, matchingConverter, e).toException();
    }
}
Also used : MatcherAndConverter(org.elasticsearch.common.inject.internal.MatcherAndConverter) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException)

Example 3 with ErrorsException

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

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

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

Aggregations

ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)18 Errors (org.elasticsearch.common.inject.internal.Errors)14 ArrayList (java.util.ArrayList)4 BindingImpl (org.elasticsearch.common.inject.internal.BindingImpl)4 InstanceBindingImpl (org.elasticsearch.common.inject.internal.InstanceBindingImpl)4 InternalContext (org.elasticsearch.common.inject.internal.InternalContext)4 InternalFactory (org.elasticsearch.common.inject.internal.InternalFactory)4 LinkedBindingImpl (org.elasticsearch.common.inject.internal.LinkedBindingImpl)4 LinkedProviderBindingImpl (org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl)4 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 Set (java.util.Set)2 ExposedBindingImpl (org.elasticsearch.common.inject.internal.ExposedBindingImpl)2 MatcherAndConverter (org.elasticsearch.common.inject.internal.MatcherAndConverter)2 ProviderInstanceBindingImpl (org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl)2 ProviderMethod (org.elasticsearch.common.inject.internal.ProviderMethod)2 Scoping (org.elasticsearch.common.inject.internal.Scoping)2 SourceProvider (org.elasticsearch.common.inject.internal.SourceProvider)2 UntargettedBindingImpl (org.elasticsearch.common.inject.internal.UntargettedBindingImpl)2 Dependency (org.elasticsearch.common.inject.spi.Dependency)2