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");
}
}
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);
}
}
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);
}
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");
}
Aggregations