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);
}
}
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<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);
}
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());
}
}
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);
}
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;
}
}
Aggregations