Skip to content

Commit d10a27d

Browse files
authored
Fixed issues (#4)
Fixed issues Fixed method assignment to the wrong class. This happens when the same method name is used in classes with the same name, too. One class is generic, and the other is not. Fixed cycle detection. The "Handles" relationship for event handlers is not a code dependency and must not be included in the cycle detection. Renamed "dependency" to "relationship" when appropriate. Dependency is no longer a good name after introducing "Handles" relationship for event handlers.
1 parent 708ec2e commit d10a27d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+850
-719
lines changed

CSharpCodeAnalyst/App.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override void OnStartup(StartupEventArgs e)
3535
var explorer = new CodeGraphExplorer();
3636
var mainWindow = new MainWindow();
3737

38-
var explorationGraphViewer = new DependencyGraphViewer(messaging);
38+
var explorationGraphViewer = new GraphViewer(messaging);
3939

4040
mainWindow.SetViewer(explorationGraphViewer);
4141
var viewModel = new MainViewModel(messaging, settings);

CSharpCodeAnalyst/Common/AddMissingDependenciesRequest.cs

-3
This file was deleted.

CSharpCodeAnalyst/Exploration/CodeGraphExplorer.cs

+59-58
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace CSharpCodeAnalyst.Exploration;
55

66
public class CodeGraphExplorer : ICodeGraphExplorer
77
{
8-
private List<Dependency> _allDependencies = [];
8+
private List<Relationship> _allRelationships = [];
99
private CodeGraph? _codeGraph;
1010

1111
public void LoadCodeGraph(CodeGraph graph)
1212
{
1313
_codeGraph = graph;
1414

1515
// Clear all cached data
16-
_allDependencies = [];
16+
_allRelationships = [];
1717
}
1818

1919
public List<CodeElement> GetElements(List<string> ids)
@@ -30,7 +30,7 @@ public List<CodeElement> GetElements(List<string> ids)
3030
{
3131
if (_codeGraph.Nodes.TryGetValue(id, out var element))
3232
{
33-
// The element is cloned internally and the dependencies discarded.
33+
// The element is cloned internally and the relationships discarded.
3434
elements.Add(element);
3535
}
3636
}
@@ -93,21 +93,21 @@ public SearchResult FindParents(List<string> ids)
9393
}
9494

9595
/// <summary>
96-
/// Returns all dependencies that link the given nodes (ids).
96+
/// Returns all relationships that link the given nodes (ids).
9797
/// </summary>
98-
public IEnumerable<Dependency> FindAllDependencies(HashSet<string> ids)
98+
public IEnumerable<Relationship> FindAllRelationships(HashSet<string> ids)
9999
{
100100
if (_codeGraph is null)
101101
{
102102
return [];
103103
}
104104

105-
var dependencies = _codeGraph.Nodes.Values
106-
.SelectMany(n => n.Dependencies)
105+
var relationships = _codeGraph.Nodes.Values
106+
.SelectMany(n => n.Relationships)
107107
.Where(d => ids.Contains(d.SourceId) && ids.Contains(d.TargetId))
108108
.ToList();
109109

110-
return dependencies;
110+
return relationships;
111111
}
112112

113113
public Invocation FindIncomingCalls(string id)
@@ -121,7 +121,7 @@ public Invocation FindIncomingCalls(string id)
121121

122122
var method = _codeGraph.Nodes[id];
123123

124-
var allCalls = GetDependencies(d => d.Type == DependencyType.Calls);
124+
var allCalls = GetRelationships(d => d.Type == RelationshipType.Calls);
125125
var calls = allCalls.Where(call => call.TargetId == method.Id).ToArray();
126126
var methods = calls.Select(d => _codeGraph.Nodes[d.SourceId]);
127127

@@ -143,10 +143,10 @@ public Invocation FindIncomingCallsRecursive(string id)
143143
var processingQueue = new Queue<CodeElement>();
144144
processingQueue.Enqueue(method);
145145

146-
var foundCalls = new HashSet<Dependency>();
146+
var foundCalls = new HashSet<Relationship>();
147147
var foundMethods = new HashSet<CodeElement>();
148148

149-
var allCalls = GetDependencies(d => d.Type == DependencyType.Calls);
149+
var allCalls = GetRelationships(d => d.Type == RelationshipType.Calls);
150150

151151
var processed = new HashSet<string>();
152152
while (processingQueue.Any())
@@ -183,18 +183,18 @@ public SearchResult FollowIncomingCallsRecursive(string id)
183183
}
184184

