Search in sources :

Example 1 with ProviderInstanceBinding

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

the class ModuleTestCase method assertMapMultiBinding.

/**
     * Configures the module and checks a Map<String, Class> of the "to" class
     * is bound to "theClass".
     */
public void assertMapMultiBinding(Module module, Class to, Class theClass) {
    List<Element> elements = Elements.getElements(module);
    Set<Type> bindings = new HashSet<>();
    boolean providerFound = false;
    for (Element element : elements) {
        if (element instanceof LinkedKeyBinding) {
            LinkedKeyBinding binding = (LinkedKeyBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                bindings.add(binding.getLinkedKey().getTypeLiteral().getType());
            }
        } else if (element instanceof ProviderInstanceBinding) {
            ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
            String setType = binding.getKey().getTypeLiteral().getType().toString();
            if (setType.equals("java.util.Map<java.lang.String, " + to.getName() + ">")) {
                providerFound = true;
            }
        }
    }
    if (bindings.contains(theClass) == false) {
        fail("Expected to find " + theClass.getName() + " as binding to " + to.getName() + ", found these classes:\n" + bindings);
    }
    assertTrue("Did not find provider for map of " + to.getName(), providerFound);
}
Also used : Type(java.lang.reflect.Type) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) HashSet(java.util.HashSet) LinkedKeyBinding(org.elasticsearch.common.inject.spi.LinkedKeyBinding)

Example 2 with ProviderInstanceBinding

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

use of org.elasticsearch.common.inject.spi.ProviderInstanceBinding in project crate by crate.

the class ModuleTestCase method assertInstanceBindingWithAnnotation.

/**
 * Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the
 * classes checked by the given annotation.
 */
private <T> void assertInstanceBindingWithAnnotation(Module module, Class<T> to, Predicate<T> tester, Class<? extends Annotation> annotation) {
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                if (annotation == null || annotation.equals(binding.getKey().getAnnotationType())) {
                    assertTrue(tester.test(to.cast(binding.getInstance())));
                    return;
                }
            }
        } else if (element instanceof ProviderInstanceBinding) {
            ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
                return;
            }
        }
    }
    StringBuilder s = new StringBuilder();
    for (Element element : elements) {
        s.append(element).append("\n");
    }
    fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s);
}
Also used : InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding)

Example 4 with ProviderInstanceBinding

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

the class ModuleTestCase method assertInstanceBindingWithAnnotation.

/**
     * Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the
     * classes checked by the given annotation.
     */
public <T> void assertInstanceBindingWithAnnotation(Module module, Class<T> to, Predicate<T> tester, Class<? extends Annotation> annotation) {
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                if (annotation == null || annotation.equals(binding.getKey().getAnnotationType())) {
                    assertTrue(tester.test(to.cast(binding.getInstance())));
                    return;
                }
            }
        } else if (element instanceof ProviderInstanceBinding) {
            ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                assertTrue(tester.test(to.cast(binding.getProviderInstance().get())));
                return;
            }
        }
    }
    StringBuilder s = new StringBuilder();
    for (Element element : elements) {
        s.append(element + "\n");
    }
    fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s);
}
Also used : ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding)

Example 5 with ProviderInstanceBinding

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

the class ModuleTestCase method bindAndGetInstance.

/**
     * Configures the module, and returns an instance  bound to the "to" class.
     */
public static <T> T bindAndGetInstance(Module module, Class<T> to) {
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                return to.cast(binding.getInstance());
            }
        } else if (element instanceof ProviderInstanceBinding) {
            ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
            if (to.equals(binding.getKey().getTypeLiteral().getType())) {
                return to.cast(binding.getProviderInstance().get());
            }
        }
    }
    fail("can't get instance for class " + to);
    // won't happen ;)
    return null;
}
Also used : ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding)

Aggregations

ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)7 Element (org.elasticsearch.common.inject.spi.Element)5 InstanceBinding (org.elasticsearch.common.inject.spi.InstanceBinding)3 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 BindingImpl (org.elasticsearch.common.inject.internal.BindingImpl)2 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)2 ExposedBindingImpl (org.elasticsearch.common.inject.internal.ExposedBindingImpl)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 ProviderInstanceBindingImpl (org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl)2 ProviderMethod (org.elasticsearch.common.inject.internal.ProviderMethod)2 Scoping (org.elasticsearch.common.inject.internal.Scoping)2 UntargettedBindingImpl (org.elasticsearch.common.inject.internal.UntargettedBindingImpl)2 LinkedKeyBinding (org.elasticsearch.common.inject.spi.LinkedKeyBinding)2 Collections.unmodifiableSet (java.util.Collections.unmodifiableSet)1 Sets.newHashSet (org.elasticsearch.common.util.set.Sets.newHashSet)1