public void Track(TrackerInput input, params Condition[] conditions)
        {
            input.Context.RegisterCompilationStartAction(c =>
            {
                if (input.IsEnabled(c.Options))
                {
                    c.RegisterSymbolAction(TrackMethodDeclaration, SymbolKind.Method);
                }
            });

            void TrackMethodDeclaration(SymbolAnalysisContext c)
            {
                if (IsTrackedMethod((IMethodSymbol)c.Symbol, c.Compilation))
                {
                    foreach (var declaration in c.Symbol.DeclaringSyntaxReferences)
                    {
                        var methodIdentifier = GetMethodIdentifier(declaration.GetSyntax());
                        if (methodIdentifier.HasValue)
                        {
                            c.ReportDiagnosticWhenActive(Diagnostic.Create(input.Rule, methodIdentifier.Value.GetLocation()));
                        }
                    }
                }
            }

            bool IsTrackedMethod(IMethodSymbol methodSymbol, Compilation compilation)
            {
                var conditionContext = new MethodDeclarationContext(methodSymbol, compilation);

                return(conditions.All(c => c(conditionContext)));
            }
        }
        protected virtual bool VisitMethodDeclaration(MethodDeclarationContext ctx)
        {
            var methodNameNode = ctx.children.OfType <TerminalNodeImpl>()
                                 .Where(x => x.Symbol.Type == Identifier).Single();

            _rewriter.Replace(methodNameNode.Symbol, methodNameNode.GetText().Pascalize());

            return(true);
        }
        private void ParseNode(IParseTree node)
        {
            if (node is PackageDeclarationContext packageContext)
            {
                string name = this.GetNodeName(packageContext);

                this.WriteKeyValue("Package", name);
            }
            else if (node is ImportDeclarationContext importContext)
            {
                string name = this.GetNodeName(importContext);

                this.WriteKeyValue("Import", name);
            }
            else if (node is TypeDeclarationContext typeContext)
            {
                foreach (var child in typeContext.children)
                {
                    if (child is InterfaceDeclarationContext)
                    {
                        InterfaceDeclarationContext interfaceContext = child as InterfaceDeclarationContext;

                        string name = interfaceContext.IDENTIFIER().GetText();

                        this.WriteLine();
                        this.WriteKeyValue("Interface", name);
                        this.WriteBeginBrace();

                        var members = interfaceContext.interfaceBody().interfaceBodyDeclaration().Select(item => item.interfaceMemberDeclaration());

                        foreach (InterfaceMemberDeclarationContext member in members)
                        {
                            var constContext = member.constDeclaration();
                            if (constContext != null)
                            {
                                string constName = constContext.constantDeclarator(0).IDENTIFIER().GetText();

                                this.WriteLine($"Const field:{constName}");
                            }

                            InterfaceMethodDeclarationContext method = member.interfaceMethodDeclaration();
                            if (method != null)
                            {
                                string methodName = method.IDENTIFIER().GetText();

                                this.WriteKeyValue("Method", methodName);
                            }
                        }
                        this.WriteEndBrace();
                    }
                    else if (child is ClassDeclarationContext)
                    {
                        ClassDeclarationContext classContext = child as ClassDeclarationContext;
                        string name = this.GetNodeName(classContext);

                        this.WriteLine();
                        this.WriteLine($"Class:{name}");
                        this.WriteBeginBrace();

                        var members = classContext.classBody().classBodyDeclaration().Select(item => item.memberDeclaration());

                        foreach (MemberDeclarationContext member in members)
                        {
                            FieldDeclarationContext field = member.fieldDeclaration();
                            if (field != null)
                            {
                                string fieldName = field.variableDeclarators().GetText();

                                this.WriteKeyValue("Field", fieldName);
                            }

                            MethodDeclarationContext method = member.methodDeclaration();
                            if (method != null)
                            {
                                string methodName = method.IDENTIFIER().GetText();

                                this.WriteKeyValue("Method", methodName);
                            }
                        }

                        this.WriteEndBrace();
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Add Methode Declaration to Class
        /// </summary>
        /// <param name="inClass"></param>
        /// <param name="tmpDeclaration"></param>
        /// <param name="tmpModifierList"></param>
        /// <returns></returns>
        private static MethodeContainer AddMethodeDeclaration(ClassContainer inClass, MethodDeclarationContext inMethodeContext, List <string> tmpModifierList)
        {
            var tmpMethode = new MethodeContainer
            {
                Name       = inMethodeContext.IDENTIFIER().GetText(),
                AntlrCode  = inMethodeContext.methodBody(),
                ReturnType = CreateTypeContainerFromType(inMethodeContext.typeTypeOrVoid()),
            };
            var tmpParams = inMethodeContext.formalParameters()?.formalParameterList()?.formalParameter();

            if (tmpParams != null)
            {
                foreach (var tmpParam in tmpParams)
                {
                    var tmpNewMethode = new FieldContainer
                    {
                        Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(),
                    }; if (tmpParam.typeType().classOrInterfaceType() != null)
                    {
                        tmpNewMethode.Type = tmpParam.typeType().classOrInterfaceType().IDENTIFIER(0).GetText();
                        foreach (var tmpGeneric in tmpParam.typeType().classOrInterfaceType().typeArguments().SelectMany(inItem => inItem.typeArgument()))
                        {
                            var tmpType = new TypeContainer(tmpGeneric.GetChild(0).GetText());
                            if (tmpGeneric.EXTENDS() != null)
                            {
                                tmpType.Extends.Add(tmpGeneric.typeType().GetText());
                            }
                            tmpNewMethode.Type.GenericTypes.Add(tmpType);
                        }

                        if (tmpParam.GetText().Contains("["))
                        {
                            tmpNewMethode.Type.IsArray = true;
                        }
                        if (tmpParam.typeType().classOrInterfaceType().IDENTIFIER().Length > 1)
                        {
                            throw new NotImplementedException("Multi-Identifier needs to be handled");
                        }
                    }
                    else if (tmpParam.typeType().primitiveType() != null)
                    {
                        tmpNewMethode.Type = tmpParam.typeType().primitiveType().GetText();
                    }
                    else
                    {
                        throw new NotImplementedException("Other Type Missing");
                    }
                    if (tmpParam.variableModifier().Length > 0)
                    {
                        foreach (var tmpModifierContext in tmpParam.variableModifier())
                        {
                            var tmpParamModifier = tmpModifierContext.GetText();
                            //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D
                            if (tmpParamModifier.StartsWith("@"))
                            {
                                tmpParamModifier = tmpParamModifier.Substring(1).ToLower();
                            }
                            tmpNewMethode.ModifierList.Add(tmpParamModifier);
                        }
                    }
                    tmpMethode.Parameter.Add(tmpNewMethode);
                }
            }
            tmpMethode.ModifierList = tmpModifierList;
            inClass.AddMethode(tmpMethode);
            return(tmpMethode);
        }
Beispiel #5
0
 public static MethodDeclaration ToAst(this MethodDeclarationContext methodDeclarationContext)
 {
     return(new MethodDeclaration(methodDeclarationContext.type().ToAst(), methodDeclarationContext.Identifier().GetText(), methodDeclarationContext.methodParameters().ToAst(), methodDeclarationContext.Exception() == null ? false : true, methodDeclarationContext.methodBody().ToAst()));
 }