185185
var allImplementsAndOverrides =
186-
GetDependencies(d => d.Type is DependencyType.Implements or DependencyType.Overrides);
187-
var allCalls = GetDependencies(d => d.Type == DependencyType.Calls);
186+
GetRelationships(d => d.Type is RelationshipType.Implements or RelationshipType.Overrides);
187+
var allCalls = GetRelationships(d => d.Type == RelationshipType.Calls);
188188

189-
var allHandles = GetDependencies(d => d.Type == DependencyType.Handles);
190-
var allInvokes = GetDependencies(d => d.Type == DependencyType.Invokes);
189+
var allHandles = GetRelationships(d => d.Type == RelationshipType.Handles);
190+
var allInvokes = GetRelationships(d => d.Type == RelationshipType.Invokes);
191191

192192
var method = _codeGraph.Nodes[id];
193193

194194
var processingQueue = new Queue<CodeElement>();
195195
processingQueue.Enqueue(method);
196196

197-
var foundDependencies = new HashSet<Dependency>();
197+
var foundRelationships = new HashSet<Relationship>();
198198
var foundElements = new HashSet<CodeElement>();
199199

200200

@@ -209,31 +209,31 @@ public SearchResult FollowIncomingCallsRecursive(string id)
209209

210210
// An event is raised by the specialization
211211
var specializations = allImplementsAndOverrides.Where(d => d.TargetId == element.Id).ToArray();
212-
foundDependencies.UnionWith(specializations);
212+
foundRelationships.UnionWith(specializations);
213213
var specializedSources = specializations.Select(d => _codeGraph.Nodes[d.SourceId]).ToHashSet();
214214
foundElements.UnionWith(specializedSources);
215215

216216
// Add all methods that invoke the event
217217
var invokes = allInvokes.Where(call => call.TargetId == element.Id).ToArray();
218-
foundDependencies.UnionWith(invokes);
218+
foundRelationships.UnionWith(invokes);
219219
var invokeSources = invokes.Select(d => _codeGraph.Nodes[d.SourceId]).ToHashSet();
220220
foundElements.UnionWith(invokeSources);
221221

222222
// Add Events that are handled by this method.
223223
var handles = allHandles.Where(h => h.SourceId == element.Id).ToArray();
224-
foundDependencies.UnionWith(handles);
224+
foundRelationships.UnionWith(handles);
225225
var events = handles.Select(h => _codeGraph.Nodes[h.TargetId]).ToHashSet();
226226
foundElements.UnionWith(events);
227227

228228
// Calls
229229
var calls = allCalls.Where(call => call.TargetId == element.Id).ToArray();
230-
foundDependencies.UnionWith(calls);
230+
foundRelationships.UnionWith(calls);
231231
var callSources = calls.Select(d => _codeGraph.Nodes[d.SourceId]).ToHashSet();
232232
foundElements.UnionWith(callSources);
233233

234234
// Abstractions. Sometimes the abstractions is called.
235235
var abstractions = allImplementsAndOverrides.Where(d => d.SourceId == element.Id).ToArray();
236-
foundDependencies.UnionWith(abstractions);
236+
foundRelationships.UnionWith(abstractions);
237237
var abstractionTargets = abstractions.Select(d => _codeGraph.Nodes[d.TargetId]).ToHashSet();
238238
foundElements.UnionWith(abstractionTargets);
239239

@@ -249,7 +249,7 @@ public SearchResult FollowIncomingCallsRecursive(string id)
249249
}
250250
}
251251

252-
return new SearchResult(foundElements, foundDependencies);
252+
return new SearchResult(foundElements, foundRelationships);
253253
}
254254

255255
/// <summary>
@@ -267,7 +267,7 @@ public SearchResult FindFullInheritanceTree(string id)
267267
var type = _codeGraph.Nodes[id];
268268

269269
var types = new HashSet<CodeElement>();
270-
var relationships = new HashSet<Dependency>();
270+
var relationships = new HashSet<Relationship>();
271271
var processingQueue = new Queue<CodeElement>();
272272

273273
var processed = new HashSet<string>();
@@ -287,7 +287,8 @@ public SearchResult FindFullInheritanceTree(string id)
287287

