protected virtual void FormatMethod(StringBuilder sb, MethodExtraction methExtract) { sb.AppendLine($"\t\tpublic {methExtract.Signature}"); sb.AppendLine("\t\t{"); sb.AppendLine("\t\t}"); }
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("}"); }
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); }
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()); }
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); }
private void ExtractFromMethod(MethodInfo methodInfo, TypeExtraction typExtracted) { MethodExtraction mextraction = new MethodExtraction() { Name = methodInfo.Name, ReturnValueType = methodInfo.ReturnType.Name, Signature = methodInfo.ToString(), Arguments = new List <MethodArgument>() { } }; var parameters = methodInfo.GetParameters(); foreach (var parameterInfo in parameters) { MethodArgument arg = new MethodArgument() { Name = parameterInfo.Name, Type = parameterInfo.ParameterType.ToString() }; foreach (var parameterInfoCustomAttribute in parameterInfo.CustomAttributes) { if (parameterInfoCustomAttribute.AttributeType == typeof(ParamArrayAttribute)) { arg.IsParams = true; break; } } mextraction.Arguments.Add(arg); } typExtracted.Methods.Add(mextraction); }