コード例 #1
0
        public void MethodTypes()
        {
            string code = @"
                class A
                {
                    public B f(C c)
                    {
                        return new B();
                    }
                }

                class B { }
                struct C { }
            ";

            SyntaxTree tree     = SyntaxFactory.ParseSyntaxTree(code);
            var        registry = new DeclarationRegistry(tree);

            var method = new MethodExtraction(registry, "f");

            ITypeExtraction b = method.GetReturnTypes().First();
            ITypeExtraction c = method.GetArgumentTypes().First();

            Assert.True(b is ClassExtraction);
            Assert.True(c is StructExtraction);
        }
コード例 #2
0
        public void AssociationsToMembersAndTypeParameters()
        {
            string text = @"
            class C1 { }
            class C2 { }
            class T { }
            class C3<TType> { }
            class A {
                public C1 c1;
                public C2 c2 { get; set; }
                public C3<T> deep;
                public C3<T> deepProp { get; set; }
            }";

            SyntaxTree tree     = SyntaxFactory.ParseSyntaxTree(text);
            var        registry = new DeclarationRegistry(tree);

            ITypeExtraction        a      = new ClassExtraction(registry, "A");
            List <ITypeExtraction> fields = a.GetReferenced().ToList();

            ITypeExtraction c1 = fields.Find(field => field.Name == "C1");
            ITypeExtraction c2 = fields.Find(field => field.Name == "C2");

            Assert.True(c1 is ClassExtraction);
            Assert.True(c2 is ClassExtraction);
            Assert.Equal(2, fields.Where(field => field.Name == "C3").Count());
            Assert.Equal(2, fields.Where(field => field.Name == "T").Count());
        }
コード例 #3
0
 public Relation(ITypeExtraction from, ITypeExtraction to, RelationType kind)
 {
     if (from == null || to == null)
     {
         throw new InvalidProgramException("From or to node of relation cannot be null");
     }
     From = from;
     To   = to;
     Kind = kind;
 }
コード例 #4
0
        private static NodeType MapKind(ITypeExtraction from)
        {
            switch (from)
            {
            case InterfaceExtraction _: return(NodeType.Interface);

            case ClassExtraction _: return(NodeType.Class);

            case StructExtraction _: return(NodeType.Struct);

            default: throw new NotImplementedException("Extraction cannot be mapped to NodeType.");
            }
        }
コード例 #5
0
        public static IEnumerable <ITypeExtraction> Reversed(DeclarationRegistry registry, string name, Func <ITypeExtraction, IEnumerable <ITypeExtraction> > getter)
        {
            IEnumerable <TypeDeclarationSyntax> declarations = registry.Types;

            foreach (TypeDeclarationSyntax declaration in declarations)
            {
                ITypeExtraction extraction = TypeExtraction.CreateTypeExtraction(registry, declaration.Identifier.ValueText);
                foreach (ITypeExtraction extr in getter(extraction))
                {
                    if (extr.Name == name)
                    {
                        yield return(extraction);
                    }
                }
            }
        }
コード例 #6
0
        private IEnumerable <ITypeExtraction> GetSymbolExtractions(ITypeSymbol symbol)
        {
            ITypeExtraction extraction = GetSymbolExtraction(symbol);

            if (extraction != null)
            {
                yield return(extraction);
            }

            if (symbol is INamedTypeSymbol namedSymbol)
            {
                foreach (ITypeSymbol tSymbol in namedSymbol.TypeArguments)
                {
                    ITypeExtraction tExtraction = GetSymbolExtraction(tSymbol);
                    if (tExtraction != null)
                    {
                        yield return(tExtraction);
                    }
                }
            }
        }
コード例 #7
0
        public void InheritanceRelationship()
        {
            string text = @"
            interface I { }
            interface J { }
            class A { }
            class B : A { }
            class C : B, I, J";

            SyntaxTree      tree     = SyntaxFactory.ParseSyntaxTree(text);
            var             registry = new DeclarationRegistry(tree);
            ITypeExtraction c        = new ClassExtraction(registry, "C");

            List <ITypeExtraction> csParents = c.GetParents().ToList();

            Assert.Equal(3, csParents.Count());
            ITypeExtraction b = csParents.Where(parent => parent.Name == "B").First();

            Assert.True(b is ClassExtraction);
            Assert.True(csParents.Where(parent => parent.Name == "I").First() is InterfaceExtraction);
            Assert.True(csParents.Where(parent => parent.Name == "J").First() is InterfaceExtraction);
        }
コード例 #8
0
        public IEnumerable <ITypeExtraction> GetReferenced()
        {
            foreach (PropertyDeclarationSyntax propDecl in _node.DescendantNodes().OfType <PropertyDeclarationSyntax>())
            {
                IPropertySymbol propSymbol = _model.GetDeclaredSymbol(propDecl);
                if (propSymbol != null)
                {
                    ITypeExtraction extraction = TypeExtraction.CreateTypeExtraction(_registry, propSymbol.Type.Name);
                    if (extraction != null)
                    {
                        yield return(extraction);
                    }
                    ;
                }

                if (propDecl.Type is GenericNameSyntax typeSyntax)
                {
                    foreach (var tArg in typeSyntax.TypeArgumentList.Arguments)
                    {
                        TypeInfo        argSymbol  = _model.GetTypeInfo(tArg);
                        ITypeExtraction extraction = TypeExtraction.CreateTypeExtraction(_registry, argSymbol.Type.Name);
                        if (extraction != null)
                        {
                            yield return(extraction);
                        }
                    }
                }
            }

            foreach (FieldDeclarationSyntax fieldDecl in _node.DescendantNodes().OfType <FieldDeclarationSyntax>())
            {
                string     childName = null;
                TypeSyntax fieldType = fieldDecl.Declaration.Type;

                if (fieldType is GenericNameSyntax talSyntax)
                {
                    childName = talSyntax.Identifier.ValueText;
                    // generic type parameters
                    foreach (var tArg in talSyntax.TypeArgumentList.Arguments)
                    {
                        TypeInfo        argSymbol  = _model.GetTypeInfo(tArg);
                        ITypeExtraction extraction = TypeExtraction.CreateTypeExtraction(_registry, argSymbol.Type.Name);
                        if (extraction != null)
                        {
                            yield return(extraction);
                        }
                    }
                }
                else if (fieldType is IdentifierNameSyntax inSyntax)
                {
                    childName = inSyntax.Identifier.ValueText;
                }

                if (childName != null)
                {
                    ITypeExtraction extraction = TypeExtraction.CreateTypeExtraction(_registry, childName);
                    if (extraction != null)
                    {
                        yield return(extraction);
                    }
                }
            }
        }