public void Generate(CSharpGeneratorContext context,
                             IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                             CSharpElementFactory factory)
        {
            IMethodDeclaration methodDeclaration = null;

            ITypeElement declaredElement = context.ClassDeclaration.DeclaredElement;

            if (declaredElement != null)
            {
                IOverridableMember member = declaredElement.Methods.FirstOrDefault(ValidateMethod);
                if (member != null)
                {
                    methodDeclaration = (IMethodDeclaration)member.GetDeclarations().FirstOrDefault();
                    Generate(context, methodDeclaration, elements, factory);
                    return;
                }
            }
            string method = String.Format("public Int32 {0}()", MethodName);

            methodDeclaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(method);
            Generate(context, methodDeclaration, elements, factory);
            context.PutMemberDeclaration(methodDeclaration, null,
                                         newDeclaration => new GeneratorDeclarationElement(newDeclaration));
        }
        public static void Initialize(CSharpGeneratorContext context)
        {
            IDeclaredType byteType = TypeFactory.CreateTypeByCLRName(PredefinedType.BYTE_FQN, context.PsiModule, context.Anchor.GetResolveContext());

            _byteArrayType  = TypeFactory.CreateArrayType(byteType, 1);
            _conversionRule = new CSharpTypeConversionRule(context.PsiModule);
        }
        public void Generate(CSharpGeneratorContext context,
                             IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                             CSharpElementFactory factory)
        {
            IMethodDeclaration methodDeclaration = null;

            ITypeElement declaredElement = context.ClassDeclaration.DeclaredElement;

            if (declaredElement != null)
            {
                IDeclaredType byteType = TypeFactory.CreateTypeByCLRName(PredefinedType.BYTE_FQN, context.PsiModule,
                                                                         context.Anchor.GetResolveContext());
                _byteArrayType      = TypeFactory.CreateArrayType(byteType, 1);
                _typeConversionRule = new CSharpTypeConversionRule(context.PsiModule);
                IOverridableMember member = declaredElement.Methods.FirstOrDefault(ValidateMethod);
                if (member != null)
                {
                    methodDeclaration = (IMethodDeclaration)member.GetDeclarations().FirstOrDefault();
                    Generate(context, methodDeclaration, elements, factory);
                    return;
                }
            }
            string method = String.Format("public Int32 {0}(Byte[] bytes, Int32 startIndex)", MethodName);

            methodDeclaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(method);
            Generate(context, methodDeclaration, elements, factory);
            context.PutMemberDeclaration(methodDeclaration, null,
                                         newDeclaration => new GeneratorDeclarationElement(newDeclaration));
        }
 public TypeHandlingContext(CSharpGeneratorContext generatorContext)
 {
     GeneratorContext = generatorContext;
     ElementFactory   = CSharpElementFactory.GetInstance(generatorContext.Root.GetPsiModule());
     Builder          = new StringBuilder();
     Args             = new List <Object>();
     Variables        = new VariablesTracker();
 }
        private void Generate(CSharpGeneratorContext context,
                              IMethodDeclaration declaration,
                              IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                              CSharpElementFactory factory)
        {
            var builder = new StringBuilder();

            builder.Append("{").AppendFormat("{0}(bytes, 0);", ReadMethodGenerator.Instance.MethodName).Append("}");

            IBlock body = factory.CreateBlock(builder.ToString());

            declaration.SetBody(body);
        }
        /// <exception cref="System.ArgumentNullException">The <paramref name="context"/> is <c>null</c>.</exception>
        protected override void Process(CSharpGeneratorContext context)
        {
            Argument.IsNotNull(() => context);

            var factory = CSharpElementFactory.GetInstance(context.Root.GetPsiModule());
            var typeOwners = context.InputElements.OfType<GeneratorDeclaredElement<ITypeOwner>>().ToList();

            var includeInSerialization = bool.Parse(context.GetGlobalOptionValue(OptionIds.IncludePropertyInSerialization));
            var notificationMethod = bool.Parse(context.GetGlobalOptionValue(OptionIds.ImplementPropertyChangedNotificationMethod));
            var forwardEventArgument = bool.Parse(context.GetGlobalOptionValue(OptionIds.ForwardEventArgumentToImplementedPropertyChangedNotificationMethod));

            var propertyConverter = new PropertyConverter(factory, context.PsiModule, (IClassDeclaration)context.ClassDeclaration);
            foreach (var typeOwner in typeOwners)
            {
                var propertyDeclaredElement = typeOwner.DeclaredElement;
                var propertyDeclaration = (IPropertyDeclaration)propertyDeclaredElement.GetDeclarations().FirstOrDefault();
                if (propertyDeclaration != null)
                {
                    propertyConverter.Convert(propertyDeclaration, includeInSerialization, notificationMethod, forwardEventArgument);
                }
            }
        }
        private static void Generate(CSharpGeneratorContext context,
                                     IMethodDeclaration declaration,
                                     IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                     CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            var size = ctx.GetSizeVariableName();

            ctx.Builder.Append("{");
            ctx.Builder.AppendFormat("var {0} = 0;", size);
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleGetSize(ctx);
                    }
                }
            }
            ctx.Builder.AppendFormat("return {0};", size);
            ctx.Builder.Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }
        protected override void Generate(CSharpGeneratorContext context,
                                         IMethodDeclaration declaration,
                                         IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                         CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            ctx.Builder.Append("{");
            ctx.Builder.Append("var index = startIndex;");
            ctx.Builder.Append("Byte[] tmp;");
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleRead(ctx);
                    }
                }
            }
            ctx.Builder.Append("return index;");
            ctx.Builder.Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }
 protected abstract void Generate(CSharpGeneratorContext context, IMethodDeclaration declaration,
                                  IList <GeneratorDeclaredElement <ITypeOwner> > elements, CSharpElementFactory factory);
