Graph-Based Software Analysis with AI – Source Code Archaeology (4/5)
How to Make Complex Dependencies in Software Analysis Easier to Understand
At a Glance: Graph-Based Software Analysis
While traditional text- and file-based analysis tools quickly reach their limits when dealing with branched structures, graph-based tools make topological dependencies, cyclic structures, control flows, and reliable impact analysis consistently accessible at the click of a button.
In [Part 3 of the blog series “Source Code Archaeology”] (https://www.codecentric.de/en/knowledge-hub/blog/analyzing-source-code-with-git-and-intellij-source-code-archaeology), we showed you how Git and the IDE can already take you a long way toward initially understanding a system. But as soon as complex inheritance hierarchies, a deep call graph, or cyclic dependencies between modules come into play, text-based tools quickly reach their limits in software analysis. In our projects at codecentric, we’ve had good experiences with graph-based tools in this regard. After all, when you’re trying to make sense of an unfamiliar codebase, the crucial question is rarely, “How many lines of code (LOC) does it have?” but rather, “Who is actually communicating with whom here?” Which class depends on which one, which method calls which one, and where do the threads converge? These are, at their core, graph-based questions—it’s about relationships, not just numbers.
What methods are available for software analysis—and when are graphs helpful?
Broadly speaking, there are various approaches to software analysis: classic text-based search, static code analysis, and graph-based analysis. Graph-based analyses translate complex source code relationships into a directly queryable network model, making it easy to detect transitive dependencies, architectural violations, and hidden couplings—unlike purely text-based searches.
Here are the topics covered in this post:
- Why is a graph the natural model for code? How nodes, edges, and properties represent relational structures in the source code.
- How are analysis results persisted and reused? How graph databases store extracted metadata long-term and make it available for automation.
- Which tools are suitable for which tasks? How the established jQAssistant works synergistically with Neo4j (including our own C# plugin) and AI-driven approaches such as codebase-memory-mcp.
- What comes next after understanding the source code? Which refactoring strategies and quality gates help fix identified architectural flaws and ensure high code quality in the long term.
Why a Graph Is the Natural Model for Code
Before we write queries, let’s briefly explain the basics. After all, if you don’t understand the graph, you won’t understand the results either.
A graph consists of nodes and edges (relationships). A node is an entity—in our case, for example, a class, a method, or a namespace. An edge is a directed, typed connection between two nodes, such as “Class A EXTENDS Class B” or “Method X CALLS Method Y.” Both nodes and edges can carry properties: key-value pairs such as name = “UserService” or async = true. And each node can have one or more labels that describe its role. For example, a C# class in our system carries both the labels CSharpClass and CSharpType.
The key difference from a relational database: In a relational database, a relationship exists only implicitly via a foreign key, and to trace it, you need a JOIN. In the graph, the relationship is a standalone, directly stored object. Neo4j calls this index-free adjacency. Each node knows its neighbors directly. As a result, a query such as “find all ancestors of this class” takes constant time per level, regardless of whether the database contains a thousand or a million nodes. In Cypher, Neo4j’s query language, this is written simply as (c)-[:EXTENDS*]->(ancestor) (the asterisk means “any number of steps”). In SQL, this would require recursive Common Table Expressions, whose computational cost increases with each level.
A small snippet from a real C# project looks like this as a graph:
How does the data get into the graph, and how does it stay there?
A graph doesn’t just appear on its own. There is an analysis step that incurs a one-time computational cost. After that, you work on the finished graph without having to touch the source code again. This point is often underestimated, but it’s practically crucial because it determines cost and reusability.
In jQAssistant, a scan runs that reads the project’s structure and writes it to an embedded Neo4j database, known as the “store.” This store is saved locally (by default under target/jqassistant/store). The graph remains persistent unless it is explicitly reset for each execution run via the store.reset: true configuration. After a server restart, the dataset is immediately available for queries again without the need for a new analysis scan. It is important to note the cumulative behavior without a reset: re-scans result in incremental updates rather than a complete synchronization. For example, if a class is removed from the source code and the project is analyzed again, the original node remains in the graph. For this reason, the store is deliberately reset in CI. This ensures that the graph accurately reflects the current state of the code. For exploratory analyses at a fixed point in time, however, it is left as is to avoid performing another scan.
The process is similar with codebase-memory-mcp. The index_repository analyzes the codebase across languages via the Language Servers (LSP), constructs the call graph, calculates complexity metrics, and generates semantic vector embeddings in “full mode.” This is the computationally expensive step. For large repositories, this takes a noticeable amount of time and computing power. Afterward, however, all results are available as a graph, and any further queries—such as call graph tracing, impact analysis, or semantic search—run efficiently on this persisted data. The source code does not need to be analyzed again for this. With the persistence: true flag, the tool even saves the finished graph as a compressed artifact (.codebase-memory/graph.db.zst) that can be shared within the team. Team members work on this instead of reindexing the data themselves. And anyone who just wants to know what has changed since the last update can use detect_changes against a Git ref instead of rebuilding everything from scratch.
The takeaway to remember: The initial analysis is expensive, but subsequent queries are cheap. And the graph can be persisted and shared.
Code Structure as a Graph: Uncovering Weaknesses with Neo4j
Enough theory. Cypher queries reveal structural weaknesses in the codebase. The process with jQAssistant consists of three steps:
Below, we’ll give you some examples of queries that are highly relevant in source code archaeology. For each of these queries, it’s important to understand not only what it does, but also why the result is interesting.
God Classes: Finding Overloaded Classes with Cypher
Finding classes with a conspicuously large number of methods is a common task in source code archaeology. As we already learned in Part 3 of this blog post series, IntelliJ also offers a feature to identify God Classes. Alternatively, here’s how it would look in Cypher:
A class with, say, thirty methods usually combines too many responsibilities (violating the Single Responsibility Principle). Such “God Classes” are difficult to test, risky to modify, and, as experience shows, prone to bugs. The query provides you with a sorted list of refactoring candidates and thus a concrete starting point.
Detecting Excessively Deep Inheritance
Inheritance depths of four to five levels are considered a code smell. Each level increases the cognitive load. To change a subclass, you must understand the entire chain as well as the risk of fragile base classes. This query leverages the graph’s core strength: the seamless tracing of inheritance chains [:EXTENDS*], which would be cumbersome to achieve in SQL.
Identifying Central Abstractions
Frequently implemented interfaces are the cornerstones of the architecture. Changes to them have far-reaching consequences. The query shows where you should be especially careful and have robust tests in place.
Ensuring Architectural Rules with jQAssistant Constraints in the Build
And now comes the real advantage over a one-time analysis. Such queries can be defined as jQAssistant constraints. A constraint fails as soon as the query returns even a single match, thereby terminating the build. This transforms an exploratory analysis into a permanently enforced architectural rule: “No public mutable fields,” “Interfaces begin with I,” “Controllers do not access repositories directly.” This is precisely where understanding turns into enforcement.
Our Own Roslyn Analyzer: Why We Added a C# Plugin to jQAssistant
By default, jQAssistant reads Java bytecode, XML, YAML, and other JVM-related formats. However, in one of our client projects, we were dealing with a purely .NET environment, and at the time, there was no C# plugin available. So we built one here at codecentric.
The plugin is based on Roslyn, Microsoft’s official C# compiler-as-a-service. Roslyn provides a fully semantically resolved syntax tree. So it’s not just “the word class appears here,” but “this is type X, which inherits from Y.” Our analyzer runs as a .NET CLI tool, reads the project, and exports the structure as JSON. The jQAssistant plugin then imports this JSON into the Neo4j graph:
In the graph, the core C# concepts are represented as nodes:
- Structure types:
CSharpProject, CSharpNamespace - Type declarations:
CSharpClass,CSharpInterface,CSharpStruct,CSharpEnum,CSharpRecord - Members:
CSharpMethod,CSharpParameter,CSharpField,CSharpProperty
The edges represent containment, inheritance, and implementation:
DECLARES_TYPEDECLARES_METHODEXTENDSIMPLEMENTSHAS_PARAMETER
And because honest tool descriptions are worth more than marketing: the plugin currently captures the static structure, but not a call graph. This means there are no INVOKES edges. Dependency analysis is therefore possible via inheritance and implementation, but not via actual method calls in the method bodies. If you need the call flow, you’re better off with the next tool.
codebase-memory-mcp: Call Graph and Knowledge Graph for AI
While jQAssistant focuses on structural rules and living documentation, codebase-memory-mcp serves a different purpose: It converts a codebase into a knowledge graph and makes it available to an AI agent such as Claude Code via the Model Context Protocol (MCP). Instead of manually searching through the files, the agent performs structured graph queries and provides exactly what our C# plugin is missing: call graph, data flow, and cross-service connections.
The data model is correspondingly richer. Functions and methods don’t just have names, but also metrics such as cyclomatic, cognitive, loop_depth, and—particularly exciting—transitive_loop_depth. That is, the loop depth propagated across method boundaries. There are CALLS and DATA_FLOWS edges, and for distributed systems, HTTP_CALLS as well as CROSS_HTTP_CALLS between routes of different services.
This enables analyses that go far beyond mere structure. A few examples:
Detecting Hidden O(n²) Problems
Nested loops (including those that arise across multiple method calls) and linear scans (a Contains within a loop) are the most common causes of performance degradation as data volumes grow. This query identifies them before they cause problems in production. A text search cannot do this because the depth is calculated interprocedurally.
Impact Analysis Before Making a Change
For example, before you modify the DeleteUser function, this query shows you all callers up to five levels deep. This is the foundation of any serious impact analysis—and protects you from the unpleasant surprise of discovering that someone, somewhere deep in the code, is actually using the method.
Finding Dead Code Using a Graph
Functions with no incoming calls and no entry point are candidates for removal. Using the call graph is more precise than a text search—although you should always verify the result, for example, due to reflection.
Untangling the Distributed Monolith
This type of question—“Who calls whom, and are there any self-calls or cycles?”—is typical of mature microservice landscapes that, upon closer inspection, turn out to be distributed monoliths: services that are tightly and, in some cases, circularly dependent on one another instead of being loosely coupled. The graph makes such hidden couplings visible at a glance.
Visualization in the Neo4j Browser: Graph View vs. Table View
The fastest way to analyze your data is in the Neo4j Browser. In Graph View, nodes appear as circles and edges as labeled arrows, color-coded by label—ideal for interactively exploring a local section of the graph.
For pure metric analysis aimed at identifying overly complex classes with too many responsibilities (God Classes) as well as particularly frequently executed paths (Hot Paths), the table view is clearer.
No single tool stands alone: integration with ArchUnit and SonarQube
None of these tools replaces the others, as they cover different levels and are interdependent. A typical analysis and verification workflow at our company often looks like this:
- codebase-memory-mcp handles the discovery phase: The AI agent explores the call graph, identifies hotspots, and uncovers problematic patterns (“this controller calls a repository directly”).
- Where a finding can be captured structurally, we formalize it as a jQAssistant constraint and have it checked periodically (e.g., overnight).
- Where a rule can be enforced quickly and locally during the build, it is also added to the unit test suite as an ArchUnit test. This causes the build to fail immediately if the rule is violated.
- At the same time, SonarQube prioritizes the specific quality and security issues at the identified locations.
The results flow between the tools: A Cypher evaluation from jQAssistant can be exported as a CSV to CodeCharta, as can SonarQube metrics. This creates a shared view among multiple specialists. Everyone excels in their own area, and the handoffs turn individual findings into a comprehensive overview.
| Tool | Strength | Role in the workflow |
|---|---|---|
| codebase-memory-mcp | Call Graph, Impact, Semantics | Discovery |
| jQAssistant (+ C# plugin) | Structural rules, multi-tech, documentation | Constraints, periodic audits |
| ArchUnit | Architecture rules as unit tests | Build gate |
| SonarQube | Bugs, Security, Smells, Coverage | Prioritization, Quality Gate |
Conclusion: Use Graph-Based Software Analysis Strategically
Even in graph-based code analysis, there is no single perfect tool. jQAssistant, codebase-memory-mcp, ArchUnit, and SonarQube answer different questions and demonstrate their value most effectively when used in combination. Therefore, the key is not to introduce as many tools as possible, but to clearly define your specific questions. Do we want to understand dependencies, validate architectural rules, evaluate changes, or prioritize specific quality risks?
Graph-based software analysis adds an important perspective to daily source code archaeology. It reveals relationships that span many files, classes, and services. jQAssistant helps anchor structural insights as permanent rules. codebase-memory-mcp accelerates the exploration of call chains, data flows, and the potential impacts of a change. Together with traditional analysis tools, this creates a more solid foundation for technical decisions. The findings also help determine which modernization steps can reduce maintenance effort and risks to the core business.
It’s important to remember: a graph provides clues, not definitive conclusions. A conspicuous class, a suspected hotspot, or a cyclical dependency must be evaluated within the technical context. But that’s precisely where the benefit lies. Instead of blindly modifying a legacy system, teams can start with a transparent diagnosis and gradually validate their architecture.
In the next part of the series, we’ll take it a step further: What happens if the right analysis tool doesn’t exist yet? We’ll explore how to develop small, AI-powered analysis tools tailored to specific questions.
The “Source Code Archaeology” Blog Series
- Part 1: Fundamentals & Business Perspective – What source code archaeology achieves and why the most reliable way to modernize legacy systems begins with understanding the existing business logic and the technical state of the application.
- Part 2: The Tool Market – Which tools exist in the field of source code archaeology and which tool is suitable for which problem.
- Part 3: Analysis with Standard Tools – How development teams can uncover valuable system knowledge using existing standard tools without an additional budget.
- Part 4: Graph-Based Code Analysis – A comparison of conventional tools with AI-powered code graphs, and how both approaches can be combined.
- Part 5: Artificial Intelligence & Autonomy – From MCP servers to AI agents: Can generative AI soon take over source code archaeology entirely? (Coming December 1, 2026)
More on Software Modernization
More articles in this subject area
Discover exciting further topics and let the codecentric world inspire you.
Blog author
Benjamin Mies
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.