Exemplo n.º 1
0
        private SourceCode GetFields(SemanticModel semanticModel, HashSet <string> typeReferences,
                                     IEnumerable <FieldDeclarationSyntax> fieldsDeclarations)
        {
            var generatedFields = new SourceCode {
                MainPart = ""
            };

            foreach (var fieldDeclaration in fieldsDeclarations)
            {
                var variables = fieldDeclaration.Declaration.Variables;
                if (variables.Count != 1)
                {
                    throw new NotImplementedException();
                }
                var    variable      = variables.First();
                string identifier    = variable.Identifier.ValueText;
                var    fieldType     = fieldDeclaration.Declaration.Type;
                var    typeReference = TypeReferenceGenerator.GenerateTypeReference(fieldType, semanticModel);
                if (!typeReference.IsPredefined)
                {
                    typeReferences.Add(typeReference.Text);
                }

                bool   isStatic = HasStaticModifier(fieldDeclaration.Modifiers.ToList());
                string optionalStaticModifierText = isStatic ? "static " : "";

                var    fieldAccessModifier     = GetAccessModifier(fieldDeclaration.Modifiers.ToList());
                string generatedAccessModifier = fieldAccessModifier == AccessModifier.Public ? "public" : "private";

                string initializerText = "";
                if (variable.Initializer != null)
                {
                    string initializerValueText = ExpressionGenerator
                                                  .GenerateExpression(variable.Initializer.Value, semanticModel);
                    initializerText += " = " + initializerValueText;
                }

                string generatedField =
                    generatedAccessModifier + " " + optionalStaticModifierText + typeReference.Text + " "
                    + identifier + initializerText + ";\n";
                generatedFields.MainPart += "\n" + generatedField;
            }
            return(generatedFields);
        }
Exemplo n.º 2
0
        private SourceCode GetProperties(SemanticModel semanticModel, HashSet <string> typeReferences,
                                         IEnumerable <PropertyDeclarationSyntax> propertyDeclarations, bool isFromInterface)
        {
            var generatedProperties = new SourceCode {
                MainPart = ""
            };

            foreach (var propertyDeclaration in propertyDeclarations)
            {
                string identifier    = propertyDeclaration.Identifier.ValueText;
                var    propertyType  = propertyDeclaration.Type;
                var    typeReference = TypeReferenceGenerator.GenerateTypeReference(propertyType, semanticModel);
                if (!typeReference.IsPredefined)
                {
                    typeReferences.Add(typeReference.Text);
                }

                bool isStatic  = HasStaticModifier(propertyDeclaration.Modifiers.ToList());
                bool isVirtual = isFromInterface || HasVirtualModifier(propertyDeclaration.Modifiers.ToList());

                var propertyAccessModifier = GetAccessModifier(propertyDeclaration.Modifiers.ToList());
                var accessors   = propertyDeclaration.AccessorList.Accessors;
                var getAccessor = accessors.FirstOrDefault(syntax => syntax.Keyword.Text == "get");
                var setAccessor = accessors.FirstOrDefault(syntax => syntax.Keyword.Text == "set");

                if (propertyDeclaration.AccessorList.Accessors.Any(syntax => syntax.Body != null))
                {
                    var complexPropertyDescription = new ComplexPropertyDescription
                    {
                        PropertyName      = identifier,
                        PropertyType      = typeReference,
                        GetAccessModifier = new Optional <AccessModifier>(),
                        SetAccessModifier = new Optional <AccessModifier>(),
                        IsStatic          = isStatic,
                        IsVirtual         = isVirtual
                    };
                    if (getAccessor != null)
                    {
                        var getAccessModifier = GetAccessModifier(getAccessor.Modifiers);
                        var getModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, getAccessModifier);
                        complexPropertyDescription.GetAccessModifier = getModifier;
                        if (getAccessor.Body != null)
                        {
                            var statements = new List <string>();
                            foreach (var statement in getAccessor.Body.Statements)
                            {
                                string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                                statements.Add(generatedStatement);
                            }
                            complexPropertyDescription.GetStatements = statements;
                        }
                    }
                    if (setAccessor != null)
                    {
                        var setAccessModifier = GetAccessModifier(setAccessor.Modifiers);
                        var setModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, setAccessModifier);
                        complexPropertyDescription.SetAccessModifier = setModifier;
                        if (setAccessor.Body != null)
                        {
                            var statements = new List <string>();
                            foreach (var statement in setAccessor.Body.Statements)
                            {
                                string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                                statements.Add(generatedStatement);
                            }
                            complexPropertyDescription.SetStatements = statements;
                        }
                    }

                    SourceCode generatedProperty = PropertyGenerator.GenerateComplexProperty(complexPropertyDescription, semanticModel);
                    generatedProperties.MainPart += "\n" + generatedProperty.MainPart;
                }
                else
                {
                    var simplePropertyDescription = new SimplePropertyDescription
                    {
                        PropertyName      = identifier,
                        PropertyType      = typeReference,
                        GetAccessModifier = new Optional <AccessModifier>(),
                        SetAccessModifier = new Optional <AccessModifier>(),
                        IsStatic          = isStatic,
                        IsVirtual         = isVirtual,
                        IsFromInterface   = isFromInterface
                    };
                    if (getAccessor != null)
                    {
                        var getAccessModifier = GetAccessModifier(getAccessor.Modifiers);
                        var getModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, getAccessModifier);
                        simplePropertyDescription.GetAccessModifier = getModifier;
                    }
                    if (setAccessor != null)
                    {
                        var setAccessModifier = GetAccessModifier(setAccessor.Modifiers);
                        var setModifier       = MaxRestrictionAccessModifier(propertyAccessModifier, setAccessModifier);
                        simplePropertyDescription.SetAccessModifier = setModifier;
                    }

                    SourceCode generatedProperty = PropertyGenerator
                                                   .GenerateSimpleProperty(simplePropertyDescription, semanticModel, isFromInterface);
                    generatedProperties.MainPart += "\n" + generatedProperty.MainPart;
                }
            }
            return(generatedProperties);
        }