288288
// Case typeToAnalyze is subclass: typeToAnalyze implements X or inherits from Y
289289
var abstractionsOfAnalyzedType =
290-
typeToAnalyze.Dependencies.Where(d => d.Type is DependencyType.Implements or DependencyType.Inherits);
290+
typeToAnalyze.Relationships.Where(d =>
291+
d.Type is RelationshipType.Implements or RelationshipType.Inherits);
291292
foreach (var abstraction in abstractionsOfAnalyzedType)
292293
{
293294
var baseType = _codeGraph.Nodes[abstraction.TargetId];
@@ -339,12 +340,12 @@ public SearchResult FindSpecializations(string id)
339340

340341
var element = _codeGraph.Nodes[id];
341342

342-
var dependencies = _codeGraph.GetAllDependencies()
343-
.Where(d => (d.Type == DependencyType.Overrides ||
344-
d.Type == DependencyType.Implements) &&
343+
var relationships = _codeGraph.GetAllRelationships()
344+
.Where(d => (d.Type == RelationshipType.Overrides ||
345+
d.Type == RelationshipType.Implements) &&
345346
d.TargetId == element.Id).ToList();
346-
var methods = dependencies.Select(m => _codeGraph.Nodes[m.SourceId]).ToList();
347-
return new SearchResult(methods, dependencies);
347+
var methods = relationships.Select(m => _codeGraph.Nodes[m.SourceId]).ToList();
348+
return new SearchResult(methods, relationships);
348349
}
349350

350351
/// <summary>
@@ -361,12 +362,12 @@ public SearchResult FindAbstractions(string id)
361362

362363
var element = _codeGraph.Nodes[id];
363364

364-
var dependencies = element.Dependencies
365-
.Where(d => (d.Type == DependencyType.Overrides ||
366-
d.Type == DependencyType.Implements) &&
365+
var relationships = element.Relationships
366+
.Where(d => (d.Type == RelationshipType.Overrides ||
367+
d.Type == RelationshipType.Implements) &&
367368
d.SourceId == element.Id).ToList();
368-
var methods = dependencies.Select(m => _codeGraph.Nodes[m.TargetId]).ToList();
369-
return new SearchResult(methods, dependencies);
369+
var methods = relationships.Select(m => _codeGraph.Nodes[m.TargetId]).ToList();
370+
return new SearchResult(methods, relationships);
370371
}
371372

372373

@@ -381,13 +382,13 @@ public Invocation FindOutgoingCalls(string id)
381382

382383
var method = _codeGraph.Nodes[id];
383384

384-
var calls = method.Dependencies
385-
.Where(d => d.Type == DependencyType.Calls).ToList();
385+
var calls = method.Relationships
386+
.Where(d => d.Type == RelationshipType.Calls).ToList();
386387
var methods = calls.Select(m => _codeGraph.Nodes[m.TargetId]).ToList();
387388
return new Invocation(methods, calls);
388389
}
389390

390-
public SearchResult FindOutgoingDependencies(string id)
391+
public SearchResult FindOutgoingRelationships(string id)
391392
{
392393
ArgumentNullException.ThrowIfNull(id);
393394

@@ -397,12 +398,12 @@ public SearchResult FindOutgoingDependencies(string id)
397398
}
398399

399400
var element = _codeGraph.Nodes[id];
400-
var dependencies = element.Dependencies;
401-
var targets = dependencies.Select(m => _codeGraph.Nodes[m.TargetId]).ToList();
402-
return new SearchResult(targets, dependencies);
401+
var relationships = element.Relationships;
402+
var targets = relationships.Select(m => _codeGraph.Nodes[m.TargetId]).ToList();
403+
return new SearchResult(targets, relationships);
403404
}
404405

405-
public SearchResult FindIncomingDependencies(string id)
406+
public SearchResult FindIncomingRelationships(string id)
406407
{
407408
ArgumentNullException.ThrowIfNull(id);
408409
if (_codeGraph is null)
@@ -411,43 +412,43 @@ public SearchResult FindIncomingDependencies(string id)
411412
}
412413

413414
var element = _codeGraph.Nodes[id];
414-
var dependencies = _codeGraph.Nodes.Values
415-
.SelectMany(node => node.Dependencies)
415+
var relationships = _codeGraph.Nodes.Values
416+
.SelectMany(node => node.Relationships)
416417
.Where(d => d.TargetId == element.Id).ToList();
417418

418-
var elements = dependencies.Select(d => _codeGraph.Nodes[d.SourceId]);
419+
var elements = relationships.Select(d => _codeGraph.Nodes[d.SourceId]);
419420

420-
return new SearchResult(elements, dependencies);
421+
return new SearchResult(elements, relationships);
421422
}
422423

423-
private List<Dependency> GetCachedDependencies()
424+
private List<Relationship> GetCachedRelationships()
424425
{
425426
if (_codeGraph is null)
426427
{
427428
return [];
428429
}
429430

430-
if (_allDependencies.Count == 0)
431+
if (_allRelationships.Count == 0)
431432
{
432-
_allDependencies = _codeGraph.GetAllDependencies().ToList();
433+
_allRelationships = _codeGraph.GetAllRelationships().ToList();
433434
}
434435

435-
return _allDependencies;
436+
return _allRelationships;
436437
}
437438

