Search in sources :

Example 1 with InvalidInjectionException

use of org.qi4j.bootstrap.InvalidInjectionException in project qi4j-sdk by Qi4j.

the class ThisInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution bindingContext, DependencyModel dependencyModel) throws InvalidInjectionException {
    if (bindingContext.model() instanceof CompositeDescriptor) {
        // If Composite type then return real type, otherwise use the specified one
        final Class<?> thisType = dependencyModel.rawInjectionType();
        Iterable<Class<?>> injectionTypes = null;
        if (Classes.assignableTypeSpecification(thisType).satisfiedBy(bindingContext.model())) {
            injectionTypes = bindingContext.model().types();
        } else {
            CompositeDescriptor acd = ((CompositeDescriptor) bindingContext.model());
            for (Class<?> mixinType : acd.mixinTypes()) {
                if (thisType.isAssignableFrom(mixinType)) {
                    Iterable<? extends Class<?>> iterable = iterable(thisType);
                    injectionTypes = (Iterable<Class<?>>) iterable;
                    break;
                }
            }
            if (injectionTypes == null) {
                throw new InvalidInjectionException("Composite " + bindingContext.model() + " does not implement @This type " + thisType.getName() + " in fragment " + dependencyModel.injectedClass().getName());
            }
        }
        return new ThisInjectionProvider(injectionTypes);
    } else {
        throw new InvalidInjectionException("Object " + dependencyModel.injectedClass() + " may not use @This");
    }
}
Also used : InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor)

Example 2 with InvalidInjectionException

use of org.qi4j.bootstrap.InvalidInjectionException 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)

Example 3 with InvalidInjectionException

use of org.qi4j.bootstrap.InvalidInjectionException in project qi4j-sdk by Qi4j.

the class InjectionProviderFactoryStrategy method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    Class<? extends Annotation> injectionAnnotationType = dependencyModel.injectionAnnotation().annotationType();
    InjectionProviderFactory factory1 = generalProviderFactories.get(injectionAnnotationType);
    InjectionProviderFactory factory2 = valuesProviderFactories.get(injectionAnnotationType);
    if (factory1 == null && factory2 == null) {
        InjectionProviderFactory factory = metaInfo.get(InjectionProviderFactory.class);
        if (factory != null) {
            return factory.newInjectionProvider(resolution, dependencyModel);
        } else {
            throw new InvalidInjectionException("Unknown injection annotation @" + injectionAnnotationType.getSimpleName());
        }
    }
    ModelDescriptor composite = resolution.model();
    Class<?> compositeType = first(composite.types());
    if (factory1 != null && ValueComposite.class.isAssignableFrom(compositeType)) {
        throw new InvalidValueCompositeException("@" + injectionAnnotationType.getSimpleName() + " is not allowed in ValueComposites: " + compositeType);
    }
    InjectionProviderFactory factory;
    if (factory1 == null) {
        factory = factory2;
    } else {
        factory = factory1;
    }
    return factory.newInjectionProvider(resolution, dependencyModel);
}
Also used : InvalidValueCompositeException(org.qi4j.api.composite.InvalidValueCompositeException) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) ModelDescriptor(org.qi4j.api.composite.ModelDescriptor) ValueComposite(org.qi4j.api.value.ValueComposite) InjectionProviderFactory(org.qi4j.runtime.injection.InjectionProviderFactory)

Example 4 with InvalidInjectionException

use of org.qi4j.bootstrap.InvalidInjectionException in project qi4j-sdk by Qi4j.

the class StateInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    if (StateHolder.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State StateHolder properties;
        return new StateInjectionProvider();
    } else if (UnitOfWork.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        if (!(resolution.model() instanceof EntityDescriptor)) {
            throw new InvalidInjectionException("Only EntityComposites can be injected with '@State UnitOfWork'");
        }
        return new UnitOfWorkInjectionProvider();
    } else if (Property.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Property<String> name;
        StateDescriptor descriptor;
        descriptor = ((StatefulCompositeDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
        // Check if property exists
        if (propertyDescriptor == null) {
            return null;
        }
        return new PropertyInjectionProvider(propertyDescriptor);
    } else if (Association.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Association<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getAssociationByName(name);
        // No such association found
        if (model == null) {
            return null;
        }
        return new AssociationInjectionProvider(model);
    } else if (ManyAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State ManyAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().equals("")) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getManyAssociationByName(name);
        // No such association found
        if (model == null) {
            return null;
        }
        return new ManyAssociationInjectionProvider(model);
    }
    throw new InjectionProviderException("Injected value has invalid type");
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation) AbstractAssociation(org.qi4j.api.association.AbstractAssociation) Association(org.qi4j.api.association.Association) State(org.qi4j.api.injection.scope.State) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) StateDescriptor(org.qi4j.api.composite.StateDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Aggregations

InvalidInjectionException (org.qi4j.bootstrap.InvalidInjectionException)4 Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 AbstractAssociation (org.qi4j.api.association.AbstractAssociation)1 Association (org.qi4j.api.association.Association)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 CompositeDescriptor (org.qi4j.api.composite.CompositeDescriptor)1 InvalidValueCompositeException (org.qi4j.api.composite.InvalidValueCompositeException)1 ModelDescriptor (org.qi4j.api.composite.ModelDescriptor)1 StateDescriptor (org.qi4j.api.composite.StateDescriptor)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 State (org.qi4j.api.injection.scope.State)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 NoSuchServiceException (org.qi4j.api.service.NoSuchServiceException)1 ServiceReference (org.qi4j.api.service.ServiceReference)1 Qualifier (org.qi4j.api.service.qualifier.Qualifier)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1