Search in sources :

Example 1 with Qualifier

use of org.qi4j.api.service.qualifier.Qualifier in project qi4j-sdk by Qi4j.

the class ServiceInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    // TODO This could be changed to allow multiple @Qualifier annotations
    Annotation qualifierAnnotation = first(filter(Specifications.translate(Annotations.type(), hasAnnotation(Qualifier.class)), iterable(dependencyModel.annotations())));
    Specification<ServiceReference<?>> serviceQualifier = null;
    if (qualifierAnnotation != null) {
        Qualifier qualifier = qualifierAnnotation.annotationType().getAnnotation(Qualifier.class);
        try {
            serviceQualifier = qualifier.value().newInstance().qualifier(qualifierAnnotation);
        } catch (Exception e) {
            throw new InvalidInjectionException("Could not instantiate qualifier serviceQualifier", e);
        }
    }
    if (dependencyModel.rawInjectionType().equals(Iterable.class)) {
        Type iterableType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
        if (Classes.RAW_CLASS.map(iterableType).equals(ServiceReference.class)) {
            // @Service Iterable<ServiceReference<MyService<Foo>> serviceRefs
            Type serviceType = ((ParameterizedType) iterableType).getActualTypeArguments()[0];
            return new IterableServiceReferenceProvider(serviceType, serviceQualifier);
        } else {
            // @Service Iterable<MyService<Foo>> services
            return new IterableServiceProvider(iterableType, serviceQualifier);
        }
    } else if (dependencyModel.rawInjectionType().equals(ServiceReference.class)) {
        // @Service ServiceReference<MyService<Foo>> serviceRef
        Type referencedType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
        return new ServiceReferenceProvider(referencedType, serviceQualifier);
    } else {
        // @Service MyService<Foo> service
        return new ServiceProvider(dependencyModel.injectionType(), serviceQualifier);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) Qualifier(org.qi4j.api.service.qualifier.Qualifier) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) Annotation(java.lang.annotation.Annotation) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) ServiceReference(org.qi4j.api.service.ServiceReference)

Aggregations

Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)1 ServiceReference (org.qi4j.api.service.ServiceReference)1 Qualifier (org.qi4j.api.service.qualifier.Qualifier)1 Annotations.hasAnnotation (org.qi4j.api.util.Annotations.hasAnnotation)1 InvalidInjectionException (org.qi4j.bootstrap.InvalidInjectionException)1