Exemple #1
0
        public void Calls()
        {
            string code = @"
            class A
            {
                public A()
                {
                    _b = new B();
                }
                public void f()
                {
                    return _b.g();
                }
                private B _b;
            }
            class B
            {
                public void g() { }
            }
            ";

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

            ITypeExtraction  b  = new ClassExtraction(registry, "B");
            MethodExtraction g1 = b.GetMethods().First();
            MethodExtraction f  = g1.GetCallees().First();
            MethodExtraction g2 = f.GetCalls().First();

            Assert.Equal("g", g1.Name);
            Assert.Equal("f", f.Name);
            Assert.Equal("g", g2.Name);
        }
        static void Main(string[] args)
        {
            var inputFolders           = new string[] { "../extractor" };
            IEnumerable <string> files = inputFolders.SelectMany(folder
                                                                 => Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories));


            StringBuilder builder = new StringBuilder();

            foreach (string filename in files)
            {
                builder.Append(File.ReadAllText(filename));
            }
            string sourceCode = builder.ToString();


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

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

            Console.WriteLine("digraph d {");
            foreach (var mthd in method.GetCalls())
            {
                Console.WriteLine($"{method.Name} -> {mthd.Name}");
            }
            foreach (var mthd in method.GetCallees())
            {
                Console.WriteLine($"{mthd.Name} -> {method.Name}");
            }
            Console.WriteLine("}");
        }
Exemple #3
0
        public void ParameterizedMethodTypes()
        {
            string code = @"
                class A<T> { }
                class C { }
                class B
                {
                    public A<C> f(A<C> a)
                    {
                        var a = new A<C>();
                        return a;
                    }
                }
            ";

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

            var method = new MethodExtraction(registry, "f");
            List <ITypeExtraction> returns   = method.GetReturnTypes().ToList();
            List <ITypeExtraction> arguments = method.GetArgumentTypes().ToList();

            Assert.Equal(2, returns.Count());
            Assert.Equal(1, returns.Where(rtrn => rtrn.Name == "A").Count());
            Assert.Equal(1, returns.Where(rtrn => rtrn.Name == "C").Count());

            Assert.Equal(2, arguments.Count());
            Assert.Equal(1, arguments.Where(arg => arg.Name == "A").Count());
            Assert.Equal(1, arguments.Where(arg => arg.Name == "C").Count());
        }
Exemple #4
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());
        }
Exemple #5
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);
        }
Exemple #6
0
        private static async Task DoWithOptions(
            string contextcenter,
            IEnumerable <string> inputFolders,
            IEnumerable <RelationType> relationTypes)
        {
            IEnumerable <string> files = inputFolders.SelectMany(folder
                                                                 => Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories));

            StringBuilder builder = new StringBuilder();

            foreach (string filename in files)
            {
                builder.Append(File.ReadAllText(filename));
            }
            string sourceCode = builder.ToString();


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

            var relationRegistry = new RelationRegistry(registry);

            List <Relation> overnextNeighbors =
                (await GetOvernextNeighbors(relationRegistry, relationTypes.ToList(), contextcenter)).
                Distinct().
                ToList();

            IEnumerable <Node> nodes = overnextNeighbors
                                       .SelectMany(relation => new Node[] {
                new Node {
                    Name = relation.From.Name, Kind = MapKind(relation.From)
                },
                new Node {
                    Name = relation.To.Name, Kind = MapKind(relation.To)
                }
            }).Distinct();

            Console.WriteLine("digraph d {");

            List <string> dotLines = nodes.Select(NodeToDotLine).ToList();

            dotLines.AddRange(
                overnextNeighbors.
                Select(RelationToDotLine).
                Distinct());

            foreach (string line in dotLines)
            {
                Console.WriteLine(line);
            }

            Console.WriteLine("}");
        }
Exemple #7
0
        public void DoNotCrashWithUnknownTypesOfProperties()
        {
            string code = @"
                class A
                {
                    public string Prop { get; }
                }
            ";

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

            var clss = new ClassExtraction(registry, "A");

            var properties = clss.GetReferenced();

            Assert.Empty(properties);
        }
Exemple #8
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);
        }
Exemple #9
0
 public RelationRegistry(DeclarationRegistry registry) => _registry = registry;