private TypeBindingInformationState NewTypeBindingInformation(
            CompositeValidationResultMutable vResult,
            CollectionsFactory collectionsFactory,
            CompositeModel compositeModel,
            AbstractInjectableModel injModel,
            Type nInfo,
            Type type,
            Type declaringType,
            IDictionary <Type, TypeBindingInformationState> privateComposites
            )
        {
            TypeBindingInformationState state = new TypeBindingInformationState(collectionsFactory);

            state.NativeInfo = nInfo.GetGenericDefinitionIfContainsGenericParameters();

            ListProxy <ListProxy <AbstractGenericTypeBinding> > bindings = collectionsFactory.NewListProxy <ListProxy <AbstractGenericTypeBinding> >();
            Boolean allBindingsOK = this.NewGenericTypeBinding(collectionsFactory, compositeModel, type, declaringType, bindings, privateComposites);

            if (allBindingsOK)
            {
                state.Bindings.AddRange(bindings.CQ);
            }
            else if (vResult != null)
            {
                vResult.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("Failed to deduce generic bindings for " + (injModel == null ? (Object)type : injModel) + ".", compositeModel, injModel as AbstractMemberInfoModel <Object>));
            }
            return(allBindingsOK ? state : null);
        }
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            InvocationInfo invocationInfo = instance.InvocationInfo;
            Object         result;

            if (Object.Equals(typeof(MethodInfo), targetType))
            {
                result = invocationInfo.CompositeMethod;
            }
            else if (Object.Equals(typeof(CompositeMethodModel), targetType))
            {
                result = invocationInfo.FragmentMethodModel.CompositeMethod;
            }
            else if (typeof(AbstractFragmentMethodModel).IsAssignableFrom(targetType))
            {
                result = invocationInfo.FragmentMethodModel;
            }
            else
            {
                AttributeHolder holder = instance.ModelInfo.CompositeMethodAttributeHolders[invocationInfo.FragmentMethodModel.CompositeMethod.MethodIndex];
                if (typeof(Attribute).IsAssignableFrom(targetType))
                {
                    ListQuery <Attribute> list;
                    holder.AllAttributes.TryFindInTypeDictionarySearchBottommostType(targetType, out list);
                    result = list.FirstOrDefault();
                }
                else // target type must be attribute holder
                {
                    result = holder;
                }
            }

            return(result);
        }
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            Object result;

            RoleMapHolder.ROLE_MAPS.Value.Peek().TryGetRole <Object>(((RoleAttribute)model.InjectionScope).Name, targetType, out result);
            return(result);
        }
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            Object retVal;

            instance.Composites.TryGetValue(targetType, out retVal);
            return(retVal);
        }
        internal InjectionValidationErrorImpl(String message, AbstractInjectableModel injectableModel)
        {
            ArgumentValidator.ValidateNotNull("Injectable model", injectableModel);

            this._injectableModel = injectableModel;
            this._message         = message;
        }
Exemple #6
0
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            Object result = null;

            if (targetType.IsAssignableFrom(typeof(CollectionsFactory)))
            {
                result = instance.StructureOwner.Application.CollectionsFactory;
            }
            else if (targetType.IsAssignableFrom(typeof(ApplicationSPI)))
            {
                result = instance.StructureOwner.Application;
            }
            else if (targetType.IsAssignableFrom(typeof(StructureServiceProviderSPI)))
            {
                result = this.GetStructureServiceProvider(instance, model, targetType);
            }
            else if (targetType.IsAssignableFrom(typeof(UsesProviderQuery)))
            {
                result = instance.UsesContainer;
            }
            else if (targetType.IsAssignableFrom(typeof(CompositeModel)) || targetType.IsAssignableFrom(typeof(ServiceCompositeModel)))
            {
                result = instance.ModelInfo.Model;
            }
            else if (targetType.IsAssignableFrom(typeof(CompositeInstance)))
            {
                result = instance;
            }
            else
            {
                result = this.ProvideNonStandardInjection(instance, model, targetType);
            }
            return(result);
        }
        public ValidationResult InjectionPossible(AbstractInjectableModel model)
        {
            // TODO might check for model here already if the type is assignable from Attribute.
            var targetType = model.TargetType;

            return(new ValidationResult(
                       Object.Equals(typeof(MethodInfo), targetType) ||
                       Object.Equals(typeof(AttributeHolder), targetType) ||
                       typeof(Attribute).IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(targetType) ||
                       Object.Equals(typeof(CompositeMethodModel), targetType) ||
                       typeof(AbstractFragmentMethodModel).IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(targetType),
                       "Target type must be either " + typeof(MethodInfo) + ", " + typeof(AttributeHolder) + ", or any sub-type of " + typeof(Attribute) + ", or " + typeof(CompositeMethodModel) + ", or any sub-type of " + typeof(AbstractFragmentMethodModel) + "."));
        }
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            var    scope = model.InjectionScope;
            Object result;

            if (typeof(CompositeState).IsAssignableFrom(targetType))
            {
                result = instance.State;
            }
            else
            {
                var attribute           = (StateAttribute)scope;
                var isCompositeProperty = typeof(CompositeProperty).IsAssignableFrom(targetType);
                var prop = this.FindSuitable <CompositeProperty, PropertyInfo>(
                    instance.State.Properties.Values,
                    attribute.DeclaringType,
                    attribute.ElementName,
                    GetActualTargetType <CompositeProperty>(targetType),
                    cProp => cProp.ReflectionInfo,
                    pInfo => pInfo.PropertyType
                    );
                if (prop != null)
                {
                    result = isCompositeProperty ? prop : prop.PropertyValueAsObject;
                }
                else
                {
                    var isCompositeEvent = typeof(CompositeEvent).IsAssignableFrom(targetType);
                    var evt = this.FindSuitable <CompositeEvent, EventInfo>(
                        instance.State.Events.Values,
                        attribute.DeclaringType,
                        attribute.ElementName,
                        GetActualTargetType <CompositeEvent>(targetType),
                        cEvent => cEvent.ReflectionInfo,
                        eventInfo => eventInfo.EventHandlerType
                        );
                    if (evt != null)
                    {
                        result = isCompositeEvent ? evt : evt.InvokeActionAsObject;
                    }
                    else
                    {
                        result = null;
                    }
                }
            }
            return(result);
        }
