use of com.google.inject.Key in project guice by google.
the class SpiUtils method setModuleTest.
@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
List<BindResult> bindResults = Lists.newArrayList(results);
List<Element> elements = Elements.getElements(modules);
Visitor<T> visitor = new Visitor<T>();
MultibinderBinding<Set<T>> multibinder = null;
for (Element element : elements) {
if (element instanceof Binding && ((Binding) element).getKey().equals(setKey)) {
multibinder = (MultibinderBinding<Set<T>>) ((Binding) element).acceptTargetVisitor(visitor);
break;
}
}
assertNotNull(multibinder);
assertEquals(elementType, multibinder.getElementTypeLiteral());
List<Object> otherMultibinders = Lists.newArrayList();
Set<Element> otherContains = new HashSet<Element>();
List<Element> otherElements = Lists.newArrayList();
int duplicates = 0;
Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
Indexer indexer = new Indexer(null);
boolean collectionOfProvidersMatch = false;
boolean collectionOfJavaxProvidersMatch = false;
for (Element element : elements) {
boolean contains = multibinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key key = null;
if (element instanceof Binding) {
Binding binding = (Binding) element;
if (indexer.isIndexable(binding) && !setOfIndexed.add((IndexedBinding) binding.acceptTargetVisitor(indexer))) {
duplicates++;
}
key = binding.getKey();
Object visited = binding.acceptTargetVisitor(visitor);
if (visited != null) {
matched = true;
if (visited.equals(multibinder)) {
assertTrue(contains);
} else {
otherMultibinders.add(visited);
}
}
}
if (collectionOfProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfProvidersMatch = true;
} else if (collectionOfJavaxProvidersKey.equals(key)) {
assertTrue(contains);
assertFalse(matched);
collectionOfJavaxProvidersMatch = true;
} else if (!matched && contains) {
otherContains.add(element);
}
}
if (allowDuplicates) {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + 1 + duplicates, otherContains.size());
} else {
assertEquals("wrong contained elements: " + otherContains, bindResults.size() + duplicates, otherContains.size());
}
assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
assertTrue(collectionOfProvidersMatch);
assertTrue(collectionOfJavaxProvidersMatch);
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
use of com.google.inject.Key in project guice by google.
the class SpiUtils method optionalModuleTest.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> void optionalModuleTest(Key<T> keyType, Iterable<? extends Module> modules, int expectedOtherOptionalBindings, BindResult<?> expectedDefault, BindResult<?> expectedActual, BindResult<?> expectedUserLinkedActual) {
if (expectedUserLinkedActual != null) {
assertNull("cannot have actual if expecting user binding", expectedActual);
assertNull("cannot have default if expecting user binding", expectedDefault);
}
Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
Map<Key<?>, Binding<?>> indexed = index(elements);
Key<Optional<T>> optionalKey = keyType.ofType(RealOptionalBinder.optionalOf(keyType.getTypeLiteral()));
Key<?> javaOptionalKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOf(keyType.getTypeLiteral())) : null;
Visitor visitor = new Visitor();
OptionalBinderBinding<Optional<T>> optionalBinder = null;
OptionalBinderBinding<?> javaOptionalBinder = null;
Key<?> defaultKey = null;
Key<?> actualKey = null;
Binding optionalBinding = indexed.get(optionalKey);
optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
if (HAS_JAVA_OPTIONAL) {
Binding javaOptionalBinding = indexed.get(javaOptionalKey);
javaOptionalBinder = (OptionalBinderBinding) javaOptionalBinding.acceptTargetVisitor(visitor);
}
// Locate the defaultKey & actualKey
for (Element element : elements) {
if (optionalBinder.containsElement(element) && element instanceof Binding) {
Binding binding = (Binding) element;
if (isSourceEntry(binding, RealOptionalBinder.Source.DEFAULT)) {
defaultKey = binding.getKey();
} else if (isSourceEntry(binding, RealOptionalBinder.Source.ACTUAL)) {
actualKey = binding.getKey();
}
}
}
assertNotNull(optionalBinder);
if (HAS_JAVA_OPTIONAL) {
assertNotNull(javaOptionalBinder);
}
assertEquals(expectedDefault == null, defaultKey == null);
assertEquals(expectedActual == null, actualKey == null);
Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.optionalOfJavaxProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalJavaxProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOfJavaxProvider(keyType.getTypeLiteral())) : null;
Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(RealOptionalBinder.optionalOfProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOfProvider(keyType.getTypeLiteral())) : null;
boolean keyMatch = false;
boolean optionalKeyMatch = false;
boolean javaOptionalKeyMatch = false;
boolean optionalJavaxProviderKeyMatch = false;
boolean javaOptionalJavaxProviderKeyMatch = false;
boolean optionalProviderKeyMatch = false;
boolean javaOptionalProviderKeyMatch = false;
boolean defaultMatch = false;
boolean actualMatch = false;
List<Object> otherOptionalElements = Lists.newArrayList();
List<Element> otherContains = Lists.newArrayList();
List<Element> nonContainedElements = Lists.newArrayList();
for (Element element : elements) {
boolean contains = optionalBinder.containsElement(element);
if (HAS_JAVA_OPTIONAL) {
assertEquals(contains, javaOptionalBinder.containsElement(element));
}
if (!contains) {
nonContainedElements.add(element);
}
Key key = null;
Binding b = null;
if (element instanceof Binding) {
b = (Binding) element;
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof OptionalBinderBinding) {
if (visited.equals(optionalBinder)) {
assertTrue(contains);
} else if (HAS_JAVA_OPTIONAL && visited.equals(javaOptionalBinder)) {
assertTrue(contains);
} else {
otherOptionalElements.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (key != null && key.equals(keyType)) {
// keyType might match because a user bound it
// (which is possible in a purely absent OptionalBinder)
assertEquals(expectedDefault != null || expectedActual != null, contains);
if (contains) {
keyMatch = true;
}
} else if (key != null && key.equals(optionalKey)) {
assertTrue(contains);
optionalKeyMatch = true;
} else if (key != null && key.equals(javaOptionalKey)) {
assertTrue(contains);
javaOptionalKeyMatch = true;
} else if (key != null && key.equals(optionalJavaxProviderKey)) {
assertTrue(contains);
optionalJavaxProviderKeyMatch = true;
} else if (key != null && key.equals(javaOptionalJavaxProviderKey)) {
assertTrue(contains);
javaOptionalJavaxProviderKeyMatch = true;
} else if (key != null && key.equals(optionalProviderKey)) {
assertTrue(contains);
optionalProviderKeyMatch = true;
} else if (key != null && key.equals(javaOptionalProviderKey)) {
assertTrue(contains);
javaOptionalProviderKeyMatch = true;
} else if (key != null && key.equals(defaultKey)) {
assertTrue(contains);
if (b != null) {
// otherwise it might just be a ProviderLookup into it
assertTrue("expected: " + expectedDefault + ", but was: " + b, matches(b, expectedDefault));
defaultMatch = true;
}
} else if (key != null && key.equals(actualKey)) {
assertTrue(contains);
if (b != null) {
// otherwise it might just be a ProviderLookup into it
assertTrue("expected: " + expectedActual + ", but was: " + b, matches(b, expectedActual));
actualMatch = true;
}
} else if (contains) {
otherContains.add(element);
}
}
// only expect a keymatch if either default or actual are set
assertEquals(expectedDefault != null || expectedActual != null, keyMatch);
assertTrue(optionalKeyMatch);
assertTrue(optionalJavaxProviderKeyMatch);
assertTrue(optionalProviderKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalJavaxProviderKeyMatch);
assertEquals(HAS_JAVA_OPTIONAL, javaOptionalProviderKeyMatch);
assertEquals(expectedDefault != null, defaultMatch);
assertEquals(expectedActual != null, actualMatch);
assertEquals(otherContains.toString(), 0, otherContains.size());
assertEquals("other OptionalBindings found: " + otherOptionalElements, expectedOtherOptionalBindings, otherOptionalElements.size());
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(nonContainedElements));
}
use of com.google.inject.Key in project guice by google.
the class OptionalBinderTest method testKeyHashCodesFixedAtInjectionTime.
/** Ensure key hash codes are fixed at injection time, not binding time. */
public void testKeyHashCodesFixedAtInjectionTime() {
Module m = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
List<String> list = Lists.newArrayList();
b.setDefault().toInstance(list);
b.setBinding().toInstance(list);
list.add("A");
list.add("B");
}
};
Injector injector = Guice.createInjector(m);
for (Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
Key<?> bindingKey = entry.getKey();
Key<?> clonedKey;
if (bindingKey.getAnnotation() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotation());
} else if (bindingKey.getAnnotationType() != null) {
clonedKey = Key.get(bindingKey.getTypeLiteral(), bindingKey.getAnnotationType());
} else {
clonedKey = Key.get(bindingKey.getTypeLiteral());
}
assertEquals(bindingKey, clonedKey);
assertEquals("Incorrect hashcode for " + bindingKey + " -> " + entry.getValue(), bindingKey.hashCode(), clonedKey.hashCode());
}
}
use of com.google.inject.Key in project guice by google.
the class MultibinderTest method testMultibindingProviderDependencies.
public void testMultibindingProviderDependencies() {
final Annotation setAnn = Names.named("foo");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
multibinder.addBinding().toInstance("a");
multibinder.addBinding().toInstance("b");
}
});
HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
});
HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
});
// sanity check the size
assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
Set<Dependency<?>> expected = Sets.newHashSet();
for (Dependency<?> dep : setBinding.getDependencies()) {
Key key = dep.getKey();
Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
expected.add(providerDependency);
}
assertEquals(expected, providerBinding.getDependencies());
}
use of com.google.inject.Key in project camel by apache.
the class Main method getCamelContextMap.
protected Map<String, CamelContext> getCamelContextMap() {
Map<String, CamelContext> answer = Maps.newHashMap();
if (injector != null) {
Set<Map.Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Map.Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Class<?> keyType = Injectors.getKeyType(key);
if (keyType != null && CamelContext.class.isAssignableFrom(keyType)) {
Binding<?> binding = entry.getValue();
Object value = binding.getProvider().get();
if (value != null) {
CamelContext castValue = CamelContext.class.cast(value);
answer.put(key.toString(), castValue);
}
}
}
}
return answer;
}
Aggregations