internal void Initialize(ClassDeclarationSyntax cds)
        {
            Name = cds.Identifier.ToString();
            foreach (var mds in cds.DescendantNodes().OfType <MethodDeclarationSyntax>())
            {
                SyntaxToken publicToken = mds.ChildTokens().ToList().Find(token => token.ValueText == "public");
                if (publicToken == default(SyntaxToken))
                {
                    continue;
                }

                BaseMethodInfo miToAdd = new BaseMethodInfo();
                miToAdd.Initialize(mds);
                Methods.Add(miToAdd);
            }

            foreach (var ctor in cds.DescendantNodes().OfType <ConstructorDeclarationSyntax>())
            {
                SyntaxToken publicToken = cds.ChildTokens().ToList().Find(token => token.ValueText == "public");
                if (publicToken == default(SyntaxToken))
                {
                    continue;
                }

                BaseMethodInfo miToAdd = new BaseMethodInfo();
                miToAdd.Initialize(ctor);
                Constructors.Add(miToAdd);
            }
        }
        public override void VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            var isPartial  = node.ChildTokens().Any(x => x.Kind() == SyntaxKind.PartialKeyword);
            var defineName = node.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString();

            // ジェネリック型を定義している場合
            if (node.ChildNodes().OfType <TypeParameterListSyntax>().Any())
            {
                var listNode     = node.ChildNodes().OfType <TypeParameterListSyntax>().FirstOrDefault();
                var genericTypes = listNode
                                   .ChildNodes()
                                   .OfType <TypeParameterSyntax>()
                                   .Select(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).ToString());

                defineName = $"{defineName}<{string.Join(", ", genericTypes)}>";
            }

            var startLength     = node.Span.Start;
            var endLength       = node.Span.End;
            var parentNamespace = GetNamespace(DefineKinds.Class, startLength, endLength);

            var baseTypeInfos = new List <BaseTypeInfo>();

            // 継承元クラス、またはインターフェースがある場合
            if (node.ChildNodes().OfType <BaseListSyntax>().Any())
            {
                var listNode  = node.ChildNodes().OfType <BaseListSyntax>().FirstOrDefault();
                var baseTypes = listNode.ChildNodes().OfType <SimpleBaseTypeSyntax>();

                baseTypeInfos = GetBaseTypeInfos(baseTypes, parentNamespace);
            }

            UserDefinitions.Add(new UserDefinition
            {
                DefineKinds    = DefineKinds.Class,
                IsPartial      = isPartial,
                Namespace      = parentNamespace,
                DefineName     = defineName,
                DefineFullName = $"{parentNamespace}.{defineName}",
                BaseTypeInfos  = baseTypeInfos,
                SourceFile     = SourceFile,
                StartLength    = startLength,
                EndLength      = endLength,
            });

            base.VisitClassDeclaration(node);
        }
        private async Task <Document> AddExceptionWordToTheName(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken c)
        {
            var root = await document.GetSyntaxRootAsync(c);

            var idToken = classDeclaration.ChildTokens().Single(x => x.Kind() == SyntaxKind.IdentifierToken);
            var newName = idToken.Text + "Exception";

            return(GetDocumentWithNewName(document, classDeclaration, root, idToken, newName));
        }