Exemple #9
0
        public Object ProvideInjection(SPI.Instance.CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            var scope = model.InjectionScope;
            // First check if we are trying to reference ourselves
            //var gDef = TypeUtils.TypeUtil.GenericDefinitionIfGenericType( targetType );
            Object result            = null;
            var    provider          = instance.StructureOwner.StructureServices;
            var    actualServiceType = GetActualServiceType(targetType);
            var    refs = provider.FindServices(actualServiceType);

            foreach (var tuple in GetQualifierTypes(model))
            {
                var qInstance = (ServiceQualifier)tuple.Item2.QualifierType.LoadConstructorOrThrow(0).Invoke(null);
                refs = refs.Where(sRef => qInstance.Qualifies(sRef, tuple.Item1));
            }

            if (Object.Equals(actualServiceType, targetType))
            {
                result = refs.Select(sRef => sRef.GetServiceWithType(actualServiceType)).FirstOrDefault();
            }
            else
            {
                if (targetType.IsGenericType())
                {
                    var gArgs = targetType.GetGenericArguments();
                    if (Object.Equals(typeof(IEnumerable <>), targetType.GetGenericTypeDefinition()))
                    {
                        var enumerableType = gArgs[0];
                        if (enumerableType.IsGenericType() &&
                            Object.Equals(typeof(ServiceReferenceInfo <>), enumerableType.GetGenericTypeDefinition())
                            )
                        {
                            result = GetTypedEnumerable(refs.Select(sRef => MakeRefToInfo(sRef, enumerableType.GetGenericArguments()[0])), enumerableType);
                        }
                        else
                        {
                            result = GetTypedEnumerable(refs.Select(sRef => sRef.GetServiceWithType(actualServiceType)), enumerableType);
                        }
                    }
                    else
                    {
                        result = MakeRefToInfo(refs.FirstOrDefault(), gArgs[0]);
                    }
                }
            }
            return(result);
        }
        public Object ProvideInjection(SPI.Instance.CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            // Code generation will use Lazy<> by itself if needed, don't do check here.
            Object result;
            InjectionFunctionality func;
            var scope = model.InjectionScope;

            if (scope != null && this._functionalities.TryGetValue(scope.GetType(), out func))
            {
                result = func.ProvideInjection(instance, model, targetType);
            }
            else
            {
                result = null;
            }
            return(result);
        }