예제 #10
0
 public static void Initialize(CSharpGeneratorContext context)
 {
     _listDeclaredType = TypeFactory.CreateTypeByCLRName(typeof(List <>).FullName, context.PsiModule,
                                                         context.Anchor.GetResolveContext());
     _listTypeElement = _listDeclaredType.GetTypeElement();
 }
        protected override void Process(CSharpGeneratorContext context)
        {
            Argument.IsNotNull(() => context);

            var factory = CSharpElementFactory.GetInstance(context.Root.GetPsiModule());
            var viewModelToModelAttributeClrType = TypeHelper.CreateTypeByCLRName(CatelMVVM.ViewModelToModelAttribute, context.PsiModule, UniversalModuleReferenceContext.Instance);

            var declaredElements = context.InputElements.OfType<GeneratorDeclaredElement>().ToList();
            var classLikeDeclaration = context.ClassDeclaration;
            if (classLikeDeclaration != null)
            {
                var includeInSerialization = bool.Parse(context.GetGlobalOptionValue(OptionIds.IncludePropertyInSerialization));
                var notificationMethod = bool.Parse(context.GetGlobalOptionValue(OptionIds.ImplementPropertyChangedNotificationMethod));
                var forwardEventArgument = bool.Parse(context.GetGlobalOptionValue(OptionIds.ForwardEventArgumentToImplementedPropertyChangedNotificationMethod));
                var propertyConverter = new PropertyConverter(factory, context.PsiModule, (IClassDeclaration)classLikeDeclaration);
                foreach (var declaredElement in declaredElements)
                {
                    var model = (IProperty)declaredElement.GetGroupingObject();
                    var modelProperty = (IProperty)declaredElement.DeclaredElement;
                    if (model != null)
                    {
                        Log.Debug("Computing property name");
                        string propertyName = string.Empty;

                        var cSharpTypeMemberDeclarations = new List<ICSharpTypeMemberDeclaration>();

                        IClassLikeDeclaration currentClassDeclaration = classLikeDeclaration;

                        do
                        {
                            cSharpTypeMemberDeclarations.AddRange(currentClassDeclaration.MemberDeclarations);
                            var superType = currentClassDeclaration.SuperTypes.FirstOrDefault(type => type.IsClassType());
                            if (superType != null)
                            {
                                var superTypeTypeElement = superType.GetTypeElement();
                                if (superTypeTypeElement != null)
                                {
                                    currentClassDeclaration = (IClassLikeDeclaration)superTypeTypeElement.GetDeclarations().FirstOrDefault();
                                }
                            }
                        }
                        while (currentClassDeclaration != null);

                        if (!cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == modelProperty.ShortName))
                        {
                            propertyName = modelProperty.ShortName;
                        }

                        if (string.IsNullOrEmpty(propertyName) && !cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == model.ShortName + modelProperty.ShortName))
                        {
                            propertyName = model.ShortName + modelProperty.ShortName;
                        }

                        int idx = 0;
                        while (string.IsNullOrEmpty(propertyName))
                        {
                            if (!cSharpTypeMemberDeclarations.Exists(declaration => declaration.DeclaredName == model.ShortName + modelProperty.ShortName + idx.ToString(CultureInfo.InvariantCulture)))
                            {
                                propertyName = model.ShortName + modelProperty.ShortName + idx.ToString(CultureInfo.InvariantCulture);
                            }

                            idx++;
                        }

                        Log.Debug("Adding property '{0}'", propertyName);
                        var propertyDeclaration = (IPropertyDeclaration)factory.CreateTypeMemberDeclaration(ImplementationPatterns.AutoProperty, modelProperty.Type, propertyName);

                        var modelMemberDeclaration = model.GetDeclarations().FirstOrDefault();
                        if (modelMemberDeclaration != null && modelMemberDeclaration.Parent != null)
                        {
                            var modelClassDeclaration = modelMemberDeclaration.Parent.Parent;
                            if (modelClassDeclaration == classLikeDeclaration)
                            {
                                propertyDeclaration = ModificationUtil.AddChildAfter(modelClassDeclaration, modelMemberDeclaration, propertyDeclaration);
                            }
                            else if (classLikeDeclaration.Body != null && classLikeDeclaration.Body.FirstChild != null)
                            {

                                propertyDeclaration = ModificationUtil.AddChildAfter(classLikeDeclaration.Body.FirstChild, propertyDeclaration);
                            }
                        }

                        var fixedArguments = new List<AttributeValue> { new AttributeValue(ConstantValueHelper.CreateStringValue(model.ShortName, context.PsiModule, UniversalModuleReferenceContext.Instance)) };

                        if (propertyName != modelProperty.ShortName)
                        {
                            fixedArguments.Add(new AttributeValue(ConstantValueHelper.CreateStringValue(modelProperty.ShortName, context.PsiModule, UniversalModuleReferenceContext.Instance)));
                        }

                        Log.Debug("Adding attribute ViewModelToModel to property '{0}'", propertyName);
                        IAttribute attribute = factory.CreateAttribute(viewModelToModelAttributeClrType.GetTypeElement(), fixedArguments.ToArray(), new Pair<string, AttributeValue>[] { });
                        propertyDeclaration.AddAttributeAfter(attribute, null);
                        propertyConverter.Convert(propertyDeclaration, includeInSerialization, notificationMethod, forwardEventArgument);
                    }
                }
            }
        }