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); }
/// <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()); }