Exemple #11
0
        public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
        {
            KeyedRoleMap roleMap = RoleMapHolder.INVOCATION_DATA.Value.RoleMap;
            Object       result;

            if (roleMap.ThisHasRole(targetType))
            {
                result = roleMap.Get <Object>(targetType);
            }
            else
            {
                FunctionInvocationDataAttribute attr = (FunctionInvocationDataAttribute)model.InjectionScope;
                result = attr.GetFactory(targetType).NewInvocationData();
                roleMap.Set(result);
            }
            return(result);
        }
        public InjectionTime GetInjectionTime(AbstractInjectableModel model)
        {
            var targetType = model.TargetType;
            var attribute  = (StateAttribute)model.InjectionScope;

            return(typeof(CompositeProperty).IsAssignableFrom(targetType) ||
                   typeof(CompositeEvent).IsAssignableFrom(targetType) ||
                   !this.FindSuitable <PropertyInfo, PropertyModel>(
                       model.CompositeModel,
                       model,
                       attribute.ElementName,
                       attribute.DeclaringType,
                       method => method.PropertyModel,
                       GetActualTargetType <CompositeProperty>(targetType),
                       pInfo => pInfo.PropertyType
                       ).Any() ?
                   InjectionTime.ON_CREATION :
                   InjectionTime.ON_METHOD_INVOKATION);
        }
        private static Type GetBottommostDeclaringType(AbstractInjectableModel iModel)
        {
            var useWholeHierarchy = iModel is ParameterModel;

            //if (!useFragments)
            //{
            //   var owner = ((ParameterModel)iModel).Owner ;
            //   useFragments = owner is SpecialMethodModel || owner is ConstructorModel;
            //}

            //return (useFragments ?
            //   iModel.CompositeModel.GetAllFragmentTypes() :
            //   iModel.CompositeModel.GetAllCompositeTypes() )
            //   .First( f => f.GetClassHierarchy().Contains( iModel.DeclaringType ) );

            return(iModel.CompositeModel
                   .GetAllFragmentTypes()
                   .First(f =>
                          (useWholeHierarchy ? f.GetAllParentTypes() : f.GetClassHierarchy())
                          .Contains(iModel.DeclaringType)
                          ));
        }
 protected IEnumerable <TModel> FindSuitable <TMemberInfo, TModel>(
     CompositeModel compositeModel,
     AbstractInjectableModel model,
     String name,
     Type declaringType,
     Func <CompositeMethodModel, TModel> methodModelGetter,
     Type targetType,
     Func <TMemberInfo, Type> memberInfoTargetTypeGetter
     )
     where TMemberInfo : MemberInfo
     where TModel : AbstractMemberInfoModel <TMemberInfo>
 {
     //         Type mixinDeclaringType = model is ParameterModel ? ( (ParameterModel) model ).NativeInfo.Member.DeclaringType : ( (FieldModel) model ).NativeInfo.DeclaringType;
     return(compositeModel.Methods
            .Select(method => methodModelGetter(method))
            .Where(targetModel => targetModel != null)
            .Distinct()
            .Where(targetModel =>
                   (name == null || name.Equals(targetModel.NativeInfo.Name)) &&
                   (declaringType == null || declaringType.Equals(targetModel.NativeInfo.DeclaringType.GetGenericDefinitionIfContainsGenericParameters())) &&
                   (targetType == null || ReflectionHelper.AreStructurallySame(targetType, memberInfoTargetTypeGetter(targetModel.NativeInfo), true))
                   ));
 }
 internal ThisTypeInfo(AbstractInjectableModel aModel)
 {
     this.model = aModel;
     this.resolvedTargetType    = aModel.TargetType;
     this.resolvedDeclaringType = aModel.DeclaringType;
 }
 public InjectionTime GetInjectionTime(AbstractInjectableModel model)
 {
     return(InjectionTime.ON_METHOD_INVOKATION);
 }
 public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
 {
     return(_holder == null ? _holder2 : _holder);
 }
 public ValidationResult InjectionPossible(AbstractInjectableModel model)
 {
     // TODO additional validation using FunctionInfo
     return(new ValidationResult(true, null));
 }
Exemple #19
0
 public Object ProvideInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
 {
     return(((CompositeInstanceImpl)instance).CreateOrGetConcernInvocationBase(targetType));
 }
Exemple #20
0
 public ValidationResult InjectionPossible(AbstractInjectableModel model)
 {
     return(new ValidationResult(true, null));
 }
 public object ProvideInjection(Qi4CS.Core.SPI.Instance.CompositeInstance instance, AbstractInjectableModel model, Type targetType)
 {
     return(InjectableObject);
 }
 /// <summary>
 /// Gets the injection time of given <see cref="AbstractInjectableModel"/>.
 /// </summary>
 /// <param name="model">The <see cref="AbstractInjectableModel"/>.</param>
 /// <returns>The injection time for <paramref name="model"/>.</returns>
 /// <exception cref="NullReferenceException">If <paramref name="model"/> is <c>null</c>.</exception>
 /// <seealso cref="InjectionTime"/>
 public static InjectionTime GetInjectionTime(this AbstractInjectableModel model)
 {
     return(model.CompositeModel.ApplicationModel.InjectionService.GetInjectionTime(model));
 }
