package io.micronaut.inject.qualifiers;
import static io.micronaut.core.util.ArgumentUtils.check;
import io.micronaut.context.Qualifier;
import io.micronaut.context.annotation.Primary;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.naming.NameResolver;
import io.micronaut.inject.BeanType;
import javax.inject.Named;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
@Internal
class NameQualifier<T> implements Qualifier<T>, io.micronaut.core.naming.Named {
private final String name;
NameQualifier(String name) {
this.name = Objects.requireNonNull(name, "Argument [name] cannot be null");
}
@Override
public <BT extends BeanType<T>> Stream<BT> reduce(Class<T> beanType, Stream<BT> candidates) {
check("beanType", beanType).notNull();
check("candidates", candidates).notNull();
return candidates.filter(candidate -> {
if (!beanType.isAssignableFrom(candidate.getBeanType())) {
return false;
}
String typeName;
AnnotationMetadata annotationMetadata = candidate.getAnnotationMetadata();
Optional<String> beanQualifier = annotationMetadata
.findDeclaredAnnotation(Named.class)
.flatMap(AnnotationValue::stringValue);
typeName = beanQualifier.orElseGet(() -> {
if (candidate instanceof NameResolver) {
Optional<String> resolvedName = ((NameResolver) candidate).resolveName();
return resolvedName.orElse(candidate.getBeanType().getSimpleName());
}
return candidate.getBeanType().getSimpleName();
});
return typeName.equalsIgnoreCase(name) || typeName.equalsIgnoreCase(name + beanType.getSimpleName());
}
);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !NameQualifier.class.isAssignableFrom(o.getClass())) {
return false;
}
NameQualifier<?> that = (NameQualifier<?>) o;
return name.equals(that.name);
}
@Override
public String toString() {
return "@Named('" + name + "')";
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String getName() {
return name;
}
protected <BT extends BeanType<T>> Stream<BT> reduceByAnnotation(Class<T> beanType, Stream<BT> candidates, String annotationName, String qualifiedName) {
return candidates.filter(candidate -> {
if (candidate.isPrimary() && Primary.class.getSimpleName().equals(annotationName)) {
return true;
}
String candidateName;
if (candidate instanceof NameResolver) {
candidateName = ((NameResolver) candidate).resolveName().orElse(candidate.getBeanType().getSimpleName());
} else {
Optional<String> annotation = candidate.getAnnotationMetadata().stringValue(Named.class);
candidateName = annotation.orElse(candidate.getBeanType().getSimpleName());
}
if (candidateName.equalsIgnoreCase(annotationName)) {
return true;
} else {
String qualified = annotationName + beanType.getSimpleName();
if (qualified.equals(candidateName)) {
return true;
}
}
return qualifiedName != null && candidate.getAnnotationMetadata().hasDeclaredAnnotation(qualifiedName);
}
);
}
protected <BT extends BeanType<T>> Stream<BT> reduceByName(Class<T> beanType, Stream<BT> candidates, String annotationName) {
return candidates.filter(candidate -> {
if (candidate.isPrimary() && Primary.class.getSimpleName().equals(annotationName)) {
return true;
}
String candidateName;
if (candidate instanceof NameResolver) {
candidateName = ((NameResolver) candidate).resolveName().orElse(candidate.getBeanType().getSimpleName());
} else {
Optional<String> annotation = candidate.getAnnotationMetadata().stringValue(Named.class);
candidateName = annotation.orElse(candidate.getBeanType().getSimpleName());
}
if (candidateName.equalsIgnoreCase(annotationName)) {
return true;
} else {
String qualified = annotationName + beanType.getSimpleName();
if (qualified.equals(candidateName)) {
return true;
}
}
return false;
}
);
}
}