use of com.google.inject.spi.ProviderLookup 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 = keyType.ofType(RealOptionalBinder.javaOptionalOf(keyType.getTypeLiteral()));
Visitor visitor = new Visitor();
Key<?> defaultKey = null;
Key<?> actualKey = null;
Binding optionalBinding = indexed.get(optionalKey);
OptionalBinderBinding<Optional<T>> optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
Binding javaOptionalBinding = indexed.get(javaOptionalKey);
OptionalBinderBinding<?> 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);
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 = keyType.ofType(RealOptionalBinder.javaOptionalOfJavaxProvider(keyType.getTypeLiteral()));
Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(RealOptionalBinder.optionalOfProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalProviderKey = keyType.ofType(RealOptionalBinder.javaOptionalOfProvider(keyType.getTypeLiteral()));
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);
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 (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);
assertTrue(javaOptionalKeyMatch);
assertTrue(javaOptionalJavaxProviderKeyMatch);
assertTrue(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.spi.ProviderLookup in project roboguice by roboguice.
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(OptionalBinder.optionalOf(keyType.getTypeLiteral()));
Key<?> javaOptionalKey = HAS_JAVA_OPTIONAL ? keyType.ofType(OptionalBinder.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, Source.DEFAULT)) {
defaultKey = binding.getKey();
} else if (isSourceEntry(binding, 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(optionalOfJavaxProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalJavaxProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(javaOptionalOfJavaxProvider(keyType.getTypeLiteral())) : null;
Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(optionalOfProvider(keyType.getTypeLiteral()));
Key<?> javaOptionalProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(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.spi.ProviderLookup in project guice by google.
the class SpiUtils method mapModuleTest.
@SuppressWarnings("unchecked")
private static <T> void mapModuleTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType, Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings, MapResult<?, ?>... results) {
Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
Visitor<T> visitor = new Visitor<>();
MapBinderBinding<T> mapbinder = null;
Map<Key<?>, Binding<?>> keyMap = Maps.newHashMap();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
keyMap.put(binding.getKey(), binding);
if (binding.getKey().equals(mapKey)) {
mapbinder = (MapBinderBinding<T>) ((Binding<T>) binding).acceptTargetVisitor(visitor);
}
}
}
assertNotNull(mapbinder);
List<MapResult<?, ?>> mapResults = Lists.newArrayList(results);
// Make sure the entries returned from getEntries(elements) are correct.
// Because getEntries() can return duplicates, make sure to continue searching, even
// after we find one match.
List<Map.Entry<?, Binding<?>>> entries = Lists.newArrayList(mapbinder.getEntries(elements));
for (MapResult<?, ?> result : mapResults) {
List<Map.Entry<?, Binding<?>>> foundEntries = Lists.newArrayList();
for (Map.Entry<?, Binding<?>> entry : entries) {
Object key = entry.getKey();
Binding<?> value = entry.getValue();
if (key.equals(result.k) && matches(value, result.v)) {
assertTrue("mapBinder doesn't contain: " + entry.getValue(), mapbinder.containsElement(entry.getValue()));
foundEntries.add(entry);
}
}
assertTrue("Could not find entry: " + result + " in remaining entries: " + entries, !foundEntries.isEmpty());
entries.removeAll(foundEntries);
}
assertTrue("Found all entries of: " + mapResults + ", but more were left over: " + entries, entries.isEmpty());
assertEquals(mapKey, mapbinder.getMapKey());
assertEquals(keyType, mapbinder.getKeyTypeLiteral());
assertEquals(valueType, mapbinder.getValueTypeLiteral());
Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
Key<?> mapOfJavaxProvider = mapKey.ofType(mapOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfJavaxProvider = mapKey.ofType(mapOfSetOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfProvider = mapKey.ofType(mapOfCollectionOfProviderOf(keyType, valueType));
Key<?> mapOfCollectionOfJavaxProvider = mapKey.ofType(mapOfCollectionOfJavaxProviderOf(keyType, valueType));
Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
Key<?> setOfJavaxEntry = mapKey.ofType(setOf(entryOfJavaxProviderOf(keyType, valueType)));
Key<?> collectionOfProvidersOfEntryOfProvider = mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
Key<?> collectionOfJavaxProvidersOfEntryOfProvider = mapKey.ofType(collectionOfJavaxProvidersOf(entryOfProviderOf(keyType, valueType)));
Key<?> setOfExtendsOfEntryOfProvider = mapKey.ofType(setOfExtendsOf(entryOfProviderOf(keyType, valueType)));
Key<?> mapOfKeyExtendsValueKey = mapKey.ofType(mapOf(keyType, TypeLiteral.get(Types.subtypeOf(valueType.getType()))));
assertEquals(ImmutableSet.of(mapOfProvider, mapOfJavaxProvider, mapOfSetOfProvider, mapOfSetOfJavaxProvider, mapOfCollectionOfProvider, mapOfCollectionOfJavaxProvider, mapOfSet, mapOfKeyExtendsValueKey), mapbinder.getAlternateMapKeys());
boolean entrySetMatch = false;
boolean entrySetJavaxMatch = false;
boolean mapProviderMatch = false;
boolean mapJavaxProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = false;
boolean mapSetJavaxProviderMatch = false;
boolean mapCollectionProviderMatch = false;
boolean mapCollectionJavaxProviderMatch = false;
boolean collectionOfProvidersOfEntryOfProviderMatch = false;
boolean collectionOfJavaxProvidersOfEntryOfProviderMatch = false;
boolean setOfExtendsOfEntryOfProviderMatch = false;
boolean mapOfKeyExtendsValueKeyMatch = false;
List<Object> otherMapBindings = Lists.newArrayList();
List<Element> otherMatches = Lists.newArrayList();
List<Element> otherElements = Lists.newArrayList();
Indexer indexer = new Indexer(null);
Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
int duplicates = 0;
for (Element element : elements) {
boolean contains = mapbinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key<T> key = null;
Binding<T> b = null;
if (element instanceof Binding) {
b = (Binding) element;
if (b instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> pb = (ProviderInstanceBinding<?>) b;
if (pb.getUserSuppliedProvider() instanceof ProviderMapEntry) {
// weird casting required to workaround jdk6 compilation problems
ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pb.getUserSuppliedProvider();
Binding<?> valueBinding = keyMap.get(pme.getValueKey());
if (indexer.isIndexable(valueBinding) && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
duplicates++;
}
}
}
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof MapBinderBinding) {
matched = true;
if (visited.equals(mapbinder)) {
assertTrue(contains);
} else {
otherMapBindings.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (!matched && key != null) {
if (key.equals(mapOfProvider)) {
matched = true;
assertTrue(contains);
mapProviderMatch = true;
} else if (key.equals(mapOfJavaxProvider)) {
matched = true;
assertTrue(contains);
mapJavaxProviderMatch = true;
} else if (key.equals(mapOfSet)) {
matched = true;
assertTrue(contains);
mapSetMatch = true;
} else if (key.equals(mapOfSetOfProvider)) {
matched = true;
assertTrue(contains);
mapSetProviderMatch = true;
} else if (key.equals(mapOfSetOfJavaxProvider)) {
matched = true;
assertTrue(contains);
mapSetJavaxProviderMatch = true;
} else if (key.equals(mapOfCollectionOfProvider)) {
matched = true;
assertTrue(contains);
mapCollectionProviderMatch = true;
} else if (key.equals(mapOfCollectionOfJavaxProvider)) {
matched = true;
assertTrue(contains);
mapCollectionJavaxProviderMatch = true;
} else if (key.equals(setOfEntry)) {
matched = true;
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
if (b != null) {
assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
}
} else if (key.equals(setOfJavaxEntry)) {
matched = true;
assertTrue(contains);
entrySetJavaxMatch = true;
} else if (key.equals(collectionOfProvidersOfEntryOfProvider)) {
matched = true;
assertTrue(contains);
collectionOfProvidersOfEntryOfProviderMatch = true;
} else if (key.equals(collectionOfJavaxProvidersOfEntryOfProvider)) {
matched = true;
assertTrue(contains);
collectionOfJavaxProvidersOfEntryOfProviderMatch = true;
} else if (key.equals(setOfExtendsOfEntryOfProvider)) {
matched = true;
assertTrue(contains);
setOfExtendsOfEntryOfProviderMatch = true;
} else if (key.equals(mapOfKeyExtendsValueKey)) {
matched = true;
assertTrue(contains);
mapOfKeyExtendsValueKeyMatch = true;
}
}
if (!matched && contains) {
otherMatches.add(element);
}
}
int otherMatchesSize = otherMatches.size();
if (allowDuplicates) {
// allow for 1 duplicate binding
otherMatchesSize--;
}
// Multiply by 2 because each has a value, and Map.Entry
int expectedSize = (mapResults.size() + duplicates) * 2;
assertEquals("incorrect number of contains, leftover matches:\n" + Joiner.on("\n\t").join(otherMatches), expectedSize, otherMatchesSize);
assertTrue(entrySetMatch);
assertTrue(entrySetJavaxMatch);
assertTrue(mapProviderMatch);
assertTrue(mapJavaxProviderMatch);
assertTrue(collectionOfProvidersOfEntryOfProviderMatch);
assertTrue(collectionOfJavaxProvidersOfEntryOfProviderMatch);
assertTrue(setOfExtendsOfEntryOfProviderMatch);
assertTrue(mapOfKeyExtendsValueKeyMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals(allowDuplicates, mapSetJavaxProviderMatch);
assertEquals(allowDuplicates, mapCollectionProviderMatch);
assertEquals(allowDuplicates, mapCollectionJavaxProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
use of com.google.inject.spi.ProviderLookup in project roboguice by roboguice.
the class SpiUtils method mapModuleTest.
@SuppressWarnings("unchecked")
private static <T> void mapModuleTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType, Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings, MapResult... results) {
Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
Visitor<T> visitor = new Visitor<T>();
MapBinderBinding<T> mapbinder = null;
Map<Key<?>, Binding<?>> keyMap = Maps.newHashMap();
for (Element element : elements) {
if (element instanceof Binding) {
Binding<?> binding = (Binding<?>) element;
keyMap.put(binding.getKey(), binding);
if (binding.getKey().equals(mapKey)) {
mapbinder = (MapBinderBinding<T>) ((Binding<T>) binding).acceptTargetVisitor(visitor);
}
}
}
assertNotNull(mapbinder);
assertEquals(keyType, mapbinder.getKeyTypeLiteral());
assertEquals(valueType, mapbinder.getValueTypeLiteral());
List<MapResult> mapResults = Lists.newArrayList(results);
Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
Key<?> collectionOfProvidersOfEntry = mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
boolean entrySetMatch = false;
boolean entryProviderCollectionMatch = false;
boolean mapProviderMatch = false;
boolean mapSetMatch = false;
boolean mapSetProviderMatch = false;
List<Object> otherMapBindings = Lists.newArrayList();
List<Element> otherMatches = Lists.newArrayList();
List<Element> otherElements = Lists.newArrayList();
Indexer indexer = new Indexer(null);
Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
int duplicates = 0;
for (Element element : elements) {
boolean contains = mapbinder.containsElement(element);
if (!contains) {
otherElements.add(element);
}
boolean matched = false;
Key key = null;
Binding b = null;
if (element instanceof Binding) {
b = (Binding) element;
if (b instanceof ProviderInstanceBinding) {
ProviderInstanceBinding<?> pb = (ProviderInstanceBinding<?>) b;
if (pb.getUserSuppliedProvider() instanceof ProviderMapEntry) {
// weird casting required to workaround jdk6 compilation problems
ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pb.getUserSuppliedProvider();
Binding<?> valueBinding = keyMap.get(pme.getValueKey());
if (indexer.isIndexable(valueBinding) && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
duplicates++;
}
}
}
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof MapBinderBinding) {
matched = true;
if (visited.equals(mapbinder)) {
assertTrue(contains);
} else {
otherMapBindings.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (!matched && key != null) {
if (key.equals(mapOfProvider)) {
matched = true;
assertTrue(contains);
mapProviderMatch = true;
} else if (key.equals(mapOfSet)) {
matched = true;
assertTrue(contains);
mapSetMatch = true;
} else if (key.equals(mapOfSetOfProvider)) {
matched = true;
assertTrue(contains);
mapSetProviderMatch = true;
} else if (key.equals(setOfEntry)) {
matched = true;
assertTrue(contains);
entrySetMatch = true;
// Validate that this binding is also a MultibinderBinding.
if (b != null) {
assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
}
} else if (key.equals(collectionOfProvidersOfEntry)) {
matched = true;
assertTrue(contains);
entryProviderCollectionMatch = true;
}
}
if (!matched && contains) {
otherMatches.add(element);
}
}
int otherMatchesSize = otherMatches.size();
if (allowDuplicates) {
// allow for 1 duplicate binding
otherMatchesSize--;
}
// value, ProviderLookup per value, Map.Entry per value
otherMatchesSize = otherMatchesSize / 3;
assertEquals("incorrect number of contains, leftover matches: " + otherMatches, mapResults.size() + duplicates, otherMatchesSize);
assertTrue(entrySetMatch);
assertTrue(entryProviderCollectionMatch);
assertTrue(mapProviderMatch);
assertEquals(allowDuplicates, mapSetMatch);
assertEquals(allowDuplicates, mapSetProviderMatch);
assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());
// Validate that we can construct an injector out of the remaining bindings.
Guice.createInjector(Elements.getModule(otherElements));
}
Aggregations