Search in sources :

Example 1 with Element

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

the class ModuleTestCase method assertMapInstanceBinding.

/**
     * Configures the module, and ensures a map exists between the "keyType" and "valueType",
     * and that all of the "expected" values are bound.
     */
@SuppressWarnings("unchecked")
public <K, V> void assertMapInstanceBinding(Module module, Class<K> keyType, Class<V> valueType, Map<K, V> expected) throws Exception {
    // this method is insane because java type erasure makes it incredibly difficult...
    Map<K, Key> keys = new HashMap<>();
    Map<Key, V> values = new HashMap<>();
    List<Element> elements = Elements.getElements(module);
    for (Element element : elements) {
        if (element instanceof InstanceBinding) {
            InstanceBinding binding = (InstanceBinding) element;
            if (binding.getKey().getRawType().equals(valueType)) {
                values.put(binding.getKey(), (V) binding.getInstance());
            } else if (binding.getInstance() instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) binding.getInstance();
                Object key = entry.getKey();
                Object providerValue = entry.getValue();
                if (key.getClass().equals(keyType) && providerValue instanceof ProviderLookup.ProviderImpl) {
                    ProviderLookup.ProviderImpl provider = (ProviderLookup.ProviderImpl) providerValue;
                    keys.put((K) key, provider.getKey());
                }
            }
        }
    }
    for (Map.Entry<K, V> entry : expected.entrySet()) {
        Key valueKey = keys.get(entry.getKey());
        assertNotNull("Could not find binding for key [" + entry.getKey() + "], found these keys:\n" + keys.keySet(), valueKey);
        V value = values.get(valueKey);
        assertNotNull("Could not find value for instance key [" + valueKey + "], found these bindings:\n" + elements);
        assertEquals(entry.getValue(), value);
    }
}
Also used : HashMap(java.util.HashMap) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) InstanceBinding(org.elasticsearch.common.inject.spi.InstanceBinding) ProviderLookup(org.elasticsearch.common.inject.spi.ProviderLookup) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Element

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

the class ModuleTestCase method assertMapMultiBinding.

/**
     * Configures the module and checks a Map&lt;String, Class&gt; 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 3 with Element

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

the class PrivateElementsImpl method applyTo.

@Override
public void applyTo(Binder binder) {
    PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
    for (Element element : getElements()) {
        element.applyTo(privateBinder);
    }
    // ensure exposedKeysToSources is populated
    getExposedKeys();
    for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
        privateBinder.withSource(entry.getValue()).expose(entry.getKey());
    }
}
Also used : PrivateBinder(org.elasticsearch.common.inject.PrivateBinder) Element(org.elasticsearch.common.inject.spi.Element) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Key(org.elasticsearch.common.inject.Key)

Example 4 with Element

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

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

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)

Aggregations

Element (org.elasticsearch.common.inject.spi.Element)12 ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)6 InstanceBinding (org.elasticsearch.common.inject.spi.InstanceBinding)4 Map (java.util.Map)3 LinkedKeyBinding (org.elasticsearch.common.inject.spi.LinkedKeyBinding)3 Type (java.lang.reflect.Type)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Key (org.elasticsearch.common.inject.Key)2 PrivateBinder (org.elasticsearch.common.inject.PrivateBinder)2 Errors (org.elasticsearch.common.inject.internal.Errors)2 HashMap (java.util.HashMap)1 ProviderLookup (org.elasticsearch.common.inject.spi.ProviderLookup)1 UntargettedBinding (org.elasticsearch.common.inject.spi.UntargettedBinding)1