return matching.get(matching.size() - 1);
}
}
public Injector getQualifiedInjector(final MetaClass type, final QualifyingMetadata metadata) {
final MetaClass erased = type.getErased();
final List<Injector> injectors = this.injectors.get(erased);
final List<Injector> matching = new ArrayList<Injector>();
boolean alternativeBeans = false;
if (injectors != null) {
for (final Injector inj : injectors) {
if (inj.matches(type.getParameterizedType(), metadata)) {
if (!inj.isEnabled()) {
if (inj.isSoftDisabled()) {
inj.setEnabled(true);
}
else {
continue;
}
}
matching.add(inj);
if (inj.isAlternative()) {
alternativeBeans = true;
}
}
}
}
if (matching.size() > 1 && type.isConcrete()) {
// perform second pass
final Iterator<Injector> secondIter = matching.iterator();
while (secondIter.hasNext()) {
if (!secondIter.next().getInjectedType().equals(erased))
secondIter.remove();
}
}
if (matching.isEmpty()) {
throw new InjectionFailure(erased);
}
else if (matching.size() > 1) {
if (alternativeBeans) {
final Iterator<Injector> matchIter = matching.iterator();
while (matchIter.hasNext()) {
if (!enabledAlternatives.contains(matchIter.next().getInjectedType().getFullyQualifiedName())) {
matchIter.remove();
}
}
}
if (IOCGenerator.isTestMode) {
final List<Injector> matchingMocks = new ArrayList<Injector>();
for (final Injector inj : matching) {
if (inj.isTestMock()) {
matchingMocks.add(inj);
}
}
if (!matchingMocks.isEmpty()) {
matching.clear();
matching.addAll(matchingMocks);
}
}
if (matching.isEmpty()) {
throw new InjectionFailure(erased);
}
if (matching.size() == 1) {
return matching.get(0);
}
final StringBuilder buf = new StringBuilder();
for (final Injector inj : matching) {
buf.append(" matching> ").append(inj.toString()).append("\n");
}
buf.append(" Note: configure an alternative to take precedence or remove all but one matching bean.");
throw new InjectionFailure("ambiguous injection type (multiple injectors resolved): "
+ erased.getFullyQualifiedName() + " " + (metadata == null ? "" : metadata.toString()) + ":\n" +
buf.toString());
}
else {
return matching.get(0);
}