Exemplo n.º 3
0
        private SourceCode GetMethods(SemanticModel semanticModel, HashSet <string> typeReferences, IEnumerable <MethodDeclarationSyntax> methodDeclarations)
        {
            var generatedMethods = new SourceCode {
                MainPart = ""
            };

            foreach (var methodDeclaration in methodDeclarations)
            {
                string identifier          = methodDeclaration.Identifier.ValueText;
                var    returnType          = methodDeclaration.ReturnType;
                var    returnTypeReference = TypeReferenceGenerator.GenerateTypeReference(returnType, semanticModel);
                if (!returnTypeReference.IsPredefined)
                {
                    typeReferences.Add(returnTypeReference.Text);
                }
                var typeParameters = new List <string>();
                if (methodDeclaration.TypeParameterList != null)
                {
                    foreach (var typeParameter in methodDeclaration.TypeParameterList.Parameters)
                    {
                        typeParameters.Add(typeParameter.Identifier.ValueText);
                    }
                }
                var parameters = new List <Var>();
                foreach (var parameter in methodDeclaration.ParameterList.Parameters)
                {
                    var typeReference = TypeReferenceGenerator.GenerateTypeReference(parameter.Type, semanticModel);
                    parameters.Add(new Var
                    {
                        Name = parameter.Identifier.ValueText,
                        Type = typeReference
                    });
                }
                var  accessModifier = GetAccessModifier(methodDeclaration.Modifiers.ToList());
                bool isStatic       = HasStaticModifier(methodDeclaration.Modifiers.ToList());
                Optional <List <string> > body;
                if (methodDeclaration.Body == null)
                {
                    body = new Optional <List <string> >();
                }
                else
                {
                    var statements = new List <string>();
                    foreach (var statement in methodDeclaration.Body.Statements)
                    {
                        string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                        statements.Add(generatedStatement);
                        new Optional <List <string> >();
                    }
                    body = new Optional <List <string> >(statements);
                }

                bool isVirtual = !body.HasValue || HasVirtualModifier(methodDeclaration.Modifiers.ToList());

                var generatedMethod = MethodGenerator.Generate(identifier,
                                                               returnTypeReference,
                                                               typeParameters,
                                                               accessModifier,
                                                               parameters,
                                                               body,
                                                               isStatic,
                                                               isVirtual,
                                                               semanticModel);
                generatedMethods.MainPart += "\n" + generatedMethod.MainPart;
            }
            return(generatedMethods);
        }