// helper functions: // ================= // helper to convert the recoursive inheritance tree to a flattened graph private InheritanceGraph Tree2Graph(InheritanceGraph graph, InheritanceNode tree, IHNode groupNode, IHNode baseTypeNode) { ClassDeclarationSyntax classDecl = tree.GetBaseClass(); string nodeName = codeAnalysis.GetClassId(classDecl); // add the type node IHNode typeNode = new IHNode(groupNode.Id + ":" + nodeName, nodeName, classDecl, !codeAnalysis.IsClassAbstract(classDecl)); graph.AddNode(typeNode); // link the type node to the method group graph.AddLink(new IHLink(groupNode.Id, typeNode.Id, true)); // link the type node to it's base type if it has one if (baseTypeNode != null) { graph.AddLink(new IHLink(typeNode.Id, baseTypeNode.Id, false)); } foreach (var subClass in tree.GetSubClasses()) { graph = Tree2Graph(graph, subClass, groupNode, typeNode); } return(graph); }
// creates and returns the inheritance graph public InheritanceGraph CreateInheritanceGraph() { InheritanceGraph graph = new InheritanceGraph(); string nodeId; foreach (NamespaceDeclarationSyntax ns in codeAnalysis.GetAllNamespaces()) { // create a group for each namespace nodeId = codeAnalysis.GetNamespaceId(ns); IHNode nsNode = new IHNode(nodeId, nodeId, Type.NAMESPACE); graph.AddNode(nsNode); foreach (ClassDeclarationSyntax cl in codeAnalysis.GetAllClasses(ns)) { // create a group for each class and link it to the namespace group nodeId = codeAnalysis.GetClassId(cl); IHNode clNode = new IHNode(nodeId, cl.Identifier.Text, Type.CLASS); graph.AddNode(clNode); graph.AddLink(new IHLink(nsNode.Id, clNode.Id, true)); foreach (MethodDeclarationSyntax method in codeAnalysis.GetAllMethods(cl)) { // create a group for each method and link it to the class group nodeId = codeAnalysis.GetMethodId(method); IHNode methodNode = new IHNode(nodeId, method.Identifier.Text, method); graph.AddNode(methodNode); graph.AddLink(new IHLink(clNode.Id, methodNode.Id, true)); // add the inheritance tree to the method group StaticCodeAnalysis.InheritanceTree tree = codeAnalysis.GetInheritanceTree(method); graph = Tree2Graph(graph, tree.rootClass, methodNode, null); } } } return(graph); }