示例#4
0
        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            var type = node.ChildNodes().OfType <BaseListSyntax>().FirstOrDefault();

            var symbol = _model.GetDeclaredSymbol(node);

            if (type == null)
            {
                node = node.AddBaseListTypes(SyntaxFactory.SimpleBaseType
                                             (

                                                 SyntaxFactory.QualifiedName
                                                 (
                                                     SyntaxFactory.IdentifierName("System"),
                                                     SyntaxFactory.IdentifierName("Object").WithTrailingTrivia(SyntaxFactory.EndOfLine(Environment.NewLine))
                                                 )
                                             ));
                var eol = node.ChildTokens().OfType <SyntaxToken>().ElementAt(1);
                var ws  = eol.ReplaceTrivia(eol.TrailingTrivia.Last(), SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "));
                node = node.ReplaceToken(eol, ws);
                var colon   = node.BaseList.ChildTokens().OfType <SyntaxToken>().FirstOrDefault();
                var colonws = colon.WithTrailingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "));
                node = node.ReplaceToken(colon, colonws);
            }
            else if (symbol.BaseType.Name == "Object" && (symbol.BaseType.ToString() != "Object" && symbol.BaseType.ToString() != "System.Object"))
            {
                var blist = node.BaseList.Types.Insert(0, SyntaxFactory.SimpleBaseType(
                                                           SyntaxFactory.QualifiedName
                                                           (
                                                               SyntaxFactory.IdentifierName("System"),
                                                               SyntaxFactory.IdentifierName("Object")
                                                           )).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")));
                node = node.ReplaceNode(node.BaseList, SyntaxFactory.BaseList(blist));
                SyntaxToken comma = node.BaseList.ChildTokens().OfType <SyntaxToken>().FirstOrDefault();
                foreach (SyntaxToken token in node.BaseList.ChildTokens().OfType <SyntaxToken>())
                {
                    if (token.ValueText == ",")
                    {
                        comma = token;
                    }
                }
                var ncomma = comma.WithTrailingTrivia(SyntaxFactory.Whitespace(" "));
                node = node.ReplaceToken(comma, ncomma);
                var colon   = node.BaseList.ChildTokens().OfType <SyntaxToken>().FirstOrDefault();
                var colonws = colon.WithTrailingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "));
                node = node.ReplaceToken(colon, colonws);
            }

            return(base.VisitClassDeclaration(node));
        }
        private async Task <Document> ReplaceLastWordWithException(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken c)
        {
            var root = await document.GetSyntaxRootAsync(c);

            var   idToken = classDeclaration.ChildTokens().Single(x => x.Kind() == SyntaxKind.IdentifierToken);
            var   oldName = idToken.Text;
            Regex regex   = new Regex("([A-Z][a-z]*)");
            var   matches = regex.Matches(oldName);

            var endingToRemove = matches.Cast <Match>().Last().Value;
            var newName        = idToken.Text.Replace(endingToRemove, string.Empty) + "Exception";

            return(GetDocumentWithNewName(document, classDeclaration, root, idToken, newName));
        }
示例#6
0
        private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
        {
            if (context.SemanticModel.IsSpeculativeSemanticModel)
            {
                return;
            }

            var compilationUnit                     = (CSharpCompilation)context.SemanticModel.Compilation;
            INamedTypeSymbol javaObjectType         = compilationUnit.GetTypeByMetadataName("Java.Lang.Object");
            INamedTypeSymbol intPtrType             = compilationUnit.GetTypeByMetadataName("System.IntPtr");
            INamedTypeSymbol jniHandleOwnershipType = compilationUnit.GetTypeByMetadataName("Android.Runtime.JniHandleOwnership");

            List <SyntaxNode> classNodes = ListClass(context.SemanticModel.SyntaxTree.GetRoot()).ToList();

            foreach (var node in classNodes)
            {
                Console.WriteLine(node);

                ClassDeclarationSyntax classDeclarationSyntax = (ClassDeclarationSyntax)node;
                INamedTypeSymbol       declaredSymbol         = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);

                if (!InheritFrom(declaredSymbol, javaObjectType))
                {
                    continue;
                }

                if (!HasJNIConstructor(declaredSymbol.BaseType, intPtrType, jniHandleOwnershipType))
                {
                    continue;
                }

                if (HasJNIConstructor(classDeclarationSyntax, context, intPtrType, jniHandleOwnershipType))
                {
                    continue;
                }

                var tokens     = classDeclarationSyntax.ChildTokens().ToList();
                var childNodex = classDeclarationSyntax.ChildNodes().ToList();

                var locations = declaredSymbol.Locations;

                foreach (var location in locations)
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, location, declaredSymbol.Name));
                }
            }
        }
 /// <summary>
 /// Gets a value indicating whether the class has the partial modifier.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <returns>
 ///   <c>true</c> if the specified class has the partial modifier; otherwise, <c>false</c>.
 /// </returns>
 internal static bool IsPartial(this ClassDeclarationSyntax node)
 => node
 .ChildTokens()
 .Any(x => x.Kind() == SyntaxKind.PartialKeyword);
示例#8
0
        public static DocumentedElement LoadFromSyntaxNode(ClassDeclarationSyntax node)
        {
            SyntaxToken syntaxToken = node.ChildTokens().First(token => token.IsKind(SyntaxKind.IdentifierToken));

            return(DocumentedElement.LoadFromSyntaxTokenAndTrivia(syntaxToken, node.GetLeadingTrivia()));
        }