예제 #1
0
        public override SyntaxNode VisitUsingStatement(UsingStatementSyntax node)
        {
            ClassDeclarationSyntax classDeclaration = node.Ancestors().OfType <ClassDeclarationSyntax>().FirstOrDefault();

            var newObjectCreation = node.Expression.DescendantNodesAndSelf().OfType <ObjectCreationExpressionSyntax>().FirstOrDefault();

            if (newObjectCreation == null || !newObjectCreation.Type.ToString().EndsWith("AopTemplate") || !(node.Statement is BlockSyntax))
            {
                return(node);
            }

            var blockTemplates = new List <AopTemplate>();

            AopTemplate aopTemplate = Utils.GetAopTemplate(newObjectCreation.ArgumentList);

            if (!String.IsNullOrEmpty(BuilderSettings.OnlyTemplate) && BuilderSettings.OnlyTemplate != aopTemplate.TemplateName)
            {
                return(node.Statement);
            }

            blockTemplates.Add(aopTemplate);

            SyntaxNode result = ProcessTemplates(blockTemplates, node.Statement, classDeclaration);

            return(result);
        }
예제 #2
0
        public static List <AopTemplate> GetAopTemplates(SyntaxList <AttributeListSyntax> attributeLists)
        {
            var templates = new List <AopTemplate>();

            foreach (AttributeListSyntax attrList in attributeLists)
            {
                foreach (AttributeSyntax attr in attrList.Attributes)
                {
                    if (BuilderSettings.Verbosity > 1)
                    {
                        Console.Out.WriteLine($"\tFound atribute {attr.Name}");
                    }

                    if (attr.Name.ToFullString() == "AopTemplate")
                    {
                        var aopTemplate = new AopTemplate();
                        templates.Add(aopTemplate);

                        int i = 0;

                        if (attr.ArgumentList != null)
                        {
                            foreach (AttributeArgumentSyntax arg in attr.ArgumentList.Arguments)
                            {
                                if (BuilderSettings.Verbosity > 1)
                                {
                                    Console.Out.WriteLine($"\tArgument {arg.ToFullString()}");
                                }

                                string argName;

                                if (arg.NameEquals != null)
                                {
                                    argName = arg.NameEquals.ToString().TrimEnd(':', '=', ' ').ToLower();
                                }
                                else if (arg.NameColon != null)
                                {
                                    argName = arg.NameColon.Name.ToString().TrimEnd(':', '=', ' ').ToLower();
                                }
                                else
                                {
                                    argName = _aopTemplateConstructor[i].ToLower();
                                }

                                UpdateTemplateFromArgument(argName, aopTemplate, arg.Expression);

                                i++;
                            }
                        }
                    }
                }
            }

            if (!String.IsNullOrEmpty(BuilderSettings.OnlyTemplate))
            {
                return(templates.Where(w => w.TemplateName == BuilderSettings.OnlyTemplate).ToList());
            }

            return(templates);
        }
        public static void UpdateTemplate(this List <AopTemplate> templates, AopTemplate template)
        {
            if (templates == null || template == null)
            {
                return;
            }

            templates.RemoveTemplateByName(template.TemplateName);
            templates.Add(template);
        }
        public static void RemoveTemplateByName(this List <AopTemplate> templates, string templateName)
        {
            if (templates == null)
            {
                return;
            }

            AopTemplate templateToRemove = templates.FirstOrDefault(w => w.TemplateName == templateName);

            if (templateToRemove != null)
            {
                templates.Remove(templateToRemove);
            }
        }
예제 #5
0
        public static AopTemplate GetAopTemplate(ArgumentListSyntax argumentList)
        {
            var aopTemplate = new AopTemplate();

            int i = 0;

            foreach (ArgumentSyntax arg in argumentList.Arguments)
            {
                if (BuilderSettings.Verbosity > 1)
                {
                    Console.Out.WriteLine($"\tArgument {arg.ToFullString()}");
                }

                string argName = (arg.NameColon == null ? _aopTemplateConstructor[i] : arg.NameColon.ToString().TrimEnd(':', '=', ' ')).ToLower();

                UpdateTemplateFromArgument(argName, aopTemplate, arg.Expression);

                i++;
            }

            return(aopTemplate);
        }
예제 #6
0
        private static void UpdateTemplateFromArgument(string argName, AopTemplate aopTemplate, ExpressionSyntax expression)
        {
            string v = expression.ToString().Trim('"');

            switch (argName)
            {
            case "templatename":
                aopTemplate.TemplateName = v;
                break;

            case "advicepriority":
                aopTemplate.AdvicePriority = Int32.Parse(v);
                break;

            case "namefilter":
                aopTemplate.NameFilter = v;
                break;

            case "extratag":
                aopTemplate.ExtraTag = v;
                break;

            case "action":
                v = v.Replace("AopTemplateAction.", "");
                aopTemplate.Action = (AopTemplateAction)Enum.Parse(typeof(AopTemplateAction), v);
                break;

            case "modifier":
                v = v.Replace("AopTemplateModifier.", "");
                aopTemplate.Modifier = (AopTemplateModifier)Enum.Parse(typeof(AopTemplateModifier), v);
                break;

            default:
                Console.Error.WriteLine($"AopTemplate unsupported attribute {argName}");
                break;
            }
        }
예제 #7
0
        public SyntaxNode[] VisitMethodDeclaration(MethodDeclarationSyntax node, AopTemplate template)
        {
            var result = new List <SyntaxNode>();

            ClassDeclarationSyntax classDeclaration = node.Ancestors().OfType <ClassDeclarationSyntax>().FirstOrDefault();

            string methodName = node.Identifier.ToString();

            Console.Out.WriteLine("Method:  " + methodName);

            if (BuilderSettings.Verbosity > 2)
            {
                Console.Out.WriteLine(node.ToFullString());
            }

            IEnumerable <SyntaxNode> resultNodes = ProcessTemplate(_templateService, template, node, classDeclaration);

            if (resultNodes != null)
            {
                result.AddRange(resultNodes);
            }

            return(result.Count > 0 ? result.ToArray() : null);
        }
예제 #8
0
        public static IEnumerable <SyntaxNode> ProcessTemplate(AopCsharpTemplateService templateService, AopTemplate template, SyntaxNode node, ClassDeclarationSyntax classDeclaration)
        {
            string startingWhitespace = "";

            if (node.HasLeadingTrivia)
            {
                startingWhitespace = node.GetLeadingTrivia().ToFullString();
            }

            string closingWhitespace = "";

            if (node.HasTrailingTrivia)
            {
                closingWhitespace = node.GetTrailingTrivia().ToFullString();
            }

            Console.Out.WriteLine($"\tProcessing template {template.TemplateName}");

            string sourceCode = templateService.ProcessTemplate(template.TemplateName, new Dictionary <string, object>()
            {
                { "ClassNode", classDeclaration },
                { "MethodNode", node is MethodDeclarationSyntax ? node : null },
                { "PropertyNode", node is PropertyDeclarationSyntax ? node : null },
                { "SyntaxNode", node },
                { "AppliedTo", template.AppliedTo.ToString() },
                { "StatementNode", null },
                { "ExtraTag", template.ExtraTag }
            });

            // if sourceCode is null, it means no changes were done to original code and we keep it as-is
            if (sourceCode == null)
            {
                return new List <SyntaxNode> {
                           node
                }
            }
            ;

            SyntaxNode compUnit = SyntaxFactory.ParseCompilationUnit(startingWhitespace + sourceCode.Trim(' ', '\r', '\n') + closingWhitespace);

            return(compUnit.ChildNodes());
        }