438-
private List<Dependency> GetDependencies(Func<Dependency, bool> filter)
439+
private List<Relationship> GetRelationships(Func<Relationship, bool> filter)
439440
{
440-
return GetCachedDependencies().Where(filter).ToList();
441+
return GetCachedRelationships().Where(filter).ToList();
441442
}
442443

443-
private HashSet<Dependency> FindInheritsAndImplementsRelationships()
444+
private HashSet<Relationship> FindInheritsAndImplementsRelationships()
444445
{
445446
if (_codeGraph is null)
446447
{
447448
return [];
448449
}
449450

450-
var inheritsAndImplements = new HashSet<Dependency>();
451+
var inheritsAndImplements = new HashSet<Relationship>();
451452
_codeGraph.DfsHierarchy(Collect);
452453
return inheritsAndImplements;
453454

@@ -458,17 +459,17 @@ void Collect(CodeElement c)
458459
return;
459460
}
460461

461-
foreach (var dependency in c.Dependencies)
462+
foreach (var relationship in c.Relationships)
462463
{
463-
if (dependency.Type is DependencyType.Inherits or DependencyType.Implements)
464+
if (relationship.Type is RelationshipType.Inherits or RelationshipType.Implements)
464465
{
465-
inheritsAndImplements.Add(dependency);
466+
inheritsAndImplements.Add(relationship);
466467
}
467468
}
468469
}
469470
}
470471
}
471472

472-
public record struct SearchResult(IEnumerable<CodeElement> Elements, IEnumerable<Dependency> Dependencies);
473+
public record struct SearchResult(IEnumerable<CodeElement> Elements, IEnumerable<Relationship> Relationships);
473474

474-
public record struct Invocation(IEnumerable<CodeElement> Methods, IEnumerable<Dependency> Calls);
475+
public record struct Invocation(IEnumerable<CodeElement> Methods, IEnumerable<Relationship> Calls);

CSharpCodeAnalyst/Exploration/ICodeGraphExplorer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public interface ICodeGraphExplorer
2020
SearchResult FindFullInheritanceTree(string id);
2121

2222
/// <summary>
23-
/// Finds all dependencies connect the given nodes.
23+
/// Finds all relationships connect the given nodes.
2424
/// </summary>
25-
IEnumerable<Dependency> FindAllDependencies(HashSet<string> ids);
25+
IEnumerable<Relationship> FindAllRelationships(HashSet<string> ids);
2626

2727
/// <summary>
2828
/// Methods that implement or overload the given method
@@ -34,8 +34,8 @@ public interface ICodeGraphExplorer
3434
/// </summary>
3535
SearchResult FindAbstractions(string id);
3636

37-
SearchResult FindOutgoingDependencies(string id);
38-
SearchResult FindIncomingDependencies(string id);
37+
SearchResult FindOutgoingRelationships(string id);
38+
SearchResult FindIncomingRelationships(string id);
3939
void LoadCodeGraph(CodeGraph graph);
4040
List<CodeElement> GetElements(List<string> ids);
4141
SearchResult FindParents(List<string> ids);

CSharpCodeAnalyst/Exports/DsiExport.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public static string Convert(CodeGraph codeGraph)
3636
currentId++;
3737
}
3838

39-
// Add dependencies (relation)
39+
// Add relationships (relation)
4040
foreach (var node in codeGraph.Nodes.Values)
4141
{
42-
foreach (var dependency in node.Dependencies)
42+
foreach (var relationship in node.Relationships)
4343
{
4444
relations.Add(new XElement(ns + "relation",
4545
new XAttribute("from", idMap[node.Id]),
46-
new XAttribute("to", idMap[dependency.TargetId]),
47-
new XAttribute("type", dependency.Type.ToString()),
46+
new XAttribute("to", idMap[relationship.TargetId]),
47+
new XAttribute("type", relationship.Type.ToString()),
4848
new XAttribute("weight", "1")
4949
));
5050
}

CSharpCodeAnalyst/GraphArea/CodeElementContextCommand.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public CodeElementContextCommand(string label, CodeElementType type, Action<Code
1818
/// <summary>
1919
/// Generic for all code elements
2020
/// </summary>
21-
public CodeElementContextCommand(string label, Action<CodeElement> action, Func<CodeElement, bool>? canExecute = null)
21+
public CodeElementContextCommand(string label, Action<CodeElement> action,
22+
Func<CodeElement, bool>? canExecute = null)
2223
{
2324
_type = null;
2425
_action = action;

0 commit comments

Comments
 (0)