Esempio n. 1
0
        public static MethodDeclarationSyntax CreateMethod(MethodGenerationData data)
        {
            Debug.Assert(!string.IsNullOrEmpty(data.m_MethodName), "Trying to generate a method with null or empty method name!");
            Debug.Assert(!string.IsNullOrEmpty(data.m_MethodReturnType), "Trying to generate a method with null or empty return type!");
            Debug.Assert(data.m_MethodBodyStatements != null && data.m_MethodBodyStatements.Count > 0, "Trying to generate a method with no body!");

            MethodDeclarationSyntax syntax = SyntaxFactory
                                             .MethodDeclaration(SyntaxFactory.ParseTypeName(data.m_MethodReturnType), data.m_MethodName)
                                             .AddModifiers(CodeGenerationUtility.CreateProtectionLevelToken(data.m_ProtectionLevel));

            switch (data.m_InheritanceKeyword)
            {
            case FunctionInheritanceKeyword.STATIC: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); break;

            case FunctionInheritanceKeyword.OVERRIDE: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.OverrideKeyword)); break;

            case FunctionInheritanceKeyword.VIRTUAL: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.VirtualKeyword)); break;
            }

            if (data.m_IsAsync)
            {
                bool canMakeAsync = false;

                for (int i = 0; i < s_AcceptedAsyncReturnTypes.Length; i++)
                {
                    if (!data.m_MethodReturnType.Contains(s_AcceptedAsyncReturnTypes[i]))
                    {
                        continue;
                    }

                    canMakeAsync = true;
                    break;
                }

                Debug.Assert(canMakeAsync, "Trying to generate async function but the return type is not supported! Please make the return type either void, Task or UniTask");

                if (canMakeAsync)
                {
                    syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.AsyncKeyword));
                }
            }

            syntax = syntax.AddAttributeLists(AttributeGenerationService.CreateAttributeListSyntaxes(data.m_Attributes));

            foreach (var param in data.m_MethodParams)
            {
                syntax = syntax.AddParameterListParameters(CodeGenerationUtility.CreateParameterSyntax(param.m_ParamName, param.m_ParamType));
            }

            foreach (var statement in data.m_MethodBodyStatements)
            {
                syntax = syntax.AddBodyStatements((SyntaxFactory.ParseStatement(statement)));
            }

            return(syntax);
        }
Esempio n. 2
0
        /// <summary> Creates the fields in the generated class. Such as "private int testInt = 0" </summary>
        public static FieldDeclarationSyntax[] CreateFieldDeclarationSyntaxes(List <FieldGenerationData> datas)
        {
            var syntaxes = new List <FieldDeclarationSyntax>();

            for (int i = 0; i < datas.Count; i++)
            {
                // Variable Type: bool, Variable Name: Cancelled. Created variable => bool Cancelled
                VariableDeclarationSyntax variableSyntax = SyntaxFactory
                                                           .VariableDeclaration(SyntaxFactory.ParseTypeName(datas[i].m_VariableType))
                                                           .AddVariables(CreateVariableDeclaratorSyntax(datas[i]));

                // With Protection Level = bool Cancelled => private bool Cancelled
                syntaxes.Add(SyntaxFactory.FieldDeclaration(variableSyntax)
                             .AddModifiers(CodeGenerationUtility.CreateProtectionLevelToken(datas[i].m_ProtectionLevel))
                             .AddModifiers(CreateStaticFieldModifierTokens(datas[i])));
            }

            return(syntaxes.ToArray());
        }
Esempio n. 3
0
        /// <summary> Modify the method found in GetOriginalMethodDeclarationSyntax(). </summary>
        private static MethodDeclarationSyntax GetModifiedMethodDeclarationSyntax(MethodDeclarationSyntax method, MethodModificationData modificationData)
        {
            var nodes = method.DescendantNodes().ToList();

            ParameterListSyntax paramListSyntax = SyntaxFactory.ParameterList();

            foreach (var param in modificationData.m_NewMethodParams)
            {
                paramListSyntax = paramListSyntax.AddParameters(CodeGenerationUtility.CreateParameterSyntax(param.m_ParamName, param.m_ParamType));
            }

            method = modificationData.m_ParamModificationType == MethodParameterModificationType.REPLACE_PARAMS
                ? method.WithParameterList(paramListSyntax)
                : method.AddParameterListParameters(paramListSyntax.Parameters.ToArray());

            BlockSyntax blockSyntax   = SyntaxFactory.Block();
            var         oldStatements = method.Body.Statements.ToList();

            foreach (var statement in modificationData.m_BodyStatements)
            {
                if (modificationData.m_BodyModificationType == MethodBodyModificationType.ADD_OR_REPLACE_BODY)
                {
                    if (oldStatements.Find(x => x.ToFullString().Contains(statement)) != null)
                    {
                        continue;
                    }
                }

                blockSyntax = blockSyntax.AddStatements(SyntaxFactory.ParseStatement(statement));
            }

            // if replacing the body, the statement in the old function with be completely replaced with the new statement
            method = modificationData.m_BodyModificationType == MethodBodyModificationType.REPLACE_BODY
                ? method.WithBody(blockSyntax)
                : method.AddBodyStatements(blockSyntax.Statements.ToArray());

            return(method.NormalizeWhitespace());
        }
Esempio n. 4
0
        private static ClassDeclarationSyntax CreateClassDeclarationSyntax(string className, string[] baseClasses, ClassType classType, List <ConstraintData> constraints)
        {
            ClassDeclarationSyntax syntax = SyntaxFactory.ClassDeclaration(className);

            List <SyntaxToken> tokens = new List <SyntaxToken>();

            tokens.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            switch (classType)
            {
            case ClassType.Standard: break;

            case ClassType.Partial: tokens.Add(SyntaxFactory.Token(SyntaxKind.PartialKeyword)); break;

            case ClassType.Static: tokens.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); break;

            case ClassType.StaticPartial:
                tokens.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
                tokens.Add(SyntaxFactory.Token(SyntaxKind.PartialKeyword));
                break;
            }

            // Add the public modifier: (public class Order)
            syntax = syntax.AddModifiers(tokens.ToArray());

            // Inherit BaseEntity<T> and implement IHaveIdentity: (public class Order : BaseEntity<T>, IHaveIdentity)
            for (int i = 0; i < baseClasses.Length; i++)
            {
                syntax = syntax.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseClasses[i])));
            }

            for (int i = 0; i < constraints.Count; i++)
            {
                syntax = syntax.AddConstraintClauses(CodeGenerationUtility.CreateConstraintClause(constraints[i]));
            }

            return(syntax);
        }