Exemple #23
0
 protected virtual Object ProvideNonStandardInjection(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
 {
     return(null);
 }
Exemple #24
0
 public InjectionTime GetInjectionTime(AbstractInjectableModel model)
 {
     return(InjectionTime.ON_CREATION);
 }
Exemple #25
0
 protected abstract StructureServiceProviderSPI GetStructureServiceProvider(CompositeInstance instance, AbstractInjectableModel model, Type targetType);
Exemple #26
0
        protected virtual void ValidateParameterType(CompositeValidationResult result, CompositeModel compositeModel, AbstractMemberInfoModel <MemberInfo> memberModel, AbstractInjectableModel injectableModel, Type type)
        {
            if (this.IsCompositeTypeAffectingInjection(injectableModel.InjectionScope))
            {
                Stack <Type> stk = new Stack <Type>();
                stk.Push(type);
                while (stk.Any())
                {
                    Type current = stk.Pop();
                    if (current.IsGenericParameter && current
#if WINDOWS_PHONE_APP
                        .GetTypeInfo()
#endif
                        .DeclaringMethod != null)
                    {
                        result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("Injection scopes affecting composite type (like " + typeof(ThisAttribute) + ", " + typeof(ConcernForAttribute) + ", or " + typeof(SideEffectForAttribute) + " are not allowed on method argument, which is referencing a generic method parameter.", compositeModel, memberModel));
                    }
                    else if (!current.IsGenericParameter && current.ContainsGenericParameters())
                    {
                        foreach (Type gArg in current.GetGenericArguments())
                        {
                            stk.Push(gArg);
                        }
                    }
                }
            }
        }
    /// <summary>
    /// Checks whether given <see cref="AbstractInjectableModel"/> is non-<c>null</c> and related to a given fragment type.
    /// </summary>
    /// <param name="model">The <see cref="AbstractInjectableModel"/>.</param>
    /// <param name="fragmentType">The fragment type.</param>
    /// <returns><c>true</c> if <paramref name="model"/> is non-<c>null</c> and <see cref="AbstractInjectableModel.DeclaringType"/> or generic definition of it if it is generic type matches <paramref name="fragmentType"/>; <c>false</c> otherwise.</returns>
    public static Boolean IsRelatedToFragment(this AbstractInjectableModel model, Type fragmentType)
    {
        var typeToCompare = model == null ? null : model.DeclaringType;

        return(typeToCompare != null && typeToCompare.GetGenericDefinitionIfGenericType().IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(fragmentType));
    }
Exemple #28
0
        protected virtual void ValidateInjectableModel(CompositeValidationResult result, CompositeModel compositeModel, AbstractInjectableModel model)
        {
            Int32 amount = model.GetAttributesMarkedWith(typeof(InjectionScopeAttribute)).Count;

            if (amount > 1)
            {
                result.InjectionValidationErrors.Add(ValidationErrorFactory.NewInjectionError("Only one injection permitted for field or parameter.", model));
            }
            else
            {
                Attribute attr = model.InjectionScope;
                if (attr != null)
                {
                    InjectionService injectionService = compositeModel.ApplicationModel.InjectionService;
                    if (injectionService.HasFunctionalityFor(attr))
                    {
                        if (!model.IsOptional)
                        {
                            var validationResult = injectionService.InjectionPossible(model);
                            if (validationResult == null || !validationResult.InjectionPossible)
                            {
                                result.InjectionValidationErrors.Add(ValidationErrorFactory.NewInjectionError("Injection was not possible" + (validationResult == null ? "." : (": " + validationResult.AdditionalMessage)), model));
                            }
                        }
                    }
                    else if (!model.IsOptional)
                    {
                        result.InjectionValidationErrors.Add(ValidationErrorFactory.NewInjectionError("Could not find injection functionality for attribute " + attr + ".", model));
                    }
                }
                else if (model is FieldModel)
                {
                    result.InternalValidationErrors.Add(ValidationErrorFactory.NewInternalError("Injection attribute was null", model));
                }
            }
        }
 /// <summary>
 /// Checks whether given <see cref="AbstractInjectableModel"/> is non-<c>null</c> and the injectable value will be dependant on fragment instance.
 /// </summary>
 /// <param name="model">The <see cref="AbstractInjectableModel"/>.</param>
 /// <returns><c>true</c> if <paramref name="model"/> is non-<c>null</c> and the injection scope attribute has <see cref="FragmentDependentInjectionAttribute"/> applied to it; <c>false</c> otherwise.</returns>
 public static Boolean IsFragmentDependant(this AbstractInjectableModel model)
 {
     return(model != null && model.InjectionScope != null && model.GetAttributesOfAttribute(model.InjectionScope.GetType()).ContainsKey(typeof(FragmentDependentInjectionAttribute)));
 }
 protected override SPI.Instance.StructureServiceProviderSPI GetStructureServiceProvider(CompositeInstance instance, AbstractInjectableModel model, Type targetType)
 {
     return(((SingletonApplication)instance.StructureOwner).StructureServices);
 }